Moving a sprite to a pre-determined location

BlitzMax Forums/BlitzMax Programming/Moving a sprite to a pre-determined location

Chroma(Posted 2009) [#1]
What method are you guys using to move a sprite from it's original location to a new location?

Of course you have to get the dist/angle and use Atan2(xdist,ydist) and then sin and cos to move it. But how are you guys stopping the tile when it gets to the spot? Are you using the distance traveled or just an x and y check to see if it's close and then placing the tile where the new spot is?


_Skully(Posted 2009) [#2]
I'm not sure of the context you are talking about? Are you referring to a sprite colliding with a tile?


GfK(Posted 2009) [#3]
But how are you guys stopping the tile when it gets to the spot?

Pseudocode:
Move SpriteA towards SpriteB by StepSize

If DistanceBetween(SpriteA,SpriteB) <= StepSize
  SpriteA.X = SpriteB.X
  SpriteA.Y = SpriteB.Y
EndIf



_Skully(Posted 2009) [#4]
Here's a function you might find useful too...

Function MaxVelocity(vx:Float Var,vy:Float Var,maxv:Float)
	' adjusts speed to maximum
	Local v:Float=Sqr(vx^2+vy^2)
	If v>maxv
		Local div:Float=maxv/v
		vx:*div
		vy:*div
	End If
End Function


It stops your angular speed from increasing beyond maximum if you are using any kind of velocity


sswift(Posted 2009) [#5]
The answer is: it depends on what you want the sprite to do.

You can move a sprite from point A to point B at a set velocity. To do that you'd store both points and the time at which the sprite began moving, (or accumulate time each frame in a counter) and then each frame calculate how far along that line the sprite should be, between 0 and 1, and then translate that into an XY position. At some point you'll calculate the sprite should be at 1.1 or something, in which case your sprite finished moving halfway through a frame and you just set its position to the end of the line.

Or you can move sprites with physics, in which case you don't really need to care about moving the sprite to a specific point.