Game Development Community

How do you orbit rotate around a object?

by Leroy Frederick · in Torque Game Engine · 05/15/2007 (6:03 am) · 6 replies

Hiya all,

Just wondered if anyone (sure someone does) knows how to make an object rotate around another, like say the classic solar system example.

Ideally I would like to orbit around a different object at different times dynamically i.e. Suppose I have a object (Player Object) and there are 3 spheres (Sphere A, B and C). 'Player Object' orbits around 'Sphere A', then after reaching a 180 degree turn, orbits around 'Sphere C', then perhaps orbits back around 'Sphere A' then perhaps 'Sphere B' etc, etc...

Thanks in advance! :0)

#1
05/15/2007 (7:42 am)
You are probably going to want to make heavy use of mSin and mCos, but most of this should be pretty doable from script...However, since my purchase of AFX I perform almost everything similar to this concept using AFX's constraints and system. My Torque life has gotten much easier thanks to their systems.
#2
05/15/2007 (7:53 am)
Thanks for pointing me in the right direction Michael, AFX is that cool effects system right? I wasn't aware it had any TGE rotation/angle system enhancements.

mSin and mCos, huh? My maths is pretty basic in certain areas, so I'll give those a try, but any script examples would be more then welcomed too! :)
#3
05/29/2007 (11:48 am)
I'm still a bit stuck with this, any more ideas or a code snippet would be more then welcomed! :0)
#4
05/29/2007 (2:42 pm)
Leroy,

I'm sure there's a better way to do this, but I'm just learning.... so with that disclaimer out of the way, here's a function I wrote a while back. Hope this helps!

// Turn and move the shape by %dist amount along one axis by angle %angle
function TurnShape(%shape, %dist, %angle)
{
	// Get current transform values
	%xfrm = %shape.getTransform();

	// Location values
	%lx = getword(%xfrm,0);
	%ly = getword(%xfrm,1);
	%lz = getword(%xfrm,2);

	// Rotation vector values
	%rx = 0;
	%ry = 0;

	%angle += 1.0;
	if(%angle >= 360)
	{
		// Object has completed a 360 degree rotation.
		// Reset to zero degrees
		%angle = 0;
	}

	if(%angle >= 180)
	{
		%rz = 2.0;
	}
	else
	{
		%rz = 1.0;
	}

	%rd = (%angle / 180) * 3.14;

	%shape.setTransform(%lx SPC %ly SPC %lz SPC %rx SPC %ry SPC %rz SPC %rd);

	schedule(500, 0, TurnShape, %shape, %dist, %angle);
}
#5
05/29/2007 (2:48 pm)
For clarification: the above code snippet can be used to make an object rotate along its Z axis. Invoke the TurnShape function 1 time and then it'll fire off a schedule that'll keep the object rotating indefinitely.

The above example doesn't handle the orbital calculation you want to do.
#6
05/30/2007 (6:47 am)
Thanks for the code snippet Dave! :0) I'll give it a try...