Getting the direction angle
by Bruno · in Torque Game Builder · 10/22/2005 (3:52 am) · 4 replies
Hi.,
I'm tryng to have a ship image, allways facing the direction my sprite is moving, but so far, i have been unsucessefull.,
This is what i'm doing :
Basically, the sprite is moving as it should, but the angle, not working at all..,
Any ideias on how can i make the sprite facing the direction that he his moving to ?
thanks,
Bruno
I'm tryng to have a ship image, allways facing the direction my sprite is moving, but so far, i have been unsucessefull.,
This is what i'm doing :
function Angle(%x1, %y1, %x2, %y2)
{
%dx = %x2 - %x1;
%dy = %y2 - %y1;
%vLength = mSqrt(%dx * %dx + %dy * %dy);
if (%vLength == 0) return 0;
%ux = %dx / %vLength;
%uy = %dy / %vLength;
return (mAtan(%ux, %uy) * 180 / 3.141592654) - 90;
}
function Update_movement(%object)
{
%oldposx = %object.getPositionX();
%oldposy = %object.getPositionY();
%nposx = %oldposX + %deltaX;
%nposy = %oldposY + %deltaY;
%object.setPositionY( "%nposx %nposy" );
%newposx = %object.getPositionX();
%newposy = %object.getPositionY();
%obj_angle = Angle(%oldposx, %oldposy, %newposx, %newposy);
%object.setRotation(%obj_angle);
schedule(1,0,"Update_movement",%object);
}Basically, the sprite is moving as it should, but the angle, not working at all..,
Any ideias on how can i make the sprite facing the direction that he his moving to ?
thanks,
Bruno
About the author
#2
That works for me, mebbe someone else has a better way
EDIT @Hans Sjunnesson Hmmm didn't know bout those, I just got T2D, gonna check that out
EDIT2: Ok, updated code, ty Hans
10/22/2005 (5:56 am)
function Update_movement(%object) {
// Make sure we're moving !
if(%object.getLinearVelocityX() != 0 || %object.getLinearVelocityY() != 0) {
%pos = %object.getPosition();
%newPos = VectorAdd(%pos, vectorScale2D(%object.getLinearVelocity(), 1.5));
//%newPos = VectorAdd(%pos, %deltaX SPC %deltaY);
//%object.setPosition( %newPos );
%angle = angleBetween2D(%pos, %newPos);
%object.setRotation(%angle);
}
schedule(100, 0, "Update_movement", %object);
}That works for me, mebbe someone else has a better way
EDIT @Hans Sjunnesson Hmmm didn't know bout those, I just got T2D, gonna check that out
EDIT2: Ok, updated code, ty Hans
#3
I'm not sure why you have your velocity in %deltaX and %deltaY. Instead these should be in set in the object via setLinearVelocity, then you don't need to add the delta movement to the position manually as you have in your code. Basically you have 2 lines of code:
10/22/2005 (6:11 am)
Yeah, this is the kind of thing that is REALLY easy to do in T2D.I'm not sure why you have your velocity in %deltaX and %deltaY. Instead these should be in set in the object via setLinearVelocity, then you don't need to add the delta movement to the position manually as you have in your code. Basically you have 2 lines of code:
// call this whenever you want to change the velocity
%object.setLinearVelocity("%velX %velY") ;
// call this very often (say once every frame)
%object.setRotation( getWord( %object.getLinearVelocityPolar(), 0 ) ) ;
#4
The thing is, the sprite is making a sinusoidal movement path, pretty much the same way i saw a guy posting in a forum here..,
So, i was tryng to calculate the direction angle, using the previous position and te current position, but somehow it's not working, so, going to the more "cleaner" root..,
Has anyone sucessefully made a sinusoidal movement, using setvelocity ? I don't see a way to keep changing velocitys to get a wave movement, the only way i see is my changing the sprite position directly, but doing this, obviously the velocity of the entity is zero, and getLinearVelocity cannot be applied in this case...
10/22/2005 (8:18 am)
Thanks guys, unfortenly the method of getLinearVelocity won't work, because i'm not moving the sprite using velocity, i'm positioning it by hand (setPosition), so the velocity is allways zero.The thing is, the sprite is making a sinusoidal movement path, pretty much the same way i saw a guy posting in a forum here..,
So, i was tryng to calculate the direction angle, using the previous position and te current position, but somehow it's not working, so, going to the more "cleaner" root..,
Has anyone sucessefully made a sinusoidal movement, using setvelocity ? I don't see a way to keep changing velocitys to get a wave movement, the only way i see is my changing the sprite position directly, but doing this, obviously the velocity of the entity is zero, and getLinearVelocity cannot be applied in this case...
Torque Owner Hans Sjunnesson
On the other hand, you could use something like fxSceneObject2D::getLinearVelocityPolar() to extract the direction of the sprite is moving.
--
Hans