Game Development Community

Rotate around arbitrary point

by Phillip O'Shea · in Torque Game Builder · 01/24/2008 (1:09 pm) · 8 replies

Here is a quick and easy script to get an object to rotate around an arbitrary point:

function t2dSceneObject::rotateAroundPoint(%this, %point, %angle)
{
	%rotation = mDegToRad(%angle - %this.getRotation());
	
	%reffPivotPoint = %this.getWorldPoint(%point);
	%reffOffset 	= VectorSub(%this.Position, %reffPivotPoint);
	%reffDistance	= VectorLen(%reffOffset);
	
	%reffRotationOffset = mAtan(%reffOffset.X, %reffOffset.Y) - %rotation;
	
	%newPosition = %reffDistance * mSin(%reffRotationOffset) SPC %reffDistance * mCos(%reffRotationOffset);
	%newPosition = VectorAdd(%newPosition, %reffPivotPoint);
	
	%this.setPosition(%newPosition.X, %newPosition.Y);
	%this.setRotation(%angle);
}

It will use an internal point, though it doesn't have to be between -1 and 1 so you can rotate the object around an external point.

About the author

Head of Violent Tulip, a small independent software development company working in Wollongong, Australia. Go to http://www.violent-tulip.com/ to see our latest offerings.


#1
01/25/2008 (12:44 am)
Thanks Phillip, this will become usefull soon.
#2
01/25/2008 (11:00 am)
As a note, this is a great thing to have, but using setPosition to move an object that has collisions can bypass some checks and produce crazy results. This would be fine for a GUI or effects, but might be dangerous to use for, say, a pinball paddle. In that case, it's probably better to just offset the texture.

Definitely useful though!
#3
01/25/2008 (12:17 pm)
I'm not sure that is would be too much of a problem for objects which will rotate by small amounts through a point inside of the collision poly. However, as Tom suggested, large amounts of rotation through external points will definitely cause physics problems.
#4
07/13/2008 (7:45 pm)
Awesome! This is exactly the thing I've been looking for. Thanks!
#5
07/14/2008 (9:51 pm)
Now you can add a rope swing to the PSK. :D
#6
07/15/2008 (5:50 am)
Tank, I believe it was probably possible to do that before by just mounting the rope to an object (check pivot points in the docs in regards to mounting etc, should be there)

I might actually code that (the swinging rope) up when I have a moment and stick it on the forums.
#7
07/15/2008 (12:57 pm)
>I might actually code that

That'd be great.
#8
08/07/2008 (1:20 pm)
I just came up with an alternative to this last night. This is one where, say, you know the x/y with respect to a grid, but now that grid is rotating... and you'd like the returned point to be correctly rotated. I use it to sync up sprites with a scroller's scroll [x,y] positions, while I rotate the scroller itself.

//http://geobabble.wordpress.com/2007/06/25/rotating-a-point-around-a-base-point/
//Taken and modified from code donated by Bill Dollins
function rotateAboutPoint(%basepoint, %sourcepoint, %rotationangle)
{
    //shift x and y relative to 0,0 origin  
    %offsetX = (getWord(%sourcepoint,0) + getWord(%basepoint,0) * -1);  
    %offsetY = (getWord(%sourcepoint,1) + getWord(%basepoint,1) * -1);
  
    %rotationRadians = mDegToRad(%rotationangle);  

    //get distance from origin to source point
    %r = t2dVectorDistance("0 0", %offsetX SPC %offsetY);

    //get current angle of orientation  
    %theta = mAtan(%offsetY, %offsetX);  

    // add rotation value to theta to get new angle of orientation  
    %offsetTheta = %theta + %rotationRadians;  

    //calculate new x coord  
    %rotateX = %r * mCos(%offsetTheta); 
 
    //calculate new y coord  
    %rotateY = %r * mSin(%offsetTheta);  

    //shift new x and y relative to base point  
    %rotateX = (%rotateX + getWord(%basepoint,0));
    %rotateY = (%rotateY + getWord(%basepoint,1));

    //return new point
    return %rotateX SPC %rotateY;  
}