Sprites using angle and velocity
BlitzMax Forums/BlitzMax Programming/Sprites using angle and velocity
| ||
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 ![]() |
| ||
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. |
| ||
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). |
| ||
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. |
| ||
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 |
| ||
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. |
| ||
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) |
| ||
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. |
| ||
Another new zip with a third example of attaching a SpriteEmitter (particle system) to show the ship flames when the player is accelerating.![]() |