problem with angles in asteroids clone

BlitzMax Forums/BlitzMax Programming/problem with angles in asteroids clone

technospy(Posted 2015) [#1]
the angle accellerates too fast with velocity. is this the right formulea?
Function turn(ships:ship)
	If ships.ang > 360 ships.ang :- 360
	If ships.ang < 0 ships.ang :+ 360
	If KeyDown(key_left) Then ships.ang:-1
	If KeyDown(Key_right) Then ships.ang:+1
	If KeyDown(Key_up) Then ships.vel = 0.05 Else ships.vel = 0
End Function


Function move(ships:ship)
	If ships.vel Then ships.xvel:+Cos(ships.ang * ships.vel)
	If ships.vel Then ships.yvel:+Sin(ships.ang * ships.vel)
	ships.x:+ships.xvel
	ships.y:+ships.yvel
        If ships.vel > 0 Then ships.vel:-1
	If ships.x > 640 Then ships.x = 0
	If ships.x < 0 Then ships.x = 640
	If ships.y > 480 Then ships.y = 0
	If ships.y < 0 Then ships.y = 480
End Function



Floyd(Posted 2015) [#2]
	If ships.vel Then ships.xvel:+Cos(ships.ang * ships.vel)
	If ships.vel Then ships.yvel:+Sin(ships.ang * ships.vel)


Should no doubt be

	If ships.vel Then ships.xvel:+ Cos(ships.ang) * ships.vel
	If ships.vel Then ships.yvel:+ Sin(ships.ang) * ships.vel


A couple of observations; testing a float for an exact value, such as if ships.vel is zero, is doomed to failure. Due to the approximate nature of floating point arithmetic you should instead test whether it is close to zero.

And in the present case you could simply omit the test. If ships.vel is zero then at worst you waste an infinitesimal amount of time adding zero to two variables.


Derron(Posted 2015) [#3]
The veeeery small amounts could make up a difference - especially when rendered at "int" positions in a later stage (if done so).

So testing for "vel < e" might be fruitful.


bye
Ron