Need help rotating an object around an arbitray point.
by \"Robert Jaramillo\" · in Torque Game Builder · 02/18/2010 (2:59 am) · 5 replies
Hello Everyone,
When I try to rotate a object around an arbitrary point using either one of these methods I found on the forms the image just sits there and hops back and forth. I could really use any help that I could get figuring out why this is happening. I am setting it to rotate at about 3 degrees every second as time passes. Please any insight is helpful. below are the 2 formulas being used by me. I only ran one method at a time incase you were wondering.
function Harpoon::RotateAroundPlayer(%this, %point, %angle)
{
///////////This was method 1//////
//shift x and y relative to 0,0 origin
%offsetX = (%this.getPositionX() + %point.X* -1);
%offsetY = (%this.getPositionY() + %point.Y* -1);
%rotationRadians = mDegToRad(%angle);
//get distance from origin to source point
%r = t2dVectorDistance(%this.getPosition(), %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 + %point.X);
%rotateY = (%rotateY + %point.Y);
//return new point
%this.setPosition(%rotateX SPC %rotateY);
////////This was method 2///////////
////Get the amount to rotate our object
//%rotation = mDegToRad(%angle); //- %this.getRotation());
////Get the point to rotate our object around
%reffPivotPoint = %this.getWorldPoint(%point);
//Calculate the offset between the point we are rotating about and this object
%reffOffset = VectorSub(%Point, %this.getPosition());
//get the length of our offsets vector
%reffDistance = VectorLen(%reffOffset);
//Get our rotation after our offset has been accounted for
%reffRotationOffset = mAtan(%reffOffset.X, %reffOffset.Y) - %rotation;
//Get the new position for our object
%newPosition = %reffDistance * mSin(%reffRotationOffset) SPC %newPosition = %reffDistance *
mCos(%reffRotationOffset);
//Add our objects piviot point to the new position
%newPosition = VectorAdd(%newPosition, %point);
//Set our objects new position
%this.setPosition(%newPosition.X, %newPosition.Y);
//Set our objects rotation
%this.setRotation(%angle);
}
When I try to rotate a object around an arbitrary point using either one of these methods I found on the forms the image just sits there and hops back and forth. I could really use any help that I could get figuring out why this is happening. I am setting it to rotate at about 3 degrees every second as time passes. Please any insight is helpful. below are the 2 formulas being used by me. I only ran one method at a time incase you were wondering.
function Harpoon::RotateAroundPlayer(%this, %point, %angle)
{
///////////This was method 1//////
//shift x and y relative to 0,0 origin
%offsetX = (%this.getPositionX() + %point.X* -1);
%offsetY = (%this.getPositionY() + %point.Y* -1);
%rotationRadians = mDegToRad(%angle);
//get distance from origin to source point
%r = t2dVectorDistance(%this.getPosition(), %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 + %point.X);
%rotateY = (%rotateY + %point.Y);
//return new point
%this.setPosition(%rotateX SPC %rotateY);
////////This was method 2///////////
////Get the amount to rotate our object
//%rotation = mDegToRad(%angle); //- %this.getRotation());
////Get the point to rotate our object around
%reffPivotPoint = %this.getWorldPoint(%point);
//Calculate the offset between the point we are rotating about and this object
%reffOffset = VectorSub(%Point, %this.getPosition());
//get the length of our offsets vector
%reffDistance = VectorLen(%reffOffset);
//Get our rotation after our offset has been accounted for
%reffRotationOffset = mAtan(%reffOffset.X, %reffOffset.Y) - %rotation;
//Get the new position for our object
%newPosition = %reffDistance * mSin(%reffRotationOffset) SPC %newPosition = %reffDistance *
mCos(%reffRotationOffset);
//Add our objects piviot point to the new position
%newPosition = VectorAdd(%newPosition, %point);
//Set our objects new position
%this.setPosition(%newPosition.X, %newPosition.Y);
//Set our objects rotation
%this.setRotation(%angle);
}
#2
02/18/2010 (3:45 am)
No I haven't thanks dude. One question about it though if you dont mind. Anyways when the radius is being used in ther right that is because of the fact that it is a moon orbiting a planet? If so I would be able to just use the center point of my player because I am not rotating around a planet?
#3
02/18/2010 (3:49 am)
Also I just tried it. It dose indeed move the image around in a circle about a point in my image. yay I see improvement> However, the image is still incredibly jittery and doesn't actually rotate the image at all. do you have any suggestions.
#4
02/18/2010 (4:09 am)
Actually after going through that solution some more I still have the same problem. the object that I am trying to rotate is still rotating by its center. What I am trying to do is to get my object to rotate by a point on the player. but that is one step closer I guess. I appreciate all the help dude. you got anymore suggestions. Theyt have been really helpful.
#5
02/18/2010 (10:46 am)
it sounds like you're trying to rotate on a different "axis". This is difficult to achieve and short of editing the source, the easiest way I've found is to mount it to another object and rotate said object.
Associate William Lee Sims
Machine Code Games