Moving an object in a direction based on it's rotation
by James Peter Fayette · in Torque Game Builder · 11/29/2012 (12:46 pm) · 3 replies
I am a complete novice to Torque script, and the answer to my question is probably in the documentation, but after hours of searching I have failed to find it. I am programming a very basic shooter, and I am to a point where I can spawn bullets and make them point in the same direction as the gun, but I am at a loss as to how to make them move in the direction they are pointing.
in short, I need a way to move an object at a constant speed in the direction indicated by it's rotation value.
in short, I need a way to move an object at a constant speed in the direction indicated by it's rotation value.
About the author
#2
11/29/2012 (9:11 pm)
Thank's for the help.
#3
In summary, the code below takes two positions (vectors) and then sets the rotation of the object running the script (represented by %this) so that it faces the target (in this case, $pShip). The code below is intended to spawn the missile so it is rotated tip first to the player ship, then fire it at the player's position as part of the spawn function.
For reference, $pShip is a global variable representing the player. %this is the missile itself.
This is a snippet of script run by the missile in its spawn function.
// Rotate the missile to face the player (flavor)
%playerPosition = $pShip.getPosition();
%enemyPosition = %this.getPosition();
%vector = t2dVectorSub(%playerPosition, %enemyPosition);
%targetRotation = mRadToDeg(mAtan(%vector.y, %vector.x)) + 90;
%this.setRotation(%targetRotation);
// Set a constant relative velocity for the missile
%this.setForwardMovementOnly(true);
%this.setForwardSpeed(150);
01/07/2013 (9:15 pm)
I remember playing around with this when doing a top down shooter as a bit of a test project. I found a snippet of code on the forums to rotate one object to face another, and still use it quite often in my test projects.In summary, the code below takes two positions (vectors) and then sets the rotation of the object running the script (represented by %this) so that it faces the target (in this case, $pShip). The code below is intended to spawn the missile so it is rotated tip first to the player ship, then fire it at the player's position as part of the spawn function.
For reference, $pShip is a global variable representing the player. %this is the missile itself.
This is a snippet of script run by the missile in its spawn function.
// Rotate the missile to face the player (flavor)
%playerPosition = $pShip.getPosition();
%enemyPosition = %this.getPosition();
%vector = t2dVectorSub(%playerPosition, %enemyPosition);
%targetRotation = mRadToDeg(mAtan(%vector.y, %vector.x)) + 90;
%this.setRotation(%targetRotation);
// Set a constant relative velocity for the missile
%this.setForwardMovementOnly(true);
%this.setForwardSpeed(150);
Torque Owner Kevin James
Reference -> T2D Class Reference -> t2dSceneObject (very top of list) -> Ctrl-F "forward"
Glad you looked in documentation first, that's the habit to get into. It does help to have an idea of what you're looking for though.