I successfully implemented cloud platforms (one way platforms) in my game. Now I'd like to have the player fall if they are on top of such a platform and press down. The problem is I don't know how to disable a specific collision without a collision event. Here is some code:
As you see in the handlePreSolve I have the code in place to allow the player to pass through the cloud platform unless he's approaching it from above. In handleBeginContact I treat StaticCloud just as a normal StaticSolid and add it to groundContacts[] if the collisionAngle is correct.
Now in my update function:
I have access to the fixture, the body, and the entity instances of the specific StaticCloud I'd like to disable collision with. maskBits and categoryBits seem like a bit too much to mess with for this. Collision groupIndex might be suitable, but I'm worried about how to set it back to normal once the player falls through.
Any ideas? Also, what do you think of my usage of collisionAngle? It yields good results but I'm worried about some kind of rare case with unpredictable behavior.
Code:
protected function handlePreSolve(e:ContactEvent):void
{
var collider:PhysicsObject = e.other.GetBody().GetUserData();
if (e.normal && collider is StaticCloud)
{
//collisionAngle gives a horizontally flipped degree, 0 is right, 90 is down, 180 is left, 270 is up
var collisionAngle:Number = new MathVector(e.normal.x, e.normal.y).angle * 180 / Math.PI;
if (!(collisionAngle > 45 && collisionAngle < 135))
{
e.contact.Disable();
}
}
}
{
var collider:PhysicsObject = e.other.GetBody().GetUserData();
if (e.normal && collider is StaticCloud)
{
//collisionAngle gives a horizontally flipped degree, 0 is right, 90 is down, 180 is left, 270 is up
var collisionAngle:Number = new MathVector(e.normal.x, e.normal.y).angle * 180 / Math.PI;
if (!(collisionAngle > 45 && collisionAngle < 135))
{
e.contact.Disable();
}
}
}
Code:
protected function handleBeginContact(e:ContactEvent):void
{
var collider:PhysicsObject = e.other.GetBody().GetUserData();
if (e.normal && (collider is StaticSolid || collider is StaticCloud))
{
//collisionAngle gives a horizontally flipped degree, 0 is right, 90 is down, 180 is left, 270 is up
var collisionAngle:Number = new MathVector(e.normal.x, e.normal.y).angle * 180 / Math.PI;
if (collisionAngle > 45 && collisionAngle < 135)
{
groundContacts.push(e.other);
onGround = true;
}
...
}
}
{
var collider:PhysicsObject = e.other.GetBody().GetUserData();
if (e.normal && (collider is StaticSolid || collider is StaticCloud))
{
//collisionAngle gives a horizontally flipped degree, 0 is right, 90 is down, 180 is left, 270 is up
var collisionAngle:Number = new MathVector(e.normal.x, e.normal.y).angle * 180 / Math.PI;
if (collisionAngle > 45 && collisionAngle < 135)
{
groundContacts.push(e.other);
onGround = true;
}
...
}
}
As you see in the handlePreSolve I have the code in place to allow the player to pass through the cloud platform unless he's approaching it from above. In handleBeginContact I treat StaticCloud just as a normal StaticSolid and add it to groundContacts[] if the collisionAngle is correct.
Now in my update function:
Code:
if (_ce.input.justPressed(Keyboard.S) && onGround)
{
for each(var fixture in groundContacts)
{
if (fixture.GetBody().GetUserData() is StaticCloud)
{
//Disable collision
}
}
}
{
for each(var fixture in groundContacts)
{
if (fixture.GetBody().GetUserData() is StaticCloud)
{
//Disable collision
}
}
}
I have access to the fixture, the body, and the entity instances of the specific StaticCloud I'd like to disable collision with. maskBits and categoryBits seem like a bit too much to mess with for this. Collision groupIndex might be suitable, but I'm worried about how to set it back to normal once the player falls through.
Any ideas? Also, what do you think of my usage of collisionAngle? It yields good results but I'm worried about some kind of rare case with unpredictable behavior.