Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

3 Dimensional Chat / How to calculate a new direction of a moving 3D object by applying a force

Author
Message
Rudders
12
Years of Service
User Offline
Joined: 20th Jan 2012
Location: Oxfordshire UK
Posted: 29th Oct 2019 16:50
Hi all,

I have a moving 3D object, I can rotate the object whilst it is moving. I want to apply a force to the object in the direction it is facing when rotated. The new direction and velocity will be the resultant calculation of the original direction and velocity effected by the new force and its angle. Mmmmm, hope I have explained the problem. I have a 3D moon lander travelling in a straight line along its Z axis. I rotate the lander as it is moving along its path, the direction of the path obviously does not change. I want to apply a thrust in the direction the lander is now facing to change its direction.

I cannot find a 3D 'Impulse' force command that make the effect realistic and so I guess a new Velocity Vector must be calculated to apply to the lander using its current direction and velocity and the new rotated angle of the lander plus the new thrust.

Any ideas Amigos?
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 16th Nov 2019 03:47
You don't need special commands for this, the math is pretty simple.

Apply a force to your lander. That force can continue to build up, at least until it hits a defined limit (terminal velocity). This force would be the thrusters but that's not the only force. Another force has to act on the lander as well or it'd continue forever. Gravity, friction, wind resistance, these would be the most common forces. In space, you might not have these forces so maybe you do want it to move forever until the thrusters turn it around. If so, skip this additional force.

Apply your forces and update the lander's position. A very simple explanation but that's all there really is to it. So how do you do all this in math?

Let this represent the lander's current velocity.
lander.vx = 0
lander.vy = 0


This is the amount of force applied by the thruster. You could also think of this as the lander's acceleration.
thruster.force = 0.5

Calculate the velocity of the thruster (its speed in a given direction).
thruster.vx = cos(lander.angle) * thruster.force
thruster.vy = sin(lander.angle) * thruster.force


The SIN and COS will calculate a direction vector based on the lander's angle of rotation (the angle your ship is facing). This will be normalized so we simply multiply the values by the thruster's force.

Now we add the thruster's speed to the lander.

lander.vx = lander.vx + thruster.vx
lander.vy = lander.vy + thruster.vy


If there's no other forces acting on this, the lander will float in this direction forever at the same speed. You may want this effect in space. If you would like it's momentum to gradually slow down when the thruster is not applied, then we need a friction value. The above equation will instead look like this:

friction# = 0.9

lander.vx = (lander.vx + thruster.vx) * friction#
lander.vy = (lander.vy + thruster.vy) * friction#


Now just apply this velocity to your lander's position.

lander.x = lander.x + lander.vx
lander.y = lander.y + lander.vy



You didn't specify what language you're using, so here's a demo in AppGameKit tier 1.

Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 21st Nov 2019 10:53
Hey Phaelax, I used your example to make a DBP version using an image for the sprite and it works well. I did notice that the OP is using a 3D object though. I haven't tried using this with an object to see how it works, but plan to when I can get the time.



So many games to code.....so little time.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 23rd Nov 2019 23:39
Principle is the same, if he's only moving in two directions then nothing changes, except maybe swap Y for Z. If moving in all 3 directions, the other axis is calculated the same way the only that's different is determining the direction vector.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds

Login to post a reply

Server time is: 2024-11-17 22:32:27
Your offset time is: 2024-11-17 22:32:27