0
\$\begingroup\$

I'm making a top-down game in Java. I watched some videos, and this is the code I currently use when the player is colliding with an object:

x += -velX
y += -velY

When I apply force to a wall, the player dips into it, and can't move along the wall. Does anyone have a better method?

\$\endgroup\$
  • \$\begingroup\$ Presumably you checked the collision-resolution tag for some ideas first? What did you learn in the course of your research? What help do you need applying it to your game? \$\endgroup\$ – DMGregory Sep 2 at 11:14
0
\$\begingroup\$

Well

If you're asking about purely preventing an invevitable movement, then simply

  • checking the position of your proposed movement against the collision bounds of your player and the wall
  • And adjusting the player's position back out of the wall

will do just fine.


However, if you want to prevent the initial movement altogether, then you need to look at adjusting the player's velocity, or redirecting it in some fashion (depending on how bouncy you want your surface to be), prior to the movement being applied.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I am checking the position, and putting the player out of the wall. The problem is that when applying force on the wall, it is constantly dipping in and going out of the wall, preventing the player from moving. \$\endgroup\$ – big papa Aug 3 at 9:54
  • \$\begingroup\$ Then you are correctly doing what you asked. What you need to do is my second option, and look into adjusting the player velocity before or after you collide with the wall. otherwise, the player will retain their velocity and keep trying to move into the wall. Look into conservation of momentum. \$\endgroup\$ – Natalo77 Aug 3 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.