// Wall
var bottomWall = new cp.SegmentShape(this.space.staticBody,
cp.v(0, this.bottomWallHeight),
cp.v(MAX_INT, this.bottomWallHeight),
wallWidth);
bottomWall.setElasticity(0);
bottomWall.setFriction(0);
this.space.addStaticShape(bottomWall);
And making a physicsSprite shape like this:
// Create and add the physics shape
var carShape = new cp.BoxShape(this.carBody, carSize.width * scaleCarSize, carSize.height * scaleCarSize);
carShape.setElasticity(0);
carShape.setFriction(0);
this.space.addShape(carShape);
And then when a button is pressed I apply this force to the phyics shape:
// Apply down impulse on player
downImpulse: function () {
this.carBody.applyImpulse(cp.v(0, -500), cp.v(0, 0));
},
Then when the carBody hits the wall segment, it bounces back and creates a constant force the other way. How can I make it so that the car just stops from moving any lower and stays against the bottomWall?
arbiter.a and arbiter.b are the shape that collided and can be wall collide car or car collide wall, so a and b would switched.
So you can check the arbiter.a and arbiter.b collision type as
if(arbiter.a.collision_type == wall)
{
//a is wall
//b is car
}
else
{
//b is wall
//a is car
}
var bodyA = arbiter.a.getBody();
var bodyB = arbiter.b.getBody();
Then you can do anything to bodyA and bodyB to stop them like bodyA.setVel(cp.v(0, 0));
and may be can change their force and impulse, can’t find document how to do that, not sure available on JS version or not.
But if you want to remove the shape, you need do it in post step call back as
var self = this;
this.space.addPostStepCallback(function()
{
self.space.removeShape(shape);
}
Thanks for your help. I tried that and then the car just went right through the wall. I’m not sure if I’m doing it wrong, I might not totally understand the logic of how physics works exactly since there isn’t much documentation about it in JS.
But…
I fixed my issue by comparing the height/position of the wall and height/position of the car then applied an equal and opposite force right before colliding.