Collision on an Angle
by Jeremy Dull · in Torque Game Builder · 03/26/2012 (10:11 pm) · 0 replies
I am working with iTorque2D and having some issues with the collision system and trying to update the animation and physics of an object. I have put this together from some of the code I have found in tutorials and such, but what I am trying to do is this. I have a simple AI creature that spawns and moves forward playing a walk animation. I need the animation to change based on when the creature falls or gets launched in the air. I am working on the falling side. I am using the castCollision like I saw in the tutorial so it knows when it is airborne or not. The problem I am running into is that when the creature hits an angled surface then it changes from airborne to not airborne rapidly which then makes the animation just freeze as it is changing back and forth quickly between the too. It makes my creature look like it is on an escalator. Any suggestions or help that you guys can give would be appreciated.
//Here is my code that I currently and using for the update. In the characters datablock the walkspeed is set to 50, gravityFactor is 100, and the force scale is 4. My collision box on the creature is in a crystal type shape ending at a point on the top and bottom of the creature.
function unitClass::updateMovement(%this)
{
%this.setLinearVelocityX(%this.walkSpeed);
%yVelocity = %this.getLinearVelocityY();
//%xVelocity = %this.getLinearVelocityX();
%this.setLinearVelocityY(5);
%collision = %this.castCollision(1.0);
%this.setLinearVelocityY(%yVelocity);
echo("Collision Data: ", %collision);
%normalX = getWord(%collision, 4);
%normalY = getWord(%collision, 5);
//no collision
if (%collision $= "")
{
%this.airborne = true;
echo("AIRBORNE");
%this.setConstantForceY(%this.gravityFactor);
//%this.setLinearVelocityX(%this.walkSpeed/5);
return;
}
//on ground with no wall collisions
if ((%normalX > -1 && %normalX < 1) && (%normalY >= -1 && %normalY < 0))
{
echo("I am on the Ground");
%this.airborne = false;
%this.setLinearVelocityX(%this.walkSpeed);
%this.setConstantForceY(0);
return;
}
}
Thanks for any assistance.
//Here is my code that I currently and using for the update. In the characters datablock the walkspeed is set to 50, gravityFactor is 100, and the force scale is 4. My collision box on the creature is in a crystal type shape ending at a point on the top and bottom of the creature.
function unitClass::updateMovement(%this)
{
%this.setLinearVelocityX(%this.walkSpeed);
%yVelocity = %this.getLinearVelocityY();
//%xVelocity = %this.getLinearVelocityX();
%this.setLinearVelocityY(5);
%collision = %this.castCollision(1.0);
%this.setLinearVelocityY(%yVelocity);
echo("Collision Data: ", %collision);
%normalX = getWord(%collision, 4);
%normalY = getWord(%collision, 5);
//no collision
if (%collision $= "")
{
%this.airborne = true;
echo("AIRBORNE");
%this.setConstantForceY(%this.gravityFactor);
//%this.setLinearVelocityX(%this.walkSpeed/5);
return;
}
//on ground with no wall collisions
if ((%normalX > -1 && %normalX < 1) && (%normalY >= -1 && %normalY < 0))
{
echo("I am on the Ground");
%this.airborne = false;
%this.setLinearVelocityX(%this.walkSpeed);
%this.setConstantForceY(0);
return;
}
}
Thanks for any assistance.
About the author
Founder and CEO of Blackstaff Studios. Our focus is to make fun and simple games and software for the everyday person. Currently working on a project for the iOS platform using iTorque 2D