Trying to figure out gravity.
by Keith Murphy · in Torque Game Builder · 07/17/2008 (6:10 am) · 9 replies
I posted this in another section but didn't get any responses, so I'm going to delete that thread and post it here in hopes that I can better understand this.
#2
//---------------------------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
// startGame
// All game logic should be set up here. This will be called by the level builder when you
// select "Run Game" or by the startup process of your game to load the first level.
//---------------------------------------------------------------------------------------------
function startGame(%level)
{
exec("./playerClass.cs");
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
new ActionMap(moveMap);
moveMap.push();
$enableDirectInput = true;
activateDirectInput();
enableJoystick();
sceneWindow2D.loadLevel(%level);
}
//---------------------------------------------------------------------------------------------
// endGame
// Game cleanup should be done here.
//---------------------------------------------------------------------------------------------
function endGame()
{
sceneWindow2D.endLevel();
moveMap.pop();
moveMap.delete();
}
07/17/2008 (6:10 am)
Here's my game.cs//---------------------------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
// startGame
// All game logic should be set up here. This will be called by the level builder when you
// select "Run Game" or by the startup process of your game to load the first level.
//---------------------------------------------------------------------------------------------
function startGame(%level)
{
exec("./playerClass.cs");
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
new ActionMap(moveMap);
moveMap.push();
$enableDirectInput = true;
activateDirectInput();
enableJoystick();
sceneWindow2D.loadLevel(%level);
}
//---------------------------------------------------------------------------------------------
// endGame
// Game cleanup should be done here.
//---------------------------------------------------------------------------------------------
function endGame()
{
sceneWindow2D.endLevel();
moveMap.pop();
moveMap.delete();
}
#3
function playerClass::onLevelLoaded(%this, %scenegraph)
{
$player = %this;
moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");
moveMap.bindCmd(keyboard, "space", "playerJump();", "");
%this.enableUpdateCallback();
}
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(-150);
$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);
}
return;
}
if (%this.moveRight)
{
if (!%this.againstRightWall)
{
%this.againstLeftWall = false;
%this.setLinearVelocityX(30);
}
return;
}
if (!%this.moveLeft && !%this.moveRight)
{
%this.setLinearVelocityX(0);
}
}
function playerClass::updateVertical(%this)
{
%yVelocity = %this.getLinearVelocityY();
%this.setLinearVelocityY(10);
%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);
}
}
Can anyone help me understand what's going on?
07/17/2008 (6:10 am)
Here's my playerClass.csfunction playerClass::onLevelLoaded(%this, %scenegraph)
{
$player = %this;
moveMap.bindCmd(keyboard, "left", "playerLeft();", "playerLeftStop();");
moveMap.bindCmd(keyboard, "right", "playerRight();", "playerRightStop();");
moveMap.bindCmd(keyboard, "space", "playerJump();", "");
%this.enableUpdateCallback();
}
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(-150);
$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);
}
return;
}
if (%this.moveRight)
{
if (!%this.againstRightWall)
{
%this.againstLeftWall = false;
%this.setLinearVelocityX(30);
}
return;
}
if (!%this.moveLeft && !%this.moveRight)
{
%this.setLinearVelocityX(0);
}
}
function playerClass::updateVertical(%this)
{
%yVelocity = %this.getLinearVelocityY();
%this.setLinearVelocityY(10);
%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);
}
}
Can anyone help me understand what's going on?
#4
just a word of meta-advice,
the GG community is pretty good and there's lots of folks more than willing to help figure out problems,
but if you can simplify the problem down to some much shorter example, folks will probably be more likely to take a look.
reducing a problem to its simplest possible form is a great programming skill in any case -
not only does it make for better forum posts, but it helps you understand the problem and possibly fix it yourself.
07/17/2008 (8:00 am)
Hey kieth -just a word of meta-advice,
the GG community is pretty good and there's lots of folks more than willing to help figure out problems,
but if you can simplify the problem down to some much shorter example, folks will probably be more likely to take a look.
reducing a problem to its simplest possible form is a great programming skill in any case -
not only does it make for better forum posts, but it helps you understand the problem and possibly fix it yourself.
#5
07/17/2008 (8:24 am)
I can understand the above looking ridiculously long and cumbersome to look at. I was honestly trying to put as much data on here because I was guessing that if I gave a description of my problem then people would start asking questions to pull data out of me. I guess I was hoping to give people what they needed up front to see where I went wrong.
#7
07/18/2008 (6:20 am)
I'm at work so I don't know exactly, but I bought it a few days ago so I'd assume the latest?
#8
07/18/2008 (6:46 am)
Can you maybe e-mail me the project so I can test it?
#9
07/18/2008 (6:49 am)
I'll do that when I get home, thanks Theo!
Torque Owner Keith Murphy
1. Take a block and drag it to the bottom of the screen.
2. Take an animated sprite of the ninja, place him high over the block.
3. Editing the Ninja Sprite:
Name: player
Class: playerClass
Send Collision:Checked
Receive Collision: Not Checked
Send Physics: Checked
Receive Physics: Not Checked
Collision Layers: All
Collision Groups: All
Editing the Block Sprite:
Send Collision:Not Checked
Receive Collision: Checked
Send Physics: Not Checked
Receive Physics: Not Checked
Collision Layers: All
Collision Groups: All
Immovable: Checked
Now what I found is, depending on what I put for the following values, I got very different results.
Constant Force Y: 4.000
Gravtic: Not Checked
Force Scale: 0.000
The ninja hangs and doesn't drop at all.
Constant Force Y: 1.000
Gravtic: Not Checked
Force Scale: 0.000
The ninja hangs and doesn't drop at all.
Constant Force Y: 4.000
Gravtic: Checked
Force Scale: 0.000
The ninja hangs and doesn't drop at all.
Constant Force Y: 4.000
Gravtic: Checked
Force Scale: 1.000
The ninja drops, hits the block and stops there, but is stuck in the "dropping down" animation.
He does move left or right, but again only shows the dropping down animation when moving.
Jump does not work..
Constant Force Y: 4.000
Gravtic: Not Checked
Force Scale: 1.000
The ninja drops, hits the block and stops there, but is stuck in the "dropping down" animation.
He does move left or right, but again only shows the dropping down animation when moving.
Jump does not work.
Constant Force Y: 0.000
Gravtic: Checked
Force Scale: 3.000
The ninja drops.
He does animate correctly when moving back and forth.
He does jump, now more normally.
He lands, but is now stuck in the jumping down animation.
He still moves left or right, but can no longer jump.
Constant Force Y: 0.000
Gravtic: Not Checked
Force Scale: 3.000
The ninja drops.
He does animate correctly when moving back and forth.
He does jump, just very high.
He lands, and it seems to work just fine.
Constant Force Y: 0.000
Gravtic: Checked
Force Scale: 2.000
The ninja drops, hits the block and stops there, but is stuck in the "dropping down" animation.
He does move left or right, but again only shows the dropping down animation when moving.
Jump does not work.
Constant Force Y: 0.000
Gravtic: Checked
Force Scale: 4.000
The ninja drops.
He does animate correctly when moving back and forth.
He does jump, now more normally.
He lands, but is caught toggling rapidly between the standing and dropping down animation.
Jump is unreliable, only working 50% of the time. (Assumingly when I hit it while he is standing)
When I jump again, landing causes it to toggle again.
Constant Force Y: 0.000
Gravtic: Checked
Force Scale: 5.000
The ninja drops and immediately goes into his rapid toggle dance as above.
Jump is unreliable, only working 50% of the time. (Assumingly when I hit it while he is standing)
When I jump again, he lands, and suddenly it seems to work just fine.
He does animate correctly when moving back and forth.
He does jump, just very high.
Jumping again proves that it is now working after the first jump.
Constant Force Y: 0.000
Gravtic: Checked
Force Scale: 5.000
The ninja drops and immediately goes into his rapid toggle dance as above.
Jump is unreliable, only working 50% of the time. (Assumingly when I hit it while he is standing)
When I jump again, he lands, and suddenly it seems to work just fine.
He does animate correctly when moving back and forth.
He does jump, just very high.
Jumping again proves that it is now working after the first jump.
Constant Force Y: 0.000
Gravtic: Checked
Force Scale: 6.000
The ninja drops.
He does animate correctly when moving back and forth.
He does jump, now more normally.
He lands, but is caught toggling rapidly between the standing and dropping down animation.
Jump is unreliable, only working 50% of the time. (Assumingly when I hit it while he is standing)
When I jump again, landing causes it to toggle again.
Constant Force Y: 0.000
Gravtic: Checked
Force Scale: 7.000
The ninja drops.
He does animate correctly when moving back and forth.
He does jump, now more normally.
He lands, but is caught toggling rapidly between the standing and dropping down animation.
Jump is unreliable, only working 50% of the time. (Assumingly when I hit it while he is standing)
When I jump again, he lands, and suddenly it seems to work just fine.
When I jump a 2nd time, he goes into his rapid dance again.