Jumping on moving platforms.
by AzraelK · in Torque Game Builder · 09/20/2006 (3:47 pm) · 10 replies
I implemented a simple moving platform for the miniplatform demo. by scheduling an object into moving up and down (setLinearVelocityY(-10)) while sending and receiving collisions (but no physics, so it can lift up our character)
It worked really well, except for a detail, when the platform goes down the player does the jumpdown animation. Also Im not able to jump while Im on the platform just walk
Any ideas on how to fix this?
here's my jump code
And heres my updateMovement Code
It worked really well, except for a detail, when the platform goes down the player does the jumpdown animation. Also Im not able to jump while Im on the platform just walk
Any ideas on how to fix this?
here's my jump code
function playerJump()
{
%yVelocity = $pGuy.getLinearVelocityY();
%xVelocity = $pGuy.getLinearVelocityX();
$pGuy.setLinearVelocityY(100);
$pGuy.setLinearVelocityX(0);
// Inset the collision poly's left/right sides a little bit before doing castCollision, so
// that castCollision truly is checking for something BELOW us.
$pGuy.setCollisionPolyCustom( 4, "-0.80 -0.358", "0.60 -0.358", "0.60 0.975", "-0.80 0.975" );
%collision = $pGuy.castCollision(0.005);
// Restore original values after castCollision
$pGuy.setCollisionPolyCustom( 4, "-0.910 -0.358", "0.678 -0.358", "0.678 0.975", "-0.910 0.975" );
if(!(%collision $= ""))
{
echo ("floor found!");
$pGuy.setLinearVelocityY(-255);
}
else
{
echo ("floor NOT found!");
$pGuy.setLinearVelocityY(-255/*%yVelocity*/);
}
$pGuy.setLinearVelocityX(%xVelocity);
}And heres my updateMovement Code
function playerClass::updateMovement(%this)
{
if(%this.moveLeft)
{
%this.setLinearVelocityX(-60);
//%this.setConstantForceX(-60);
}
if(%this.moveRight)
{
%this.setLinearVelocityX(60);
//%this.setConstantForceX(60);
}
if(!%this.moveLeft && !%this.moveRight)
{
%this.setLinearVelocityX(0);
//%this.setAtRest();
//%this.stopConstantForce();
//%this.setConstantForceX(0);
}
%yVelocity = $pGuy.getLinearVelocityY();
$pGuy.setLinearVelocityY(100);
$pGuy.setCurrentAnimation(%yVelocity);
// Inset the collision poly's left/right sides a little bit before doing castCollision, so
// that castCollision truly is checking for something BELOW us.
$pGuy.setCollisionPolyCustom( 4, "-0.80 -0.358", "0.60 -0.358", "0.60 0.975", "-0.80 0.975" );
%collision = $pGuy.castCollision(0.005);
// Restore original values after castCollision
$pGuy.setCollisionPolyCustom( 4, "-0.910 -0.358", "0.678 -0.358", "0.678 0.975", "-0.910 0.975" ); $pGuy.setLinearVelocityY(%yVelocity );
if(%collision $= "")
{
$pGuy.setConstantForceY(100);
}
else
{
$pGuy.setConstantForceY(0);
}
}
#2
It has improved but the character still wont jump from a moving platform, it seems like its stuck on the platform somehow.
09/22/2006 (3:32 pm)
Thanks That was a great idea, unfortunately It didnt worked that well. Ive added and Ive added a $onFloor flag to know if he is touching the floor or not. It has improved but the character still wont jump from a moving platform, it seems like its stuck on the platform somehow.
function checkFloor(){
%yVelocity = $pGuy.getLinearVelocityY();
%xVelocity = $pGuy.getLinearVelocityX();
$pGuy.setLinearVelocityY(100);
$pGuy.setLinearVelocityX(0);
// Inset the collision poly's left/right sides a little bit before doing castCollision, so
// that castCollision truly is checking for something BELOW us.
$pGuy.setCollisionPolyCustom( 4, "-0.80 -0.358", "0.60 -0.358", "0.60 0.975", "-0.80 0.975" );
%collision = $pGuy.castCollision(0.0005);
// Restore original values after castCollision
$pGuy.setCollisionPolyCustom( 4, "-0.910 -0.358", "0.678 -0.358", "0.678 0.975", "-0.910 0.975" );
if(!(%collision $= ""))
{
$onFloor=true;
}
else
{
$onFloor=false;
}
$pGuy.setLinearVelocityX(%xVelocity);
$pGuy.setLinearVelocityY(%yVelocity);
return $onFloor;
}
function playerJump()
{
$onFloor=checkFloor();
if ($onFloor){
echo("Jump!");
$pGuy.setLinearVelocityY(-255);
}
}
function playerClass::setCurrentAnimation(%this, %yVelocity)
{
if (!$onFloor)
{
if(%yVelocity < 0 )
{
%this.playAnimation(playerJumpUp);
}
else if(%yVelocity > 0 )
{
%this.playAnimation(playerJumpDown);
}
}
else
{
if(%this.moveLeft || %this.moveRight)
{
if(%this.getAnimationName() $= "playerRun")
{
if(%this.getIsAnimationFinished())
{
%this.playAnimation(playerRun);
}
}
else
{
%this.playAnimation(playerRun);
}
}
else
{
%this.playAnimation(playerStand);
}
}
}
function playerClass::updateMovement(%this)
{
if(%this.moveLeft)
{
%this.setLinearVelocityX(-60);
//%this.setConstantForceX(-60);
}
if(%this.moveRight)
{
%this.setLinearVelocityX(60);
//%this.setConstantForceX(60);
}
if(!%this.moveLeft && !%this.moveRight)
{
%this.setLinearVelocityX(0);
//%this.setAtRest();
//%this.stopConstantForce();
//%this.setConstantForceX(0);
}
%yVelocity = $pGuy.getLinearVelocityY();
//$pGuy.setLinearVelocityY(100);
$pGuy.setCurrentAnimation(%yVelocity);
$onFloor=checkFloor();
if ($onFloor)
{
$pGuy.setConstantForceY(0);
}
else
{
$pGuy.setConstantForceY(100);
}
}
#3
-Have you checked that your ground check is returning true when on the moving platform?
-This might sound like an odd question, but what version of TGB are you using?
-Also, is jumping from normal (non-moving) platforms working?
This probably won't solve your problem, just something I noticed: your ground check is returning true based on any collision reported by cast collision. It seems that if you were in the air and hit a ceiling or even an enemy it would return true.
09/24/2006 (1:02 pm)
3 questions:-Have you checked that your ground check is returning true when on the moving platform?
-This might sound like an odd question, but what version of TGB are you using?
-Also, is jumping from normal (non-moving) platforms working?
This probably won't solve your problem, just something I noticed: your ground check is returning true based on any collision reported by cast collision. It seems that if you were in the air and hit a ceiling or even an enemy it would return true.
#4
Have you checked that your ground check is returning true when on the moving platform?
-Yes
-This might sound like an odd question, but what version of TGB are you using?
-Version 1.1.1
-Also, is jumping from normal (non-moving) platforms working?
Good question, I did checked that, and theres no problem to jump if theres no movement on them. once I apply setLinearVelocity() I cant jump
This probably won't solve your problem, just something I noticed: your ground check is returning true based on any collision reported by cast collision. It seems that if you were in the air and hit a ceiling or even an enemy it would return true.
actually no, I did had that problem earlier, but by using a method I found here in the forums, I set a linear velocity down then modify my collision box so is a little bigger beneath the character and then I cast the collision and return everything the way it was. So im pretty sure checkFloor is checking the floor beneath the character, not the ceiling or walls. (although I could be wrong)
09/25/2006 (8:21 am)
Thanks for answering my thread let me try to answer those.Have you checked that your ground check is returning true when on the moving platform?
-Yes
-This might sound like an odd question, but what version of TGB are you using?
-Version 1.1.1
-Also, is jumping from normal (non-moving) platforms working?
Good question, I did checked that, and theres no problem to jump if theres no movement on them. once I apply setLinearVelocity() I cant jump
This probably won't solve your problem, just something I noticed: your ground check is returning true based on any collision reported by cast collision. It seems that if you were in the air and hit a ceiling or even an enemy it would return true.
actually no, I did had that problem earlier, but by using a method I found here in the forums, I set a linear velocity down then modify my collision box so is a little bigger beneath the character and then I cast the collision and return everything the way it was. So im pretty sure checkFloor is checking the floor beneath the character, not the ceiling or walls. (although I could be wrong)
#5
And as for the ground check, I was just mentioning that because I noticed you arent bringing the top of the collision poly down for castCollision. I didn't realize that you are actually setting the linear velocity Y to 100 before the cast, so ceilings should never be an issue, but you aren't checking if what you collide with is actually 'floor' so you might get a true colliding with something else (an enemy or a powerup or something) even if there is no physics response.
09/25/2006 (12:35 pm)
For the jumping, just for kicks, try this and let me know what happens: $pGuy.setPositionY( $pGuy.getPositionY() - 0.05); $pGuy.setLinearVelocityY(-255);
And as for the ground check, I was just mentioning that because I noticed you arent bringing the top of the collision poly down for castCollision. I didn't realize that you are actually setting the linear velocity Y to 100 before the cast, so ceilings should never be an issue, but you aren't checking if what you collide with is actually 'floor' so you might get a true colliding with something else (an enemy or a powerup or something) even if there is no physics response.
#6
Dont worry I think Ive solved this problem in design. I will use moving platforms going sideways. ;)
Do you know how can I get what type of platform Im stepping on using castcollision?
09/25/2006 (3:44 pm)
Thanks! that did worked. But Im still getting problems to jump when the platform is going down, but works like a charm when the platform is going up. :)Dont worry I think Ive solved this problem in design. I will use moving platforms going sideways. ;)
Do you know how can I get what type of platform Im stepping on using castcollision?
#7
[soapbox]
Completely unrelated to this thread, but I wanted to emphasize the point Azrael just made--and this is not intended to be Torque specific, but is true for any engine, any toolset, any project, any deadline (except for no deadline at all):
It's critically important in game development to not only set realistic design requirements early on, but be willing to adjust them (or drop them altogether) along the way. Sometimes the tech won't support a requirement, sometimes it's developer skills, sometimes it's artwork, sometimes it's project timeline---sometimes, it's just a bad design requirement that turns out to be "not fun". A good game developer will be willing to sometimes "let go" of design requirements, or adjust them in simple, or even complex, ways, to keep the game fun, and keep progress moving.
[/soapbox]
PS: In this particular circumstance, it's probably a design requirement that can be met--probably a small corner case that's causing an issue when a platform is moving down. However, in general the soapbox speech above I think is a good one!
09/25/2006 (4:12 pm)
Quote:
Dont worry I think Ive solved this problem in design. I will use moving platforms going sideways. ;)
[soapbox]
Completely unrelated to this thread, but I wanted to emphasize the point Azrael just made--and this is not intended to be Torque specific, but is true for any engine, any toolset, any project, any deadline (except for no deadline at all):
It's critically important in game development to not only set realistic design requirements early on, but be willing to adjust them (or drop them altogether) along the way. Sometimes the tech won't support a requirement, sometimes it's developer skills, sometimes it's artwork, sometimes it's project timeline---sometimes, it's just a bad design requirement that turns out to be "not fun". A good game developer will be willing to sometimes "let go" of design requirements, or adjust them in simple, or even complex, ways, to keep the game fun, and keep progress moving.
[/soapbox]
PS: In this particular circumstance, it's probably a design requirement that can be met--probably a small corner case that's causing an issue when a platform is moving down. However, in general the soapbox speech above I think is a good one!
#8
To determine whether or not an object is a platform you need the platforms to know that they are platforms. You can either set some specific info about each type of platform on the platform itself, or assume it based on the class name of the platform. I prefer the first method, but either way works. You can assign that data via datablocks to make it nice and easy to build your levels.
Assuming all your platforms have a field called 'type', and that solid platforms have a type value of 1 and moving platforms have a type value of 2, you could do the following to find out what type of platforms you are in contact with:
Edit: SP
09/25/2006 (7:07 pm)
CastCollision returns a list of properties about the collision that would have occurred in the time you specify. You would probably want to use CastCollisionList instead (same syntax: %object.castCollisionList(%time);) which returns a list of objects, rather than just the first object collided with. To determine whether or not an object is a platform you need the platforms to know that they are platforms. You can either set some specific info about each type of platform on the platform itself, or assume it based on the class name of the platform. I prefer the first method, but either way works. You can assign that data via datablocks to make it nice and easy to build your levels.
Assuming all your platforms have a field called 'type', and that solid platforms have a type value of 1 and moving platforms have a type value of 2, you could do the following to find out what type of platforms you are in contact with:
%objList = %this.castCollisionList( 0.005 );
%count = getWordCount( %objList );
for( %i = 0; %i < %count; %i++ )
{
%obj = getWord( %objList, %i );
switch( %obj.type )
{
case 1:
// in contact with a solid platform...
case 2:
// in contact with a moving platform...
}
}Edit: SP
#9
All I had to do then was to try to follow the platform movement. with this code:
Where obj is the moving platform and offset is the distance between the platform center and the player center!
Works like a charm now!
09/26/2006 (1:40 pm)
Thomas: Thanks! that did worked better than expected! It didnt solved the platform problem but It did helped me to know when Im specifically on top of a moving platform. All I had to do then was to try to follow the platform movement. with this code:
case 2: $pGuy.setPositionY( %obj.getPositionY() - %offset); %onfloor=true;
Where obj is the moving platform and offset is the distance between the platform center and the player center!
Works like a charm now!
#10
moving vehicles!
all you have to do is to modify your character X and Y position. until you jump out of the vehicle.
If you want to be able to move the vehicle, all you have to do is to modify the X and Y position of the "platform" instead of the platform moving your character.
It does work! ;)
09/26/2006 (1:49 pm)
By the way, this code can also be used (although modified) to make the character stand on moving vehicles!
all you have to do is to modify your character X and Y position. until you jump out of the vehicle.
If you want to be able to move the vehicle, all you have to do is to modify the X and Y position of the "platform" instead of the platform moving your character.
It does work! ;)
Torque Owner Thomas Buscaglia