Good day! I am trying to create a 2D game for personal use. However, I am having a hard time figuring out why the collision component is not working. My character is still able to walk through walls even if I already added a rigid body and collision component. What do you guys think is causing this? I’ve been doing similar projects before, but this is my first time encountering such a problem. Is this a bug?
Hi @cormilyn_g,
How are you moving your character? Are you using physics or just something like setLocalPosition()
/translateLocal()
? If you are moving the character without physics, and they have a dynamic rigidobdy type, the collider is not actually being moved, so the engine never thinks it’s colliding with anything. You will have to use forces or impulses to ensure that the physics simulation and collision is accurate.
If this is the case you’ll probably be able to see this behavior at runtime with @yaustar 's excellent devTools here:
var Walk = pc.createScript(‘walk’);
var mcDir = 270;
Walk.prototype.update = function(dt) {
// pos = this.entity.getPosition()
if(this.app.keyboard.isPressed(pc.KEY_RIGHT)){
this.entity.sprite.play("JoeWalkingRight"); this.entity.rigidbody.applyForce(2800,0,0); mcDir = 180; } else if(this.app.keyboard.isPressed(pc.KEY_LEFT)){ this.entity.sprite.play("JoeWalkingLeft"); this.entity.rigidbody.applyForce(-2800,0,0); mcDir = 0; } else if(this.app.keyboard.isPressed(pc.KEY_UP)){ this.entity.sprite.play("JoeWalkingUp"); this.entity.rigidbody.applyForce(0,2800,0); mcDir = 90; } else if(this.app.keyboard.isPressed(pc.KEY_DOWN)){ this.entity.sprite.play("JoeWalkingDown"); this.entity.rigidbody.applyForce(0,-2800,0); mcDir = 270; }else{ if(mcDir === 0){ this.entity.sprite.play("JoeLeft"); }else if(mcDir == 90){ this.entity.sprite.play("JoeUp"); }else if(mcDir == 180){ this.entity.sprite.play("JoeRight"); }else if(mcDir == 270){ this.entity.sprite.play("JoeDown"); } }
};
-This is how my code looks like.
My character’s rigid body is already set to dynamic but it’s still not working.
I took a look at your project, and I see that you have the sprites on a 2D screen. This is generally incompatible with the sprite system, and if you use the devTools at runtime, you can see that your physics shapes are very small, and likely not in the correct position. Try saving a checkpoint and then removing all of the sprites and colliders from the 2D screen. You’ll have to do a fair bit of rearranging, but it should help with the warping of all the physics objects
It worked! Thanks so much.