Asteroids - Spaceship Movement Code

Blitz3D Forums/Blitz3D Programming/Asteroids - Spaceship Movement Code

Davros(Posted 2015) [#1]
Hi guys

I have literally spent days googling, trying to find spaceship movement code asteroids style but to work in 3D on the XZ plane.

Almost all of the examples I found were for 2D screen coordinates. I understand that I need to keep a local velocity vector and thrust for acceleration.

I got close, but found that when I turn the ship to fire the engines, it was immediate - no thrusting against the already built up inertia in the opposite direction to give a smooth decelerate and accelerate in the new direction.

I found something primitive in the code archives but it does the same. Can anyone help with some example code please?


Matty(Posted 2015) [#2]
This is the most basic version (concept) you can use which should work fine....

x = x + vx
y = y + vy
z = z + vz

vx = vx + ax
vy = vy + ay
vz = vz + az

set ax,ay,az based on applying 'thrust'.....

you may wish to limit the maximum value for vx,vy,vz (square root usually involved)


Davros(Posted 2015) [#3]
so the vector 'a' is the acceleration vector?


Matty(Posted 2015) [#4]
Yes....sorry I should have been clearer.

this is a very basic method - (similar to Euler Integration which is a numerical technique that improves as long as the timestep is small however I wouldn't recommend it for anything that needs a high degree of accuracy over time - but it is good enough for games)

EDIT - similar to this: http://mathworld.wolfram.com/EulerForwardMethod.html


Stevie G(Posted 2015) [#5]
Something like this should work. No need for square roots etc.. and maximum speed is derived from from the drag and acceleration. Just play about with the variables.

Drag# = 0.99
Acceleration# = .03
TurnSpeed# = 3.0

'Maxmium Speed = Acceleration / ( 1.00 - Drag ) = 3.0

ACC_PRESSED = keydown(200)
TURN_PRESSED = keydown(203) - keydown(205)

'Movement
Tformvector 0,0,Acceleration * ACC_PRESSED, ShipEntity, 0
Ax# = tformedx()
Az# = tformedz()
Vx# = Vx# * Drag + Ax#
Vz# = Vz# * Drag + Az#
translateentity ShipEntity, Vx, 0, Vz

'Turning
turnentity ShipEntity,0,TurnSpeed * TURN_PRESSED, 0


You could also build momentum into the turning but I don't think asteroids does this. See post 13 from this thread ..

http://www.blitzbasic.com/Community/posts.php?topic=44999#500683

Stevie


Davros(Posted 2015) [#6]
no it's fine, no need for turning momentum and this isn't for an asteroids clone (god there are plenty of them), but it's for my ship movement code... I wanted the directional inertia. My ships are meant to be generally large so I am going to have to figure out where I can Insert a Mass variable into the Acceleration.

I see you have defined Maximum Speed but i'm unsure as to where that goes. (unless we are talking of using that to compare a vector length and cap it?)


Matty(Posted 2015) [#7]
Well.. if good old F = ma applies then simply let a = your thrust force / mass.

Drag only really applies in an atmosphere and increases in proportion to the square of the velocity of the object (cross sectional area comes into play too)


Matty(Posted 2015) [#8]
@Stevie - you are aware the tform commands use square roots internally?


Stevie G(Posted 2015) [#9]
@ Matty, Tformnormal does of course but I can't see why would you need a sqrt internally for the tformvector command?

@ Davros, maxmium speed is implied from the drag and the acceleration, there is no need to actually define it. You need to play about with the 2 values to see what feels right.

e.g.
Acceleration = 0.05, Drag = 0.98 => Max Speed = 0.05 / ( 1 - 0.98 ) = 2.5

Basically if you were at a speed of 2.5 and you continue to hit the thrust then the next frame the speed would be ( 2.5 * .98 + 0.05 ) = 2.5.


Matty(Posted 2015) [#10]
Because you are transforming the vector from one space to another (ie local object to world space) if there are any differences in the rotation between the two spaces then square roots will be involved internally ( or worse trigonometric functions) when the rotation matrix is calculated.

Ultimately whichever method is easier is probably best but ive always found it simpler to do the basic calcs explicitly and only involve various tform commands when it was absolutely necessary to transform between spaces.


Davros(Posted 2015) [#11]
I didn't realise trig functions were slower than sqrt


Davros(Posted 2015) [#12]
Many thanks to @Stevie G and @Matty. Code works a treat.


Stevie G(Posted 2015) [#13]
@ Matty, ok, maybe it does use sqrt internally but why would you care as it's hardly likely to be a bottleneck in terms of execution speed?

My mention of not using square roots related to limiting the magnitude of a velocity vector. It is unecessary in the simple example I've shown.

I've always found the tform commands to be among the most useful and powerful commands within Blitz3d.

To each their own :)


Davros(Posted 2015) [#14]
@Stevie G / @Matty ..If you were to attach a helper child object to a parent and change it's local position to that parent, When you rotate the parent, does that still use the square root to re-calculate the child position? or is it managed in some less expensive way?


Matty(Posted 2015) [#15]
To be honest im not concerned with the speed of a square root....i just prefer the simplicity of my method....which is fairly accurate in most situations.


EpicElrod(Posted 2015) [#16]
I have a book that teaches you to make your own version of that game...