Long time no keyboard input!
Well, the title says it all... almost. :P
I've been busy trying to make a snassy top-down space shooter using the classic way of achieving asteroids movement.
Though I've realized that the "classic way" does not actually replicate pure asteroids/space movement... what's this?
When referring to the "classic way" I mean using:
Xvel = Xvel + Cos(angle)*acceleration
Yvel = Yvel - Sin(angle)*acceleration
Xpos = Xpos + Xvel
Ypos = Ypos + Yvel
X = Xpos
Y = Ypos
That's the classic answer to making asteroids movement.
Though there is a snag in that formula. It becomes very clear when using it to "travel" long distances with a capped maxvelocity.
The problem is evident in 90 degree course changes.
Example) You are moving at an angle of 90 degrees i.e up, at a velocity of 0,-100(Xvel,Yvel). You rotate your ship 90 degrees clockwise(i.e 0 degrees) and start accelerating. This results in the Xvelocity increasing to a capped 100 while maintaining Yvelocity at -100, just how we like to believe spacemovement to work(ignoring the capped speed), ending with our ship soaring steadily at 45 degrees up.
Now, say we tilt our angles with 45 degrees, starting us at an angle of 45 degrees at a velocity of 100,-100(Xvel,Yvel). Rotate 90 degrees clockwise again(to 315 degrees) and start accelerating, alas what is this!? The ship behaves more like a skidding car and will make a nice turn resulting in a velocity of 100,100(Xvel,Yvel) soaring towards an angle of 315 degrees/45 degrees down!?
How can this be?
I'll tell you, it's because of the Sin and Cos functions.
In the first example when accelerating towards the angle 0 Xvel and Yvel are both increased with our accelerating force, the Xvel is increased with the acceleration multiplied by Cos of the angle i.e
[Xvel + Cos(0)*acceleration]
Cos(0) is 1, so we get a proper acceleration towards the right.
Sin(0) is 0, so the acceleration for the Yvel is multiplied by zero, leaving our Yvel untouched.
But when tilted 45 degrees strange things happen,
Cos(315) is 0.707 and Sin(315) is -0.707 increasing our Xvel with 0.707 * acceleration and our Yvel with 0.707 * acceleration(Yvel-[-0.707]*acceleration) resulting in our 100,100 velocity!
Now, with the logic of the first example we should be moving at an angle of 0 degrees with a velocity of 100,0(Xvel,Yvel), but instead the ship behaves like if it was subject to friction.
That's the s-C-a-M!
Ok, ignoring the rant, does anyone know how the program a movement that solely behaves as either one of the examples, i.e space-ish OR frictionary(!), not combining the two depending on cardinal directions.
Sorry for the long post, I would have included an example, but just didn't, my bad. I'll try and whip one up soon so you can see what I mean.
In the meantime, feel free to help.
Cheers!
//EE







Reply With Quote



