Im in search of various kinds of algorithms or collections of ways to move sprites, like move an object from one point to another, or make it do weird movements, or make it bounce back and forth, make it follow a path, make it tremble and things like that, i would like to experiment with various kinds of movements for special effects in my game, not actual "enemy" or player movement or physics, and since im very bad with math some help will save my life, thanks in advance.
Various kinds of movements
BlitzMax Forums/BlitzMax Beginners Area/Various kinds of movements To simply move right,left, up, or down, you just have to tell your sprite to add or subtract from the x and y coordinates of the sprite.
For example:
(this isn't as long as it seems;it's mostly comments to make sure you understand it all the way.
if you already know how to do this, skip down to the next post on how to jump)
'****set graphics*****
Graphics 600,800,16 'fullscreen
'****variables****
Global x:Float=0 'x-coordinate variable
Global y:Float=0 'y-coordinate variable
'****start loop****
While Not KeyDown(key_escape) 'as long as you don't push the escape button, it will continue to make the loop
DrawRect x,y,10,10 'sprite = 10x10 square
'***input keys***
If KeyDown(key_left) 'if you want to say while key_left not down, you just say "keydown(key_left)=false
x:-1 'subtract one from x is now =to x; try changing the value of what you subtract x with.
EndIf
If KeyDown(key_right)
x:+5 'add to x; going right 5 pixels; notice the speed of going right compared to left
EndIf
If KeyDown(key_up)
y:-1 'this is going up 1; not down because if you look at the screen, 0,0 starts at the top left and is positive going down and right
EndIf
If KeyDown(key_down)
y:+5 'this is going 5 down; faster than going up
EndIf
If KeyHit(key_space) 'if you tap space; holding it down doesn't matter
x=300 'go to center;resetting what x and y equal
y=400
EndIf
Flip 'this is just drawing the program onto the screen: the rectangle
'cls 'take out the first appostraphe in order to uncomment the cls(clear screen) to see what happens
'******End loop******
Wend
'*******Done!!!!***********
Try it out with the "cls" behind the apostraphe first. Try to spell your name with it. Then take out the apostraphe and see what happens.
For example:
(this isn't as long as it seems;it's mostly comments to make sure you understand it all the way.
if you already know how to do this, skip down to the next post on how to jump)
'****set graphics*****
Graphics 600,800,16 'fullscreen
'****variables****
Global x:Float=0 'x-coordinate variable
Global y:Float=0 'y-coordinate variable
'****start loop****
While Not KeyDown(key_escape) 'as long as you don't push the escape button, it will continue to make the loop
DrawRect x,y,10,10 'sprite = 10x10 square
'***input keys***
If KeyDown(key_left) 'if you want to say while key_left not down, you just say "keydown(key_left)=false
x:-1 'subtract one from x is now =to x; try changing the value of what you subtract x with.
EndIf
If KeyDown(key_right)
x:+5 'add to x; going right 5 pixels; notice the speed of going right compared to left
EndIf
If KeyDown(key_up)
y:-1 'this is going up 1; not down because if you look at the screen, 0,0 starts at the top left and is positive going down and right
EndIf
If KeyDown(key_down)
y:+5 'this is going 5 down; faster than going up
EndIf
If KeyHit(key_space) 'if you tap space; holding it down doesn't matter
x=300 'go to center;resetting what x and y equal
y=400
EndIf
Flip 'this is just drawing the program onto the screen: the rectangle
'cls 'take out the first appostraphe in order to uncomment the cls(clear screen) to see what happens
'******End loop******
Wend
'*******Done!!!!***********
Try it out with the "cls" behind the apostraphe first. Try to spell your name with it. Then take out the apostraphe and see what happens.
Hi :)
Just a question, since you wrote Sprite, Are you going to use 3D?
Else, Here's a quick Atan2 Point-to-Point waypoint movement thingie I wrote for you ;)
Just a question, since you wrote Sprite, Are you going to use 3D?
Else, Here's a quick Atan2 Point-to-Point waypoint movement thingie I wrote for you ;)
'############################################################################################################# Strict '############################################################################################################# 'Graphics Graphics 800 , 600 '############################################################################################################# 'Globals Global MyX:Float = 0 Global MyY:Float = 100 '############################################################################################################# 'Waypoint type Type TWaypoint '######################################################################################################### Global List:TList = CreateList() Global Current:TWayPoint '######################################################################################################### Field X:Float , Y:Float '######################################################################################################### 'Constructor Function Create:TWayPoint( _X:Float , _Y:Float ) Local A:TWayPoint = New TWayPoint A.X = _X A.Y = _Y A.List.Addlast( A ) Return( A ) End Function '######################################################################################################### 'Updater Function Update() Local A:Double 'Check if there's a current Waypoint to reach. If Self.Current <> Null 'Check if Current WayPoint was reached. (With a 0.5 margin) If MyX < Self.Current.X + 0.5 And MyX > Self.Current.X - 0.5 And MyY < Self.Current.Y + 0.5 And MyY > Self.Current.Y - 0.5 'Remove a reached waypoint from the list. Self.List.Remove( Self.Current ) 'And find a new one. Self.Current = TWayPoint(Self.List.First() ) Else 'Here's the algorithm for moving to a point using Atan2. A = ATan2( Self.Current.Y - MyY , Self.Current.X - MyX ) MyX:+ Cos( A ) MyY:+ Sin( A ) EndIf Else 'If there isn't a waypoint to reach, It'll go for the first one in the list. Self.Current = TWayPoint(Self.List.First()) EndIf End Function '######################################################################################################### End Type '############################################################################################################# 'Create some waypoints ;D TWaypoint.Create( 100 , 100 ) TWaypoint.Create( 300 , 100 ) TWaypoint.Create( 300 , 300 ) TWaypoint.Create( 100 , 300 ) TWaypoint.Create( 500 , 500 ) TWaypoint.Create( 300 , 100 ) '############################################################################################################# 'Main Loop While Not KeyHit( KEY_ESCAPE ) DrawRect( MyX, MyY, 32, 32 ) TWayPoint.Update() Flip ; Cls Wend End '#############################################################################################################
JUMPING!!!!!
Here is an easy way to get a sprite to jump up and come back down.
'*******start code*********
Graphics 600,800
Global x:Float=0 'character coordinates
Global y:Float=400
Global jump:Int=0 'a flag; tells us if we are jumping or not (0=no jump,1=jump)
Global gravity:Float=0.5 'this affects the height of the jump
Global jumpheight:Float=7 'this is the max height of the jump; mess around with these numbers
While Not KeyDown(key_escape)
SetColor 192,0,0 'red
DrawOval x,y,10,15 'x,y,width,height of oval
SetColor 192,192,192 'white
DrawLine 0,415,600,415 'horizontal line from x1,y1 to x2,y2 right below the sprite
If KeyDown(key_space)
jump=1 'flag activated
EndIf
Rem
this is wher you would add left and right movements
discussed in the first post by Jebbs (me)
that way you can move side to side while jumping
these were just copied to save time
EndRem
If KeyDown(key_left)
x:-1 'this makes it seem like you are going against the wind when you go left; could be an idea?
EndIf
If KeyDown(key_right)
x:+5
EndIf
Rem
notice how I took out the going up and down codes;
you don't need them in a jumping side to side scrolling game
howerver, you could add them to see what will happen after you jump at a higher altitude
EndRem
Dojump() 'just a lable, when a function calls it up later, it will be activated
If y>=400 'y starts at 400 remember; once it gets back to 400 after jumping, it will stop falling
'*if y>=400 means, if y is greater than or equal to 350
jump=0 'now launch is over; inorder to jump again, must reset flag to 0
jumpheight=7 'later on, jumpheight will get changed; this just sets it back to normal again
EndIf
Flip
Cls
Wend
Function dojump() 'goes to the line that says "dojump()" and processess through it
If jump=1 'if the flag says "yes" then jump; in otherwords, if spacebar, then flag = 1(yes), then actually jump
y:-jumpheight 'while jump=1, take the y coordinate and subtract it by height
jumpheight:-gravity 'basically widdles "jumpheight" until it starts to go the other way; kinda like a parabola!
EndIf
If jumpheight<=-5
jumpheight=-5 'makes it so that jumpheight doesn't get lower than negative five
EndIf
EndFunction
'********Done!!!!!!!!***********
I give credit to Amon in one of his posts, "Jump Code for Kaisuo". They should be quite similar.
I still am trying to figure out some nice codes for moving sprites myself. I've been looking for a good way of adding momentum to my sprites so that when they hit a wall, they will take several steps back. And other things like that.
Well, I hope this helped you.
Goodluck
PS- Do you know how to insert images as sprites?
Like, substituting the ovals and rectangles with pictures?
Ex.: global Sprite:timage("sprite.png")
drawimage sprite,x,y
Here is an easy way to get a sprite to jump up and come back down.
'*******start code*********
Graphics 600,800
Global x:Float=0 'character coordinates
Global y:Float=400
Global jump:Int=0 'a flag; tells us if we are jumping or not (0=no jump,1=jump)
Global gravity:Float=0.5 'this affects the height of the jump
Global jumpheight:Float=7 'this is the max height of the jump; mess around with these numbers
While Not KeyDown(key_escape)
SetColor 192,0,0 'red
DrawOval x,y,10,15 'x,y,width,height of oval
SetColor 192,192,192 'white
DrawLine 0,415,600,415 'horizontal line from x1,y1 to x2,y2 right below the sprite
If KeyDown(key_space)
jump=1 'flag activated
EndIf
Rem
this is wher you would add left and right movements
discussed in the first post by Jebbs (me)
that way you can move side to side while jumping
these were just copied to save time
EndRem
If KeyDown(key_left)
x:-1 'this makes it seem like you are going against the wind when you go left; could be an idea?
EndIf
If KeyDown(key_right)
x:+5
EndIf
Rem
notice how I took out the going up and down codes;
you don't need them in a jumping side to side scrolling game
howerver, you could add them to see what will happen after you jump at a higher altitude
EndRem
Dojump() 'just a lable, when a function calls it up later, it will be activated
If y>=400 'y starts at 400 remember; once it gets back to 400 after jumping, it will stop falling
'*if y>=400 means, if y is greater than or equal to 350
jump=0 'now launch is over; inorder to jump again, must reset flag to 0
jumpheight=7 'later on, jumpheight will get changed; this just sets it back to normal again
EndIf
Flip
Cls
Wend
Function dojump() 'goes to the line that says "dojump()" and processess through it
If jump=1 'if the flag says "yes" then jump; in otherwords, if spacebar, then flag = 1(yes), then actually jump
y:-jumpheight 'while jump=1, take the y coordinate and subtract it by height
jumpheight:-gravity 'basically widdles "jumpheight" until it starts to go the other way; kinda like a parabola!
EndIf
If jumpheight<=-5
jumpheight=-5 'makes it so that jumpheight doesn't get lower than negative five
EndIf
EndFunction
'********Done!!!!!!!!***********
I give credit to Amon in one of his posts, "Jump Code for Kaisuo". They should be quite similar.
I still am trying to figure out some nice codes for moving sprites myself. I've been looking for a good way of adding momentum to my sprites so that when they hit a wall, they will take several steps back. And other things like that.
Well, I hope this helped you.
Goodluck
PS- Do you know how to insert images as sprites?
Like, substituting the ovals and rectangles with pictures?
Ex.: global Sprite:timage("sprite.png")
drawimage sprite,x,y
Thanks a lot!, Jebbs, i was mainly asking for something less specific than player movement, something more like Hule made, thanks a lot for taking the time though, really appreciated.
I mayde do some fake 3D by resizing in and out the sprite and adding some shadow, but no more than that.
I did a search on this subject of "movement algorithms" and i just could find anything, wich is very weird, it could be so usefull for so many programmers because we all need to move sprites in different ways, having a common library to choose what kind of movement we are looking for would save so much time and effort.
As an example, imagine i am doing a vertical shoot em up game, where the bullets and enemies all do very weird movements, some follow paths, some bullets go back and fort, the explotions move all in different ways, and so on, imagine if we all had a library of possible movements, we just copy and paste code, mix different ones, and then we have the movement done for our bullet/enemy/effect just a little tweaking here and there and thats it... thats what i had in mind :)
I mayde do some fake 3D by resizing in and out the sprite and adding some shadow, but no more than that.
I did a search on this subject of "movement algorithms" and i just could find anything, wich is very weird, it could be so usefull for so many programmers because we all need to move sprites in different ways, having a common library to choose what kind of movement we are looking for would save so much time and effort.
As an example, imagine i am doing a vertical shoot em up game, where the bullets and enemies all do very weird movements, some follow paths, some bullets go back and fort, the explotions move all in different ways, and so on, imagine if we all had a library of possible movements, we just copy and paste code, mix different ones, and then we have the movement done for our bullet/enemy/effect just a little tweaking here and there and thats it... thats what i had in mind :)
For anyone interested, i found this article on smooth spline path following thingie http://www.shmup-dev.com/forum/index.php?cat=18;tpstart=1 you can find other cool stuff on the site also.
A well known animation technique, but nevertheless worth to mention is the parametric animation technique.
With this technique you can easily do all kinds of (weird) movements, as well as using the same parameter for transparency, scale, rotation etc.
A very good tutorial can be found here:
http://www.jmckell.com/parametric.html (the rest of the animation tutorial is very helpful and inspiring, too).
It's written for Director-programmers and the examples are therefor in Lingo but that's simple to convert to BlitzMax.
With this technique you can easily do all kinds of (weird) movements, as well as using the same parameter for transparency, scale, rotation etc.
A very good tutorial can be found here:
http://www.jmckell.com/parametric.html (the rest of the animation tutorial is very helpful and inspiring, too).
It's written for Director-programmers and the examples are therefor in Lingo but that's simple to convert to BlitzMax.
Ok, so you're trying to make a game kind of like galaga. Cool. I've been planning on making a verticle shooting game sometime, so I tried looking up some good ideas you might be able to use; I found a good code of a verticle shooting game where the aliens, or whatever, come down in squiggly lines and they randomly shoot. It might be helpful. I didn't try to read it so I really don't know how much use it will be to you.
http://www.blitzmax.com/Community/posts.php?topic=55150
I think waves of enemies would be a good idea just like in galaga. I'll keep an eye out for some codes like that.
http://www.blitzmax.com/Community/posts.php?topic=55150
I think waves of enemies would be a good idea just like in galaga. I'll keep an eye out for some codes like that.
No, actually i wanted to do a system for moving everything in my game, fx, buttons, enemies, ui elements, etc, so im thinking in making a system to be able to move all kinds of objects in many kinds of ways. Thanks for the links, they are really usefull to me.