Is it posible to restrict player's movement?
by Vinicius Gouveia · in Torque Game Builder · 07/17/2009 (12:50 pm) · 33 replies
Hello. I'm trying to do an arcade-esque game, and I want to restrict the movement from 8 angles (Up, Left, Down, Right and their diagonals) to only 4 angles (Up, Left, Down, Right), but I really don't know how to do it.
If someone could just shine a light for me, I would be grateful!
If someone could just shine a light for me, I would be grateful!
About the author
#2
07/17/2009 (1:10 pm)
What input devices are you planning on supporting?
#3
Gonna double-check the examples and reply then.
@Patrick: Probably just keyboard and, if everything goes right, a cellphone keypad.
07/17/2009 (1:11 pm)
I'm quite sure I did, but I think I didn't notice any bits of code that could restrict the movement of my character.Gonna double-check the examples and reply then.
@Patrick: Probably just keyboard and, if everything goes right, a cellphone keypad.
#4
07/17/2009 (1:14 pm)
the code in there should only allow your player to go left, right, jump or duck
#5
07/17/2009 (1:16 pm)
Well, mine won't need to jump or duck 'cause I'm planning to do some controls that resamble Bomberman. But I think that, as everything else on programming, I can try to modify that to my own needs.
#6
Place something like the following into gameScriptsgame.cs under moveMap.push(); where PlayerName is the name you gave the player either in script or in the editor - this will depend on how you spawn it.
I hope this gets you started. I'd be straight shocked if it works out of the box untested, but I think it should get you in the right direction.
Best,
Patrick
EDIT: I just thought about this and I don't think you even need to have those nested if statements...
07/17/2009 (1:32 pm)
Warning - rusty tgb scripter spewing untested code...Place something like the following into gameScriptsgame.cs under moveMap.push(); where PlayerName is the name you gave the player either in script or in the editor - this will depend on how you spawn it.
moveMap.bindCmd(keyboard, "left", "PlayerName.moveLeft();", "PlayerName.stop();"); moveMap.bindCmd(keyboard, "right", "PlayerName.moveRight();", "PlayerName.stop();"); moveMap.bindCmd(keyboard, "up", "PlayerName.moveUp();", "PlayerName.stop();"); moveMap.bindCmd(keyboard, "down", "PlayerName.moveDown();", "PlayerName.stop();");now create another script file called input.cs and make sure you execute it from main.cs using:
exec("./gameScripts/input.cs");Use something like this in input.cs$moveFactor = 10; //speed to move player
function PlayerClass::moveLeft(%this)
{
$moveLeft = true;
if($moveUp == true) //also holding up
{
//so let's move him left and up
%this.setPositionX(%this.getPositionX() - $moveFactor);
%this.setPositionY(%this.getPositionY() - $moveFactor);
}
else if($moveDown == true) //also holding down
{
//so let's move him left and down
%this.setPositionX(%this.getPositionX() - $moveFactor);
%this.setPositionY(%this.getPositionY() + $moveFactor);
}
else //not holding up or down
{
%this.setPositionX(%this.getPositionX() - $moveFactor);
}
}
function PlayerClass::moveRight(%this)
{
//see moveLeft and modify this one accordingly
}
function PlayerClass::moveUp(%this)
{
//see moveLeft and modify this one accordingly
}
function PlayerClass::moveDown(%this)
{
//see moveLeft and modify this one accordingly
}
function PlayerClass::stop(%this)
{
$moveLeft = false;
$moveRight = false;
$moveUp = false;
$moveDown = false;
%this.setPosition(%this.getPosition());
}I hope this gets you started. I'd be straight shocked if it works out of the box untested, but I think it should get you in the right direction.
Best,
Patrick
EDIT: I just thought about this and I don't think you even need to have those nested if statements...
#7
07/17/2009 (1:39 pm)
thats how i'd do it, just check to see if you are moving in another direction then cancel that before you move the way you want to go. this way a user cant go up and left at the same time.
#8
basically, as soon as the move function its invoked, it will stop the player, and proceed to move it to the desired direction... no questions asked :) ... this is a quick and dirty move, but it sure works.
07/17/2009 (2:27 pm)
this is how i'd do it:function Player::move(%this, %dir)
{
%this.setAtRest(); //stops the player at once
switch$(%dir)
{
case "up":
%this.setLinearVelocityY(-$pSpeed);
case "down":
%this.setLinearVelocityY($pSpeed);
case "left":
%this.setLinearVelocityX(-$pSpeed);
case "right":
%this.setLinearVelocityX($pSpeed);
}
}basically, as soon as the move function its invoked, it will stop the player, and proceed to move it to the desired direction... no questions asked :) ... this is a quick and dirty move, but it sure works.
#9
07/17/2009 (3:14 pm)
That's a nice solution, much more elegant.
#10
Notice that I have the "updateMovement" funcion, and it calculates the horizontal and vertical speeds, based on a parameter that's passed by the developer on TGB, since it's a function:
I don't know much about programming in Torque/C++, and I lack programming logic, but I tried to do this (I assume that "||" means "or"):
But still, failure...
Probably I'll use your examples as a basis for developing my own code (I think it's a better way to learn), but I don't know if I can get that one working...
07/20/2009 (7:03 am)
Tried both examples, but they rendered my character unmovable. Probably I just inserted that bit of code in the wrong place, but whatever...Notice that I have the "updateMovement" funcion, and it calculates the horizontal and vertical speeds, based on a parameter that's passed by the developer on TGB, since it's a function:
function TUX::updateMovement(%this)
{
%this.owner.setLinearVelocityX((%this.right - %this.left) * %this.horizontalSpeed);
%this.owner.setLinearVelocityY((%this.down - %this.up) * %this.verticalSpeed);
}I don't know much about programming in Torque/C++, and I lack programming logic, but I tried to do this (I assume that "||" means "or"):
function TUX::updateMovement(%this)
{
if (%this.up != 0 || %this.down != 0){
%this.sethorizontalSpeed = 0;
}
if (%this.left != 0 || %this.right != 0){
%this.setverticalSpeed = 0;
}
%this.owner.setLinearVelocityX((%this.right - %this.left) * %this.horizontalSpeed);
%this.owner.setLinearVelocityY((%this.down - %this.up) * %this.verticalSpeed);
}But still, failure...
Probably I'll use your examples as a basis for developing my own code (I think it's a better way to learn), but I don't know if I can get that one working...
#11
07/20/2009 (8:14 am)
Try selecting your player in TGB, go to the scripting section and make sure that your Class is equal to "TUX".
#12
07/20/2009 (8:42 am)
Yes, the class is "TUX", the name and super class are in blank.
#13
07/20/2009 (9:02 am)
Put an echo statement in on of those functions and let's see if they're being called. If so, then it's an issue with the code inside there.function TUX::updateMovement(%this)
{
echo("updateMovement called on TUX.");
if (%this.up != 0 || %this.down != 0){
%this.sethorizontalSpeed = 0;
}
if (%this.left != 0 || %this.right != 0){
%this.setverticalSpeed = 0;
}
%this.owner.setLinearVelocityX((%this.right - %this.left) * %this.horizontalSpeed);
%this.owner.setLinearVelocityY((%this.down - %this.up) * %this.verticalSpeed);
}
#14
Nothing happened. I can be unskilled with C++, but I know from PHP that echo prints something on the screen, and nothing was printed. I inserted that statement right where you showed in the example.
Gonna try to echo stuff from the other functions, will be back in a moment.
EDIT: OK, it didn't echo anything on the movement functions either, and I can guarantee you that they're working.
07/20/2009 (9:06 am)
:ONothing happened. I can be unskilled with C++, but I know from PHP that echo prints something on the screen, and nothing was printed. I inserted that statement right where you showed in the example.
Gonna try to echo stuff from the other functions, will be back in a moment.
EDIT: OK, it didn't echo anything on the movement functions either, and I can guarantee you that they're working.
#15
07/20/2009 (10:00 am)
It would echo them in your console. hit the ` key to bring it up...
#16
07/20/2009 (10:41 am)
Yeah, it echo'd everything on the console. The updateMovement function is called right after the move<direction> function.
#17
Add the following into gameScripts\game.cs under moveMap.push();
Patrick
07/20/2009 (12:53 pm)
Give this a shot..Add the following into gameScripts\game.cs under moveMap.push();
moveMap.bindCmd(keyboard, "left", "TUX.move(left);", ""); moveMap.bindCmd(keyboard, "right", "TUX.move(right);", ""); moveMap.bindCmd(keyboard, "up", "TUX.move(up);", ""); moveMap.bindCmd(keyboard, "down", "TUX.move(down);", "");Now create another script file called input.cs and make sure you execute it from main.cs using:
exec("./gameScripts/input.cs");Add the following to input.cs and save it in your gameScripts directory.function TUX::move(%this, %dir)
{
%this.setAtRest(); //stops the player at once
switch$(%dir)
{
case "up":
%this.setLinearVelocityY(-$pSpeed);
case "down":
%this.setLinearVelocityY($pSpeed);
case "left":
%this.setLinearVelocityX(-$pSpeed);
case "right":
%this.setLinearVelocityX($pSpeed);
}
}Let us know how it works out for you as I can't test this... sorry.Patrick
#18
I think I'll take a break for today... My head is full of numbers and digits and all of that. It isn't easy to work with MySQL and study Torque at the same time, I tell ya.
07/20/2009 (1:29 pm)
Nope, it rendered my character unmovable :(I think I'll take a break for today... My head is full of numbers and digits and all of that. It isn't easy to work with MySQL and study Torque at the same time, I tell ya.
#19
07/20/2009 (1:39 pm)
I know what you mean... Best of luck to ya!
#20
the guide that patrick gave you is the right one... i'll give you an example of how i use that code in a game i made long ago.
i put all that code in a single file named "player.cs", and then created a level with an object named "player_obj" (put that in the name field, and hit the ENTER key... if you dont, it wont get saved!), and save the level.
with that, it SHOULD work. the problem here is that probably you didnt set a value for "$pShip", and then when you tried to move, it wouldnt because speed was zero!...
try that, and you'll see. im telling you, it works, cuz i've just posted a snippet of something i did myself.
07/20/2009 (1:54 pm)
hello... since you're a begginer, you shoulndt start with tryin to create behaviors... instead, start with the regular path (i mean, creating classes and all that).the guide that patrick gave you is the right one... i'll give you an example of how i use that code in a game i made long ago.
function Player::onLevelLoaded(%this, %scenegraph)
{
$pSpeed=20; //this is the default speed of the object...
%this = player_obj; //player_obj is the name of the object created in the editor that we'll control in the game.
moveMap.bindCmd(keyboard, "up", "%this.move(up);", "return;");
moveMap.bindCmd(keyboard, "down", "%this.move(down);", "return;");
moveMap.bindCmd(keyboard, "left", "%this.move(left);", "return;");
moveMap.bindCmd(keyboard, "right", "%this.move(right);", "return;");
}
function Player::move(%this, %dir)
{
%this.setAtRest(); //stops the player at once
switch$(%dir)
{
case "up":
%this.setLinearVelocityY(-$pSpeed);
case "down":
%this.setLinearVelocityY($pSpeed);
case "left":
%this.setLinearVelocityX(-$pSpeed);
case "right":
%this.setLinearVelocityX($pSpeed);
}
}i put all that code in a single file named "player.cs", and then created a level with an object named "player_obj" (put that in the name field, and hit the ENTER key... if you dont, it wont get saved!), and save the level.
with that, it SHOULD work. the problem here is that probably you didnt set a value for "$pShip", and then when you tried to move, it wouldnt because speed was zero!...
try that, and you'll see. im telling you, it works, cuz i've just posted a snippet of something i did myself.
Torque Owner Timothy Castagna
TK Games LLC
tdn.garagegames.com/wiki/TGB/Tutorials/Platformer/Player