Code-bits : PSK edge grip code
by Ian Smith · 06/12/2009 (1:55 am) · 3 comments
I was fiddling around, trying to come up with an edge-gripping system. I started on it, but some fudginess occured where the PC would jump into the wall before repositioning itself. I probably need to refine the actual logic and position readjustment code as well anyway.
The function that detects when a wall collision has occurred and decides whether or not to enter the Gripped physics state based on the criteria stated in the code below.
This function is not complete in any stretch; the code after "if (%this.Direction.X < 0 && %wallX > 0) // Left side" is not the same as the code after }else{ because I was trying to make it work in one direction first.
But it's here as a starting point because I know it's oft-requested in the forums. If there are any glaring issues with it feel free to drop me a line, as I myself am still learning this toolkit.
The function that detects when a wall collision has occurred and decides whether or not to enter the Gripped physics state based on the criteria stated in the code below.
This function is not complete in any stretch; the code after "if (%this.Direction.X < 0 && %wallX > 0) // Left side" is not the same as the code after }else{ because I was trying to make it work in one direction first.
But it's here as a starting point because I know it's oft-requested in the forums. If there are any glaring issues with it feel free to drop me a line, as I myself am still learning this toolkit.
function PlayerClass::hitWall(%this, %otherObject, %normal)
{
//if (!%otherObject.isMethod("getObjectType") || %otherObject.getObjectType() !$= "SolidPlatform") return;
//if (!%otherObject.isMember("Gripable") || %otherObject.Gripable == 0) return;
if (!%otherObject.Gripable) return;
// All gripable rect's must satisfy the following conditions:
// - The upper left corner MUST be the first point in the collision poly
// - The upper left and right edges must be 90-degree corners.
%wallX = getWord(%normal, 0);
%poly = %otherObject.getCollisionPoly();
// ran into a wall face-first, continue to check if the top edges of the player and
// the collding wall are within the grip threshold.
if (%this.Direction.X < 0 && %wallX > 0) // Left side
{
%wallLeftX = getWord(%poly, 0);
%wallLeftY = getWord(%poly, 1);
%wallRightY = getWord(%poly, 3);
%wallBottomLeftX = getWord(%poly, 6);
// Check to see if corner is a right-angle
if (%wallLeftX != %wallBottomLeftX) return; // Nope
if (%wallRightY != %wallLeftY) return; // and Nope
// Check to see if upper left corner lies within the grip threshold
// and if so, adjust the player position and set physics state accordingly.
%playerY = %this.getPositionY() - (%this.getWidth() / 2);
if (mAbs(%playerY - %wallLeftY) <= %this.gripThreshold)
{
// Succesfully met the grip conditions, now grip the wall
%this.setPhysicsState("Custom");
%this.setLinearVelocity(0,0);
echo("HANG ON! [right]");
}
}
else if (%this.Direction.X > 0 && %wallX < 0) // Right side
{
%wallRightX = getWord(%poly, 2);
%wallRightY = getWord(%poly, 3);
%wallLeftY = getWord(%poly, 1);
%wallBottomRightX = getWord(%poly, 4);
// Check to see if corner is a right-angle
if (%wallRightX != %wallBottomRightX) return;
if (%wallRightY != %wallLeftY) return;
// Convert upper right corner to world points
%point = %otherObject.getWorldPoint(%wallRightX, %wallRightY);
%gripY = mAbs(getWord(%point, 1));
// Check to see if upper right corner lies within the grip threshold
// and if so, adjust the player position and set physics state accordingly.
%playerY = mAbs(%this.getPositionY() - (%this.getWidth() / 2));
//%wallRightY = mAbs(%wallRightY);
if (mAbs(%playerY - %gripY) <= %this.gripThreshold)
{
// Succesfully met the grip conditions, now grip the wall
%newX = %wallRightX - (%this.getWidth() / 2);
%newY = %wallRightY - (%this.getHeight() / 2);
%this.setPosition(%newX, %newY);
%this.setPhysicsState("Gripped");
%this.setLinearVelocity(0,0);
echo("HANG ON! " @ %playerY SPC %wallRightY);
}
}
}About the author
#2
Will
06/12/2009 (1:03 pm)
Wow nice job, I would guess this could be used like you see in the DS Mario game? Sounds cool, I will be adding this to my game. Thanks man for posting. Will
#3
That's great to hear, just remember that the function does need to be finished and probably optimized ;)
06/13/2009 (12:20 am)
Quote:Wow nice job, I would guess this could be used like you see in the DS Mario game? Sounds cool, I will be adding this to my game. Thanks man for posting.
Will
That's great to hear, just remember that the function does need to be finished and probably optimized ;)
Associate Phillip O'Shea