Game Development Community

How to make an object orbit another

by Eugene · in Torque Game Builder · 08/04/2009 (7:09 pm) · 8 replies

I would like to know if there is a way in TGB to make one object continuously orbit another.
I'd think this is a common animation in games, but I can't find any code examples for it.

I guess a quick and dirty way to do it is by making an image with an object placed off-center,
and then changing its rotation value.

However, I would like to explore Torque Script, and I would like to know what is a more proper way to do this in the Torque script language.

Thank you for any help.

#1
08/04/2009 (7:29 pm)
Could you define it more clearly?

1. Are you looking for something with physics for gravity towards a certain object?
2. Are you looking for something on an aligned path to orbit the object?
3. Something to follow the object and orbit if it gets near?

2 is the easiest. Make a path that will go into a circle, and mount it onto the center object, then mount the orbiting object onto the path. Set the parameters onto the path and it will follow it.
#2
08/04/2009 (8:25 pm)
2: I'm new to Torque and not yet familiar with setting paths. Is there a tutorial or documentation you can point me to?

I want the object to orbit just like the moon going around the earth, while the earth can be moved by a mouse. I want to be able to change the distance between the moon and earth, and also the speed of the moon.

Thank you for your help.
#3
08/06/2009 (9:27 am)
one EASY way of doing it is by mounting a scene object in the player, and then you mount the object you want to "orbit" in that scene object... just make sure to move the mounting point to a desirable position that is NOT the center.

after that, all you have to do is make the scene object spin...

if you want to change the radius and the speed of the orbit, jsut modify the scene object size and spin ratio.
#4
08/06/2009 (9:30 am)
doing it with paths is more effective (you doit basically as i explained it with the scene object) when it comes with the speed handling, and maybe with the radio changing... but there are a few gotchas with the paths that unless you know them, you're better off avoiding them.
#5
08/06/2009 (12:16 pm)
Mounting off-center and spinning is a great idea; thank you.

So far, I've tried setting angular velocity (w/ moving forward option) and setting the linear velocities. It also makes the object go around in circular orbit.

To make it follow a mouse while orbiting, I used setConstantForce(...) or setImpulseForce(...) to nudge towards mouse position. That works, except it kinda goes off in to an ugly swing when the mouse is moved suddenly.

I'll try that mounting trick to avoid getting that swing problem. Thanks.

I thought I had to use mcos() or msin() or something. I'd appreciate any other ideas to explore. Thank you in advance.
#6
08/06/2009 (2:41 pm)
using math calculations could help, but can be expensive in the long run (havin to update that function a few times per second is a no no...)... the only 2 ideas i can come up with right now, that are effective, and once set, you can forget about them (unless you wanna change its parameters), are the ones exposed.
#7
08/06/2009 (8:37 pm)
Mounting off-center works great and was simple too. Orbits around a moving mouse cursor beautifully and no weird swings. Thank you.

#8
08/06/2009 (9:03 pm)
not a problem man...