Request: getLinearVelocityPolar()
by Jason McIntosh · in Torque Game Builder · 03/17/2005 (10:48 am) · 11 replies
This would be rather handy for setting rotations, etc. :) So, basically, I just want a getAngle() function.
Also, is there a way to force a sprite to automatically rotate to its velocity angle every frame so I don't have to do it by hand?
Also, is there a way to force a sprite to automatically rotate to its velocity angle every frame so I don't have to do it by hand?
About the author
#2
Not an angle to move to, but to have the sprite automatically face the direction it's traveling each frame. So if I change the angle of movement, the sprite will automatically face that way without a call to setRotation(). Well, it makes sense to me! :P But I've had 2 hours of sleep, so maybe it doesn't make sense.
03/17/2005 (10:59 am)
GetLinearVelocityPolar(), meaning return the current angle and speed components. Right? :)Not an angle to move to, but to have the sprite automatically face the direction it's traveling each frame. So if I change the angle of movement, the sprite will automatically face that way without a call to setRotation(). Well, it makes sense to me! :P But I've had 2 hours of sleep, so maybe it doesn't make sense.
#3
03/17/2005 (11:04 am)
You know, I think this is just a convenience function about the angle stuff. I can use mAtan() to get the angle, so please kindly disregard that silliness. :) I'm really really tired. I'll leave now so I don't begin to look even more foolish.
#4
getRotation() = Current Angle
getAngularVelocity() = Angular Speed in +/- degrees/sec
Can you not use these?
EDIT: I see now, you want the current direction in degrees! Yes, atan will do this. Already mentioned about a helper for this so the answer is yes. :)
- Melv.
03/17/2005 (12:41 pm)
Linear?getRotation() = Current Angle
getAngularVelocity() = Angular Speed in +/- degrees/sec
Can you not use these?
EDIT: I see now, you want the current direction in degrees! Yes, atan will do this. Already mentioned about a helper for this so the answer is yes. :)
- Melv.
#5
Here's my code. It doesn't give the correct angle for left/right world limit collisions (seems 180 degrees off or something) but it works fine for top/bottom limit collisions. The camera is set up as "standard" with 100x75 world units, centered on 0, 0.
I'm probably using the completely wrong math function. :) Any help appreciated.
(I'm still not asleep, though I should be.)
03/17/2005 (12:49 pm)
I have a spaceship bouncing off world limits, and I want the sprite to face the new angle when it reflects. I haven't done trig or vector math in ages, so I can't get it working. This is why I was asking for a convenience function something like getCurrentVelocityAngle() that simply returns the current velocity vector angle. Calculating it every time the ship bounces seems like extra work just to get the angle of the vector. Is there a better way that I don't know about?Here's my code. It doesn't give the correct angle for left/right world limit collisions (seems 180 degrees off or something) but it works fine for top/bottom limit collisions. The camera is set up as "standard" with 100x75 world units, centered on 0, 0.
%newAngle = mAtan( %this.getLinearVelocityX(), %this.getLinearVelocityY() ); %newAngle = mRadToDeg( %newAngle ); %this.setRotation( %newAngle );
I'm probably using the completely wrong math function. :) Any help appreciated.
(I'm still not asleep, though I should be.)
#6
I noticed some odd things (maybe it's not odd, though, I wouldn't really know). Here's some console output:
Thanks for any bones you guys can toss me. :)
03/17/2005 (1:00 pm)
Thanks for your time Melv, I know it's much better spent somewhere else than wrestling with my confusion. :) I do appreciate it.I noticed some odd things (maybe it's not odd, though, I wouldn't really know). Here's some console output:
rocket world limit - bottom dx = 15.2825 / dy = 47.4764 / angle = 17.8432 rocket world limit - top dx = 15.2825 / dy = -47.4764 / angle = 162.157 rocket world limit - right dx = 15.2825 / dy = 47.4764 / angle = 17.8432 rocket world limit - bottom dx = -15.2825 / dy = 47.4764 / angle = -17.8432 rocket world limit - top dx = -15.2825 / dy = -47.4764 / angle = -162.157 rocket world limit - bottom dx = -15.2825 / dy = 47.4764 / angle = -17.8432 rocket world limit - left dx = -15.2825 / dy = -47.4764 / angle = -162.157I noticed that the top/left and bottom/right sometimes report the same dx/dy values (that's getLinearVelocityX() and getLinearVelocityY() respectively). Is this normal? Does this have anything to do with why my left/right rotation angles aren't correct?
Thanks for any bones you guys can toss me. :)
#7
03/17/2005 (1:22 pm)
If it helps, I've been using this to accomplish roughly the same thing:function fxSceneObject2D::getLinearVelocityPolar( %this )
{
%velocity = %this.getLinearVelocity();
%speed = VectorLength2D(%velocity);
%cos = VectorDot2D( VectorScale2D( %velocity, 1 / %speed ), "1 0" );
%angle = 90 - mRadToDeg( mACos(%cos) );
// ArcCosine yields ambiguous results when dealing with angles > 180. Adjust.
if( getWord( %velocity, 1 ) > 0 )
%angle = 180 - %angle;
// Normalize the angle into [-180, 180] for no real reason.
if( %angle > 180 )
%angle = %angle - 360;
return %angle SPC %speed;
}
#8
03/18/2005 (4:58 am)
Hey thanks, Matt! I'll give it a shot. I didn't realize you can use the vector console commands on the return value of getLinearVelocity().
#9
I have added this function. It will be included in the next update.
In the meantime, you can add it yourself by adding the following code to "fxSceneObject2D.cc" (after the "setLinearVelocityPolar()" function)...
- Melv.
03/18/2005 (7:36 am)
FYII have added this function. It will be included in the next update.
In the meantime, you can add it yourself by adding the following code to "fxSceneObject2D.cc" (after the "setLinearVelocityPolar()" function)...
//-----------------------------------------------------------------------------
// Get Linear Velocity Polar.
//-----------------------------------------------------------------------------
ConsoleMethod(fxSceneObject2D, getLinearVelocityPolar, const char*, 2, 2, "Gets Objects Linear Velocity using Polar angle/speed.")
{
// Get Linear Velocity.
fxVector2D linearVelocity = object->getLinearVelocity();
// Create Returnable Buffer.
char* pBuffer = Con::getReturnBuffer(256);
// Format Buffer.
dSprintf(pBuffer, 256, "%f %f", mRadToDeg(mAtan(linearVelocity.mX, -linearVelocity.mY)), linearVelocity.len() );
// Return Velocity.
return pBuffer;
}Thanks for the report.- Melv.
#10
03/21/2005 (10:53 pm)
@Melv: When Josh Williams wrote in his technical overview blog that you were a monster, he wasn't kidding! A day job and all this. Where do you get the energy? :) Many thanks!
#11
I don't. My body is slowly withering away. Just need to get T2D done before I become too crispy.
- Melv.
03/22/2005 (3:42 am)
Quote:Where do you get the energy?
I don't. My body is slowly withering away. Just need to get T2D done before I become too crispy.
- Melv.
Associate Melv May
Sorry, but I don't really understand your second question. Are you talking about the ability to specify an angle to move to? If so, this has been requested half a dozen times at least so I'll give you the stock answer of yes, it'll come when I get around to it. ;)
- Melv.