Game Development Community

Gravity Question

by Christopher Figueroa · in Torque Game Builder · 11/06/2009 (7:01 pm) · 5 replies

I am trying to make my character flip so his feet are facing the ceiling and then reverse gravity so now gravity is coming from the other direction so he goes to the ceiling and walks along it.

In the game he will be running down a tunnel and I will be keybinding "e" so that when I press it. Flip's on his Y axis and gravity becomes a negative number. What is the code line for changing gravity or how do I go about doing this?

#1
11/06/2009 (7:39 pm)
You already set your force in the game (probably using setConstantForce). I would think you'd just need to change that force on the keybind.
#2
11/06/2009 (7:50 pm)
Well I have gravity set to 4.00 for my character so when in the keybind I would just use setConstantForce = -4.00?
#3
11/07/2009 (2:44 am)
I've not tried it, but, yeah, that's what I'd do.
#4
11/09/2009 (10:18 am)
So I tried that and it doesn't work. Here is the code i'm using and I don't know what is wrong so if you could figure this out that would be awesome.




function playerClass::onLevelLoaded(%this, %scenegraph)
{
     $player = %this;
      
      moveMap.bindCmd(keyboard, "a", "playerLeft();", "playerLeftStop();");
      moveMap.bindCmd(keyboard, "d", "playerRight();", "playerRightStop();");
      moveMap.bindCmd(keyboard, "space", "playerJump();", "");
      moveMap.bindCmd(keyboard, "e", "playerflip();", "");

      %this.enableUpdateCallback();
}
function playerflip()
{
 $player.setFlip(false, true);
 $player.setConstantForceY(-4);
}

function playerLeft()
{
    $player.moveLeft = true;
}

function playerLeftStop()
{
    $player.moveLeft = false;
}

function playerRight()
{
     $player.moveRight = true;
}

function playerRightStop()
{
     $player.moveRight = false;
}

function playerJump()
{
     if(!$player.airborne)
     {
          $player.setLinearVelocityY(-125);
          $player.airborne = true;
     }
}
function playerClass::updateHorizontal(%this)
{
   if (%this.moveLeft == %this.moveRight)
   {
      %this.setLinearVelocityX(0);
      return;
   }
   
   if (%this.moveLeft)
   {
      if (!%this.againstLeftWall)
      {
         %this.againstRightWall = false;
         %this.setLinearVelocityX(-30);
      }
   }

   if (%this.moveRight)
   {
      if (!%this.againstRightWall)
      {
         %this.againstLeftWall = false;
         %this.setLinearVelocityX(30);
      }
   }
}
function playerClass::updateVertical(%this)
{
   %yVelocity = %this.getLinearVelocityY();
   
   %this.setLinearVelocityY(5);
   %collision = %this.castCollision(0.005);
   
   %normalX = getWord(%collision, 4);
   %normalY = getWord(%collision, 5);
   
   // no collision
   if (%collision $= "")
   {
      %this.airborne = true;
      %this.setConstantForceY(100);
      %this.setLinearVelocityY(%yVelocity);
      return;
   }
   
   // collides with wall to the left
   if (%normalX == 1 && %normalY == 0)
   {
      %this.againstLeftWall = true;
      %this.setLinearVelocityX(0);
      %this.setLinearVelocityY(%yVelocity);
      return;
   }
   
   // collides with wall to the right
   if (%normalX == -1 && %normalY == 0)
   {
      %this.againstRightWall = true;
      %this.setLinearVelocityX(0);
      %this.setLinearVelocityY(%yVelocity);
      return;
   }
   
   // on ground with no wall collisions
   if (%normalX == 0 && %normalY == -1)
   {
      %this.airborne = false;
      %this.againstLeftWall = false;
      %this.againstRightWall = false;
      %this.setConstantForceY(0);
      %this.setLinearVelocityY(%yVelocity);
      return;
   }
   
   // in air and hits platform with head
   if (%normalY == 1)
   {
      %this.airborne = true;
      %this.setLinearVelocityX(0);
      %this.setConstantForceY(100);
      %this.setLinearVelocityY(%yVelocity);
      return;
   }
   
   // in case another type of collision normal was detected
   error("another collison type" SPC %normalX SPC %normalY);
   %this.airborne = false;
   %this.againstLeftWall = false;
   %this.againstRightWall = false;
   %this.setLinearVelocityY(%yVelocity);
}
function playerClass::onUpdate(%this)
{
   %this.updateHorizontal();
   %this.updateVertical();
   %this.setCurrentAnimation();
}
function playerClass::setCurrentAnimation(%this)
{
   %xVelocity = %this.getLinearVelocityX();
   %yVelocity = %this.getLinearVelocityY();

   if (%xVelocity > 0)
   {
      %this.setFlip(false, false);
   }
   else if (%xVelocity < 0)
   {
      %this.setFlip(true, false);
   }
   
   if (%this.airborne)
   {
      if(%yVelocity < 0)
      {
         %this.playAnimation(PlayerJumpUpAnimation);
         return;
      }else
      {
         %this.playAnimation(PlayerJumpDownAnimation);
         return;
      }
   }

   if ($player.moveLeft == true || $player.moveRight == true)
   {
      if (%this.getAnimationName() $= "playerRunAnimation")
      {
         if(%this.getIsAnimationFinished())
         {
            %this.playAnimation(PlayerRunAnimation);
            return;
         }
      }else
      {
         %this.playAnimation(PlayerRunAnimation);
      }
   }else
   {
      %this.playAnimation(PlayerStandAnimation);
   }
}
#5
11/09/2009 (1:37 pm)
I can't guarantee that I found everything, but here's a start:

Line 15. $player.setConstantForce( "0 -4", true );

You're going to have to keep a flag around (say, $flipped) that starts at false, and is toggled when the "e" key is pressed.

Then you'll need to modify almost all of this code to take advantange of the $flipped flag. For example, "playerFlip" will need to change the force back to "0 4". Also, none of the jump code will work without making changes relating to the $flipped flag.