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

Author Topic: PixelPerfectTest: car simulation  (Read 3071 times)

0 Members and 1 Guest are viewing this topic.

rogeriodec

  • Newbie
  • *
  • Posts: 42
    • View Profile
PixelPerfectTest: car simulation
« on: April 17, 2018, 06:10:12 am »
I am creating a 2d car simulation in c ++ and found it fantastic to detect irregular areas of collision using PixelPerfectTest.
But I would like as I could "advance" this collision.
For example: a car is walking at 80 km / h and there is a barrier in front of it, say, 40 meters away. At this speed, using the full brake, the car will still travel 32 meters ahead.
In the PixelPerfectTest solution, collision is only detected when the car literally bumps into the barrier.



Thinking about it, I created an artificial barrier in front of the car's png image, to force an early collision:



But this is not enough, because in practice, the faster the car is, the more ahead the driver has to pay attention. In this way the artificial barrier in front of the image should be "flexible".
To avoid freakish solutions, I ask: is there any way to pass some parameter to PixelPerfectTest for it to "advance" the collision?

sjaustirni

  • Jr. Member
  • **
  • Posts: 94
    • View Profile
Re: PixelPerfectTest: car simulation
« Reply #1 on: April 17, 2018, 08:05:21 am »
If you're worried about your collision detection not being able to detect a collision between frames, the way to do it is usually casting a ray before your car in the direction of the car's velocity, possibly multiple of those even.

Tijmen

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: PixelPerfectTest: car simulation
« Reply #2 on: April 17, 2018, 11:07:49 am »
Like the answer above, the easy way is to use ray intersection to find if the object would have collided along its path. The easiest thing to do is usually to "project" a new copy of your collider mesh forwards and trace from your current position to this projected copy. You'd usually just connect the corresponding current position vertices to the projected ones and those lines you can use to check whether you "went through" anything else.

Say your state looks like this, with the "future" car being a bit larger to make the projection lines clear later on:

We can trace from our current position to our potential future selves, for each vertex to the future vertex:

Looks like we have some intersections, these ones:


Now for each collision on a current vertex -> future vertex you can only keep the closest one, since the ones further away obviously happen later.
Now you got your closest collisions for a few vertices, use the calculated distance again and only keep the closest one, since that one will happen the first. You now know to what position you can exactly move to get a collision.

Keep in mind that this approach works even if your car is rotating in between frames, but it still requires you to check collisions on the future car because the projected rays don't cover all the angles there are.

rogeriodec

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: PixelPerfectTest: car simulation
« Reply #3 on: April 18, 2018, 04:20:21 pm »
Thanks for the answers.
I found a solution that I believe to be ideal.
I created a second car as a "ghost", that is, an identical copy of the original car's png image, but I left it 99% transparent so that it visually is not detected but is still detected by PixelPerfectTest.
Thus, it turns an object independent of the original car, which runs ahead on the same route, but varies its distance from the original car, depending on its speed.
By doing so, it respects the collision even with the rounded contours of the front of the original car and allows the original car to brake and decelerate properly without colliding with the barrier.

rogeriodec

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: PixelPerfectTest: car simulation
« Reply #4 on: April 18, 2018, 06:24:48 pm »
Here are the examples of how my solution got.
With the "ghost" car appearing:

https://imgur.com/HFfiSZO

And the end result:

https://imgur.com/a/dEDne

rogeriodec

  • Newbie
  • *
  • Posts: 42
    • View Profile
Re: PixelPerfectTest: car simulation
« Reply #5 on: April 18, 2018, 08:04:35 pm »
I received an orientation from user Geheim, which showed me that I simply do not need to draw the ghost car on the screen, so I do not need to create a semi-transparency!
Very simple.
Thank you Geheim!
 :)