so I’m trying to even out my box collision for the bounding box by completely centering the bounding box on each atlas animation frame that is currently animating.
i tried this & it works for my character when facing right :
what i meant by go through the walls is for some reason SOMETIMES, the collision faults & the whole character bounding box & all falls through the wall & through the floor.
That can happen if you change the body size or offset such that part of the body immediately extends into a wall/floor. If you’re making the body taller, for example, you would want to move the offset upwards so that only the top edge is extended and the bottom edge doesn’t move (assuming the character is on the floor). For walls I don’t know. You could try increasing physics.world.OVERLAP_BIAS.
I’m not sure, and it might depend on your atlas. After you change the texture frame
how would i A) prevent character from sometimes going through wall & B) get the X-offset of the flipped sprite so I can add it to the width of the bounding box? Also, how would I center the bounding box all the time no matter what offset is used? i’ll use a static size, but a constantly changing center that basically targets the player.
is there a way to center the bounding box onto the player and have collision equal on both sides of the player but still not be able to go through the wall?
i just tried editing the origins of each sprite in the texture atlas & it ended in disaster. i have a better idea. how do i keep the bounding box static, yet still have the texture stay IN the level & not in the walls?
I’ve nearly figured it out. Problem is when I’m running then I do a jump, the bounding box ceases to go back to normal while in the air… What am I doing wrong here?
So I have decided to try body.setSize ( )... & it seems to work better. But for some reason when I am holding the walking key & then press the jump key, the bounding box while in mid-air refuses to resize to the correct size. It stays the same size as the walking animation’s bounding box.
How do I resize it correctly while doing a running jump?
// Get current Animation name
this.__currentAnimName = ( sprite.anims.getCurrentKey ( sprite.anims ) );
this.__spriteW = ( sprite.width );
this.__spriteH = ( sprite.height );
this.__spriteBodW = ( sprite.body.width );
this.__spriteBodH = ( sprite.body.height );
this.__realFrameW = ( sprite.frame.realWidth );
this.__realFrameH = ( sprite.frame.realHeight );
// Apply horizontal acceleration when left/a or right/d
// keys are applied
if (this.keys.left.isDown || this.keys.a.isDown){
// If the `Player` is `touching` floor
if ( sprite.body.onFloor () ){
// Get Offset{s} { X / Y }
this.__offX = 0;
this.__offY = 0;
// Set Offset{s} { X / Y }
// Set the Bounding Box's Size
sprite.body.offset.x = this.__offX;
sprite.body.offset.y = this.__offY;
sprite.body.setSize ( this.__spriteW, this.__spriteH, true );
// Set Player Animation to `Walk`...
// No need to have a separate set of graphics for running to the
// left & right
// We can just mirror the sprite
sprite.setFlipX(true);
}
// Set Player's Negative X-Acceleration
}
// Otherwise, if the `right` key or `D` key is being
// held down
else if(this.keys.right.isDown || this.keys.d.isDown){
// If the `Player` is `touching` floor
if ( sprite.body.onFloor() ) {
// Get Offset{s} { X / Y }
this.__offX = ( 0 );
this.__offY = ( 0 );
// Set Offset{s} { X / Y }
// Set the Bounding Box's Size
sprite.body.offset.x = this.__offX;
sprite.body.offset.y = this.__offY;
sprite.body.setSize ( this.__spriteW, this.__spriteH, true );
// Set `Player` Animation to `walk`
}
// Set Player's Positive X-Acceleration
// Do NOT flip the Player's Sprite
sprite.setFlipX(false);
// Otherwise, if NOT holding down
// left/right or A/D,
} else {
// If the `Player` is `touching` floor
if ( sprite.body.onFloor ( ) )
{
// Get Offset{s} { X / Y }
this.__offX = ( 0 );
this.__offY = ( -4 );
// Set Offset{s} { X / Y }
// Set the Bounding Box's Size
sprite.body.offset.x = this.__offX;
sprite.body.offset.y = this.__offY;
sprite.body.setSize ( this.__spriteBodW, this.__spriteBodH, true );
// Set `Player` Animation to `idle`
}
// Stop Player's current X-Acceleration
}
// Player can jump while walking any direction by
// pressing the space bar or the 'UP' arrow
if ( this.keys.w.isDown || this.keys.up.isDown && sprite.body.onFloor ( ) ) {
// Get Offset{s} { X / Y }
this.__offX = ( 0 );
this.__offY = ( 0 );
sprite.body.setSize ( this.__spriteW, this.__spriteH, true );
// Set Player's Y-Velocity
// Set `Player` Animation to `jump`
}