Game Development Community

Broken player movement

by Robert Brown · in Torque 2D Beginner · 03/09/2014 (5:58 pm) · 4 replies

I have been working on this all day and Ive found a few bugs. But the only thing that works still is rotation I can not get the player to move in any direction. Here is the script.

if (!isObject(PlayerMovementBehavior))
{

%template = new BehaviorTemplate (PlayerMovementBehavior);
%template.friendlyName = "Player Movement";
%template.behaviorType = "Input";
%template.description = "Moves the player for a top down shooter";

//keybind is a type for input
%template.addBehaviorField(upKey, "Key to bind to Upward Movement",keybind,"keyboard up");
%template.addBehaviorField(downKey, "Key to bind to Downward Movement", keybind,"keyboard down");
%template.addBehaviorField(leftKey, "Key to bind to rotate Left Movement",keybind, "keyboard left");
%template.addBehaviorField(rightKey, "Key to bind to rotate Right Movement",keybind, "keyboard right");

//float is a type for numbers with decimals
%template.addBehaviorField(turnSpeed,"Velocity of turning (degrees per second)",float, 120.0);

%template.addBehaviorField(damping,"Amount to damp movement (percentage of velocity per second)",float, 1.0);
%template.addBehaviorField(acceleration,"Acceleration of your Ship",float, 5.0);

//bool is a type for true and false
%template.addBehaviorField(joystick, "Are you using a joystick?", bool, false);
%template.addBehaviorField(anologStickX, "Anolog stick of your joystick (X axis)", keybind, "keyboard right");
%template.addBehaviorField(anologStickY, "Anolog stick of your joystick (Y axis)", keybind, "keyboard right");

}



function PlayerMovementBehavior::onBehaviorAdd(%this)
{
if (!isObject(moveMap))
return;

%this.owner.enableUpdateCallback();
//damping is a resistive force that slows the player to a stop
%this.owner.setDamping(%this.damping);

//Sets up the keybinds
if(!%this.joystick)
{
moveMap.bindObj(getWord(%this.upKey,0),getWord(%this.upKey, 1),"moveUp",%this);
moveMap.bindObj(getWord(%this.downKey,0),getWord(%this.downKey, 1),"moveDown",%this);
moveMap.bindObj(getWord(%this.leftKey,0),getWord(%this.leftKey, 1),"moveLeft",%this);
moveMap.bindObj(getWord(%this.rightKey,0),getWord(%this.rightKey, 1),"moveRight",%this);
}
else
{
moveMap.bindObj(getWord(%this.anologStickX,0),getWord(%this.anologStickX,1),"moveX",%this);
moveMap.bindObj(getWord(%this.anologStickY,0),getWord(%this.anologStickY, 1),"moveY",%this);
}

//sets the default value incase no button has been pressed
%this.controls Y= 0;
%this.controlsX = 0;
}

function PlayerMovementBehavior::onBehaviorRemove(%this)
{

if (!isObject(moveMap)) return;

//disables updateCallback
%this.owner.disableUpdateCallback();


//disables keybinds
moveMap.unbindObj(getWord(%this.upKey, 0),getWord(%this.upKey, 1), %this);
moveMap.unbindObj(getWord(%this.downKey, 0),getWord(%this.downKey, 1), %this);
moveMap.unbindObj(getWord(%this.leftKey, 0),getWord(%this.leftKey, 1), %this);
moveMap.unbindObj(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), %this);
}

function PlayerMovementBehavior::moveY(%this, %val)
{
//adds a cutoff for joysticks
if(%val < 0.15 && %val > -0.15)
{
%val = 0;
}
//the y axis is inverted for joysticks
%this.controlsY =-1*%val;
}

function PlayerMovementBehavior::moveX(%this, %val)
{
//adds a cutoff for joysticks
if(%val < 0.15 && %val >-0.15)
{
%val = 0;
}
%this.controlsX = %val;
}
function PlayerMovementBehavior::moveUp(%this, %val)
{
%this.controlsY = %val;
}
function PlayerMovementBehavior::moveDown(%this,%val)
{
%this.controlsY = -1*%val;
}
function PlayerMovementBehavior::moveLeft(%this, %val)
{
%this.controlsX = -1*%val;
}
function PlayerMovementBehavior::moveRight(%this, %val)
{
%this.controlsX = %val;
}




function PlayerMovementBehavior::OnUpdate(%this)
{
//Finds angle to the position (controlsX, controlsY)
%angle = mRadtoDeg(maTan(%this.controlsX, %this.controlsY));
//if the player has input to move, else stop moving
if(%this.controlsX != 0 || %this.controlsY != 0)
{
%this.owner.rotateTo(%angle,%this.turnspeed);
//Impulse is a onetime force in the direction %angle with force of acceleration

%this.owner.setImpulseForcePolar(%angle,%this.acceleration);
}
}



Any ideas where I messed up?

#1
03/09/2014 (10:09 pm)
I think there may have been changes to SceneObject - setImpulseForcePolar() no longer exists. Use setLinearVelocityPolar() instead:
function PlayerMovementBehavior::OnUpdate(%this) 
{ 
    //Finds angle to the position (controlsX, controlsY)
    %angle = mRadtoDeg(maTan(%this.controlsX, %this.controlsY));
    //if the player has input to move, else stop moving 
    if(%this.controlsX != 0 || %this.controlsY != 0) 
    {
        %this.owner.rotateTo(%angle,%this.turnspeed); 
        //Impulse is a onetime force in the direction %angle with force of acceleration 

        %this.owner.setLinearVelocityPolar(%angle,%this.acceleration); 
    } 
}

Hope that works! I didn't test it....
#2
03/10/2014 (1:07 am)
It looks like your script is from an older version of Torque Game Builder/Torque 2D. As Richard mentioned, the names of certain SceneObject methods have changed in the open source version.

You should get an error message in the console when using an invalid function name. Check the console.log file or see the GUI tutorial on the Github wiki for building an in-game console if you are not using the Sandbox.

In addition to Richard's fix - enable/disableUpdateCallback don't exist, it is now setUpdateCallback(true/false). Also on the Github wiki in the TorqueScript section is a link to a script class reference which lists all of the valid methods available to use.
#3
03/10/2014 (5:26 am)
Thanks guys I am actually using the last release before it went open source I have a preference for the editor heh. I will try those adjustments and see if it helps anyway.
#4
03/10/2014 (12:00 pm)
If you're using the commercial engine, then the changes Richard and I suggested won't work. This forum is for the open source version of T2D, so unless you state otherwise, we assume that is what you are using.

Not that we don't mind helping users of the legacy engine though - all I can suggest is to place echo() statements in your functions. Is onUpdate being called, have the console spit out the values of %this.controlsX and %this.controlsY, etc. It can sometimes be tedious, but I find it's the best way to understand what the engine is doing with my scripts when things aren't working as expected.