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

Author Topic: Collisions Fail at higher speed....  (Read 2454 times)

0 Members and 1 Guest are viewing this topic.

aNewHobby

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
    • Live 4 Ever Or Die Trying
Collisions Fail at higher speed....
« on: July 21, 2012, 04:28:34 am »
I'm using a bunch of collision tests. Everything works great.. but if I am going to fast then the collisions simply fail and nothing happens. I am trying to make a pacman clone so I would like pac-man to move pretty fast by the end of a long game.

  • Pac-Man is moving though a factor = velocity * frametime;
  • I am using SFML rectangles to do the tests... eg...sf::FloatRect.contains(pacman.getPostion); This tests a point in space to see if it is inside the rectangle. The point being the centre of pac-man.


So yeah.. I would love to hear any ideas on this!

Thanks in advance

Example of Collision as it is now...

 if ( _rectSwitch.getGlobalBounds().contains( PacMan.getPosition().x, PacMan.getPosition().y ) )
Twitter: @Jyinkies
My Own Little Spot on the Net: - Live4everOrDieTrying.info (Blog)

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Collisions Fail at higher speed....
« Reply #1 on: July 21, 2012, 05:03:59 am »
There isnt really enough code to see exactly what is going on, but one thing I did see in my code some time ago was make sure you have your collision detection check before the movement of the "object", if you move the character then check you will go through anything if they have the correct speed. if you check because the player moves then it will see the collision and stop, that might not be the correct way, but it did help me once apon a time :)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Collisions Fail at higher speed....
« Reply #2 on: July 21, 2012, 09:13:44 am »
Unfortunatly that's a normal problem with collision detection. There a few solution for it. The easiest is to increase the collision box to a size where you can guarantee that one movement step can't just simply pass the collision box (e.g. the box is 20px and one movement is 30px, thus the sprite just 'jumps' over the collision rectangle).
Another solution is not just to check intersection, but to actually calculate when the collision would happen. This can be quite tricky though.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

WiltedChameleon

  • Newbie
  • *
  • Posts: 16
    • View Profile
    • Email
Re: Collisions Fail at higher speed....
« Reply #3 on: July 24, 2012, 04:56:17 pm »
Expanding on exploiter's post you could do a rectangle where X is the sprite's X position, minus any negative horizontal velocity, Y is the sprite's Y position minus any negative vertical velocity, the width is the sprite's width add any positive horizontal velocity and the height is the sprite's height add any horizontal velocity.

It's not overly efficient, and it requires a bit more math (Although god help your computer if a few basic math expressions make a difference), but in the end of the day it's a simpler solution than some, especially if you're just dealing with regular rectangles like in a pacman game.

 

anything