1
\$\begingroup\$

I'm currently trying to make a small ball-like game in C++. I have two disc-like objects with varying radii. Let's say Disc 1 is on coordinates (100,0) and Disc 2 is on coordinates (400,0). Now Disc 1 has a velocity vector of (300,0) and Disc 2 has a velocity vector of (-300,0). In my program, the Discs just swap places and no collision happens.

Now how do I program in the collisions? Do I have to program my own manual raycasting or something?

\$\endgroup\$
  • \$\begingroup\$ Do you really need such speeds in your game? If velocity is measured in pixels per tick, then it's too high for "a small ball-like game". Usually you want to update your game objects 60 times per second. At 60 TPS one usually needs velocities around 1..15 pixels/second. At such small speeds you won't need to bother with fancy continuous collision detection. \$\endgroup\$ – HolyBlackCat Nov 29 '16 at 14:04
1
\$\begingroup\$

This is a continuous collision problem. The "bullet through paper" problem as most refer to it. I'm not sure how you implemented your system, your question could use more details. But it sounds like you're discretely checking collision. Basically in the example below: enter image description here The ball never intersects the rectangle since it's doing a basic intersection test every iteration. There's lots of ways to battle this. It's called continuous collision detection. Raycasting can produce some results. But there are many use cases where unless you shot infinite rays out of the circle you wouldn't be able to use it. Consider the examples below: enter image description here

This is where shooting one ray or even two would break. What you need is too model the area the shape will take as it moves. Like so: enter image description here Above shows the blue area that needs too be checked. So calculate the area to be checked and then if it is intersecting you need to calculate where to resolve the ball too on the rectangle. The resolve is the hard part.

Obviously CCD (continous collision detection) is costly and in fact not used in many games/applications. But sometimes it is needed to produce acceptable results.

You can get around using CCD by using smaller velocities ensuring none of your objects are able to pass through one another.

\$\endgroup\$
  • \$\begingroup\$ Couldn't I just raycast from the center of the disc in the direction of movement? \$\endgroup\$ – bi0phaz3 Nov 29 '16 at 0:21
  • 1
    \$\begingroup\$ I show on picture 2 why that doesn't work. The arrow is the ray shot which is also the direction of movement. You can see that the ray will not hit the rectangle yet if we kept moving it in that direction it would indeed hit it. \$\endgroup\$ – Andrew Wilson Nov 29 '16 at 0:25
  • \$\begingroup\$ You can use GJK for CCD with the restriction that the motion needs to be linear (so the space+time bounding box remains convex) \$\endgroup\$ – ratchet freak Nov 29 '16 at 9:18

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.