Sprites using angle and velocity

BlitzMax Forums/BlitzMax Programming/Sprites using angle and velocity

Scott Shaver(Posted 2006) [#1]
Normally when making a sprite class (type) I would have separate velocity values for x and y. I decided to try something different and use a velocity and an direction (velocity angle). Seems to work pretty well. Here is a little test with an explosion sprite, space ship sprite and a sprite particle emitter. Nothing Earth shattering here, just thought I would share.

http://www.scottshaver2000.com/blitz/spritestemp/spritestemp.zip




ImaginaryHuman(Posted 2006) [#2]
There's no reason why you can't use an angle instead of a vector, then you just add to or subtract from the angle. Some more complex math might be trickier but for basic movement it's okay. Then you just need to turn the angle into x and y `adders` for each frame.


FlameDuck(Posted 2006) [#3]
Yes. There are two types of vectors. Rectangular (X , Y in offsets) and Circular (Angle [usually in radians] , Velocity). They are interchangable.

The reason people use the former on computers is because certain operations (like dot products) are faster that way (as they do not require trigonometric functions).


SSS(Posted 2006) [#4]
Technically it's more just a difference between rectangular vs. polar coordinates. It would be cool to try some really wacky coordinate systems. Imagine playing a game in which movement was controlled by a nice curvillinear coordinate system.


Scott Shaver(Posted 2006) [#5]
Okay so now I'm applying acceleration and I have a bit of a problem. The calculations only work for 0-90 degrees.

Type Sprite
	Field velocity:Float = 0 	' The velocity of the sprite
	Field velocityAngle:Float = 0 ' The angle at which the velocity is directed
	
	Rem
	bbdoc: Apply a certain ammount of acceleration to the sprite.
	about:
	End Rem
	Method ApplyAcceleration(accel:Float, toangle:Float)
		Local Vx:Float = velocity*Cos(velocityAngle)
		Local Vy:Float = velocity*Sin(velocityAngle)
		Local Ax:Float = accel*Cos(toangle)
		Local Ay:Float = accel*Sin(toangle)
		Local Rx:Float = Vx+Ax
		Local Ry:Float = Vy+Ay
		
		
		Local Rm:Float = Sqr((Rx*Rx)+(Ry*Ry))
		Local Ra:Float = ATan(Ry/Rx)
		'If Ra<0 Then Ra=360+Ra
		Print "vx="+Vx+" vy="+Vy+" v="+velocity+" va="+velocityAngle+" a="+accel+" aa="+toangle+" rm="+Rm+" ra="+Ra
		velocity = Rm
		velocityAngle = Ra 
	End Method



Haramanai(Posted 2006) [#6]
Why you are doing something like this?
Think that any position in the coordinates system it's a point... and a point it's a vector!
So you have to play around with vectors grab a Vector type from the MaxPhisics. Or grab SSS's Vector type posted at the sticky topic of maxPhisics.

I am saying that because if you continue this way you will be writing the same vector maths over and over again.
Think about it.


Scott Shaver(Posted 2006) [#7]
I figured it out, the problem was that ATan only functions correctly for -90 to 90. I changed to ATan2 and it works prefectly now. :)

Local Ra:Float = ATan2(Ry,Rx)


Scott Shaver(Posted 2006) [#8]
A new zip is up with two examples in it now. The one shown above and another one with a ship you can move around and fire. It uses acceleration.


Scott Shaver(Posted 2006) [#9]
Another new zip with a third example of attaching a SpriteEmitter (particle system) to show the ship flames when the player is accelerating.