A complicated question about Paths, rotation, and vectors
by Steven Turner · in Torque Game Builder · 02/05/2008 (6:27 pm) · 8 replies
Hello, I'm hoping this is the right place to post this.
I'm working on a system for player movement. Basically, player movement is controlled both by the arrow keys and the mouse position. When the player presses the up key, the player's avatar moves towards the mouse pointer. When pressing the down key, the avatar moves away from the mouse pointer. When pressing left or right, the avatar rotates around the mouse pointer in a circle. For an example of how this works, I have a flash program here:
http://personal.centenary.edu/~skturner/games/movetest1.swf
I'm just having a problem creating this kind of system in TGB. I've played with paths and with manipulating the world position values, but I don't know how to go about it.
Any notes or information is helpful. Also I can provide the actionscript code I wrote for Flash.
I'm working on a system for player movement. Basically, player movement is controlled both by the arrow keys and the mouse position. When the player presses the up key, the player's avatar moves towards the mouse pointer. When pressing the down key, the avatar moves away from the mouse pointer. When pressing left or right, the avatar rotates around the mouse pointer in a circle. For an example of how this works, I have a flash program here:
http://personal.centenary.edu/~skturner/games/movetest1.swf
I'm just having a problem creating this kind of system in TGB. I've played with paths and with manipulating the world position values, but I don't know how to go about it.
Any notes or information is helpful. Also I can provide the actionscript code I wrote for Flash.
#2
Steven - create a long thin t2dsceneobject which will be like an invisible stick between the player and the mouse pointer. Move the center of this 'stick' with the mouse pointer, and mount the player to one end of the stick. Now to move the player toward the pointer, you just have to scale down the stick, and rotating the stick will rotate the player around the mouse pointer. The hard part will be leaving the player in place when the mouse pointer is moved. You'll have to do a rescale and rotation of the stick on every frame that the mouse cursor moves to leave the player in place.
02/17/2008 (11:30 pm)
Phillip - that won't rotate the player around the mouse pointer, it just rotates around its own center to face the mouse pointer.Steven - create a long thin t2dsceneobject which will be like an invisible stick between the player and the mouse pointer. Move the center of this 'stick' with the mouse pointer, and mount the player to one end of the stick. Now to move the player toward the pointer, you just have to scale down the stick, and rotating the stick will rotate the player around the mouse pointer. The hard part will be leaving the player in place when the mouse pointer is moved. You'll have to do a rescale and rotation of the stick on every frame that the mouse cursor moves to leave the player in place.
#3
I'm new to TGB (and physics) and don't know if it is possible to direct a constant force to a point or object rather than just in a given direction. I've been looking in the reference guide for a while now and can't find a method for this kind of semi-real gravity.
One of my ideas (based on what you wrote, Conor) is to skip the force-thing and make the players base be mounted to a invisible circle-formed scene object. If I make the scene object the same size as the planet and at the same position, I don't have to move the character anymore; just rotate the scene-object and he will follow.
But this is a big walkaround and I just know I will need the real physics sooner or later.
It would help a lot if someone could help me here.
03/13/2008 (6:47 pm)
This discussion is very interesting, I'm trying to make the player character in my side-view game respond to gravity, but not the simple 'pull me to the bottom of the screen'- gravitic. I wan't the player to walk around a small planet (so the circle is a platform and the character walks on it's outer border, he will not get inside the circle or on top of it). I'm new to TGB (and physics) and don't know if it is possible to direct a constant force to a point or object rather than just in a given direction. I've been looking in the reference guide for a while now and can't find a method for this kind of semi-real gravity.
One of my ideas (based on what you wrote, Conor) is to skip the force-thing and make the players base be mounted to a invisible circle-formed scene object. If I make the scene object the same size as the planet and at the same position, I don't have to move the character anymore; just rotate the scene-object and he will follow.
But this is a big walkaround and I just know I will need the real physics sooner or later.
It would help a lot if someone could help me here.
#4
The angle might be messed up, but the above is the process that you should use.
Basically you find the relative position, normalise it and then apply the force to the player.
03/13/2008 (6:57 pm)
This will move the player towards an specified point:// The point that you would like to be forced towards %referenceOrigin = "0 0"; // Relative position %relativePosition = t2dVectorSub(%playerObj, %referenceOrigin); // Direction to that point %referenceNormal = t2dVectorNormalize(%relativePosition); // Set the contant force so that it will head towards that object %graviticForce = t2dVectorScale(%referenceNormal, %forceAmount); %playerObj.setConstantForce(%gravaticForce); // Angle %angle = mRadToDeg(mATan(-%referenceNormal.Y, %referenceNormal.X)); %playerObj.setRotation(%angle);
The angle might be messed up, but the above is the process that you should use.
Basically you find the relative position, normalise it and then apply the force to the player.
#5
Phillip, are you blazingly fast in your responses with code examples and all, or were you answering Steven's question?
By the way. Are there any plans on a mac build of your PSK? I'm very excited about the product.
03/13/2008 (7:21 pm)
This is just what I needed! Thanks!Phillip, are you blazingly fast in your responses with code examples and all, or were you answering Steven's question?
By the way. Are there any plans on a mac build of your PSK? I'm very excited about the product.
#6
function PlanetGravityBehavior::onBehaviorAdd(%this)
{
%this.owner.enableUpdateCallback();
}
function PlanetGravityBehavior::onUpdate(%this)
{
if (!isObject(%this.object))
return;
// Your code:
// The point that you would like to be forced towards
%referenceOrigin = "0 0";
// Relative position
%relativePosition = t2dVectorSub(%this.owner, %referenceOrigin);
// Direction to that point
%referenceNormal = t2dVectorNormalize(%relativePosition);
// Set the contant force so that it will head towards that object
%graviticForce = t2dVectorScale(%referenceNormal, %forceAmount);
%this.owner.setConstantForce(%gravaticForce);
// Angle
%angle = mRadToDeg(mATan(-%referenceNormal.Y, %referenceNormal.X));
%this.owner.setRotation(%angle);
}
Where do I specify the %forceAmount?
03/14/2008 (5:28 am)
Phillip, I couldn't get the code to work. I've put it in a behavior on the player character, like this:function PlanetGravityBehavior::onBehaviorAdd(%this)
{
%this.owner.enableUpdateCallback();
}
function PlanetGravityBehavior::onUpdate(%this)
{
if (!isObject(%this.object))
return;
// Your code:
// The point that you would like to be forced towards
%referenceOrigin = "0 0";
// Relative position
%relativePosition = t2dVectorSub(%this.owner, %referenceOrigin);
// Direction to that point
%referenceNormal = t2dVectorNormalize(%relativePosition);
// Set the contant force so that it will head towards that object
%graviticForce = t2dVectorScale(%referenceNormal, %forceAmount);
%this.owner.setConstantForce(%gravaticForce);
// Angle
%angle = mRadToDeg(mATan(-%referenceNormal.Y, %referenceNormal.X));
%this.owner.setRotation(%angle);
}
Where do I specify the %forceAmount?
#7
%forceAmount should just be the speed in which you want the player to move towards a point. If you threw something like 10 in there you'll see the object move slowly towards the point, throw 50 and it will be quick. Though you could probably use it as a behavior field.
03/14/2008 (1:19 pm)
Mac version is in QA, I don't have a Mac to test it on so it might take a few days.%forceAmount should just be the speed in which you want the player to move towards a point. If you threw something like 10 in there you'll see the object move slowly towards the point, throw 50 and it will be quick. Though you could probably use it as a behavior field.
#8
The player doesn't move a bit. Im thinking it could be the referensorigin that point's to the center of the playersprite instead of to the "0 0" position of the map because the code is in a behavior on the player?
Must the collision callback be checked on the character? I have 'receive physics' checked, is that wrong?
Sorry for the stupid questions, It's hard when you have next to none coding experience.
Thanks
03/14/2008 (5:40 pm)
Very strange.. The player doesn't move a bit. Im thinking it could be the referensorigin that point's to the center of the playersprite instead of to the "0 0" position of the map because the code is in a behavior on the player?
Must the collision callback be checked on the character? I have 'receive physics' checked, is that wrong?
Sorry for the stupid questions, It's hard when you have next to none coding experience.
Thanks
Associate Phillip O'Shea
Violent Tulip
function PlayerClass::updateMove(%this) { %mousePos = sceneWindow2d.getMousePosition(); %forwardVector = VectorSub(%this.getPosition(), %mousePos); %forwardVector = VectorNormalize(%forwardVector); // Positive speed for forward, negative for backwards %moveSpeed = 20; %moveVector = VectorScale(%forwardVector, %moveSpeed); %moveVector = getWords(%moveVector, 0, 1); %this.setLinearVelocity(%moveVector); // This updates the rotation %newAngle = -mRadToDeg(mATan(%forwardVector.X, %forwardVector.Y)); %this.setRotation(%newAngle); }Bob's your uncle ;)