Ask coding questions

← Back to all posts
Create a solid wall if you know when the character is intersecting with the wall in p5.js
CrazyVideoGamez (7)

Hello, I'm working on a kinda crappy platformer, and I need to create a wall. I have a function that tells you if the character (cat) is intersecting with the wall. I want to find a way to stop the character from going into the wall. I want the character to be able to stand on top of a wall and also be stopped when walking into a wall.

Comments
hotnewtop
19wintersp (1121)

Wherever you're updating the sprite's position, change the position virtually without updating the actual position, and check if that new position intersects with the wall. If it does, don't actually change the position?

CrazyVideoGamez (7)

@19wintersp I tried adding it to the Player.js file (using checkWallIntersectionBeforePosChange), but it's not really working, now the Player is bugged. Can you please tell me why? thx

19wintersp (1121)

@CrazyVideoGamez I think it's because you're checking the collision between the current position and the wall (Player.js:156), rather than the x and y arguments.

CrazyVideoGamez (7)

@19wintersp Ok, I am now using an updated bounding box. But it still isn't working 🤔.

ABadGamer73 (0)

@CrazyVideoGamez so maybe you have to triple check your code or something

19wintersp (1121)

@CrazyVideoGamez You're not though:

let newBoundingBox = {
  tL: createVector(this.pos.x, this.pos.y),
  // tR: [this.pos.x + 25 + 126, this.pos.y + 6],
  // bL: [this.pos.x + 25, this.pos.y + 6 + 94],
  bR: createVector(this.pos.x + this.catWidth, this.pos.y + this.catHeight)
}

just creates a bounding box from the existing position (this.pos). You need to use x and y:

let newBoundingBox = {
  tL: createVector(x, y),
  bR: createVector(x + this.catWidth, y + this.catHeight)
}
CrazyVideoGamez (7)

@19wintersp Ok, I put in the new position instead of the old position, but it is now REALLY glitchy.

CrazyVideoGamez (7)

@19wintersp btw variables x, y don't exist so ya

19wintersp (1121)

@CrazyVideoGamez They're passed to the function as parameters.

CrazyVideoGamez (7)

@19wintersp I updated the function to take in a bounding box

19wintersp (1121)

@CrazyVideoGamez Where are you calling it from?

CrazyVideoGamez (7)

@19wintersp mb, I'm still trying to fix the bugs. Go to player_old.js, it will be in there, called inside of the function display

19wintersp (1121)

@CrazyVideoGamez You're still changing the bounding box even when it would collide.

CrazyVideoGamez (7)

@19wintersp ok, I fixed that, but now the cat is going crazy

CrazyVideoGamez (7)

@19wintersp Oh and also, checkWallIntersectionBeforePosChange is broken, because it only returns false when the cat reaches the end, and I don't god f'ing know why
it's so painful