Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: player.move() sends player into wal  (Read 3426 times)

0 Members and 1 Guest are viewing this topic.

quinnreilly

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
player.move() sends player into wal
« on: May 30, 2018, 01:50:58 pm »
Hi.

I am currently making a c++ game. I have the movement down and did have the collisions sorted until I realized that they only worked because they were positioned at a multiple of the speed. When I re-positioned the blocks, my character was not flush with the wall because I have buffers around the outside of the character.

When I tried to fix the movement, I tried to make it so that it moves 1 pixel at a time and when a collision occurs, the character will cancel the rest of the movement.

Does anyone have any pointers as to how to fix this? Thanks for any help.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: player.move() sends player into wal
« Reply #1 on: May 30, 2018, 03:31:47 pm »
So you mean it gets stuck in the wall because your collision detection doesn't allow it to move out of the wall again?

I tend to check whether the next move would cause a collision and if it does, I'd resolve it by moving it as close to the wall as allowed. That way you never move the character beyond the allowed boundary and thus it can't get stuck.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

quinnreilly

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: player.move() sends player into wal
« Reply #2 on: May 30, 2018, 03:43:18 pm »
Hi

Thanks for the reply.

The buffer I mentioned previously acts as the next step. the problem is that the buffer is skipped because the player moves 4 pixels every time the move button is pressed. If I make the buffer any bigger, the buffer prevents the character from being flush with the wall. If I slow the movement to 1 pixel, the character is too slow.

I think that I could solve this problem by moving 4 pixels every button press, but interrupt this movement if the character's buffer meets a wall before the movement stops. The problem is my implementation of this does not work. Do you have any ideas on how I could implement this?

Thanks!

Phanoo

  • Full Member
  • ***
  • Posts: 136
    • View Profile
Re: player.move() sends player into wal
« Reply #3 on: June 01, 2018, 04:17:04 pm »
The usual way of doing this is to separate the collision checks depending on the acceleration sign :

// player moving to the right
if(xacc > 0 && collision) xplayer = xwall - widthplayer;

// player moving to the left
if(xacc < 0 && collision) xplayer = xwall;
 

It'll works great for any speed lower than your object/tile width

quinnreilly

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: player.move() sends player into wal
« Reply #4 on: June 01, 2018, 05:20:23 pm »
Hi

I believe I have done something similar to this before, but it caused the player to spaz out because it is constantly moving into the wall and then moving to the outside of the wall.

Dschoonmaker

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: player.move() sends player into wal
« Reply #5 on: July 18, 2018, 06:14:24 pm »
When the player hits a wall, make them move away from the wall by a small amount (like 1 pixel).

verdog

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: player.move() sends player into wal
« Reply #6 on: July 18, 2018, 06:37:14 pm »
So you mean it gets stuck in the wall because your collision detection doesn't allow it to move out of the wall again?

I tend to check whether the next move would cause a collision and if it does, I'd resolve it by moving it as close to the wall as allowed. That way you never move the character beyond the allowed boundary and thus it can't get stuck.
This is how I do it too. Pseudocode:
// this moves the player to where he would be next frame
player.move(player.velocity);

// check for collision on next frame's position
if (player.collidesWithWall(wall)) {
  // move player back to position he was in (that is; not colliding with the wall)
  player.move(-player.velocity); // note the negative sign

  // slowly move player towards wall until they hit it
  while (!player.collidesWithWall(wall)) {
    player.move({tiny distance towards wall, maybe one pixel});
  }

  // player is now inside the wall, move them out
  player.move(-{same tiny distance as above}); // note the negative sign

  // player is now right up next to the wall. stop movement.
  player.velocity = Vector(0, 0);
}
 

 

anything