victorfisac / Physac

2D physics header-only library for videogames developed in C using raylib library.
http://www.victorfisac.com/physac
MIT License
432 stars 28 forks source link

Knowing whether collisions have happened #51

Closed ncot-tech closed 2 years ago

ncot-tech commented 3 years ago

(I found the closed issue from 2017, but this query is a little different)

I'm just using Physac by itself, with SDL 2. So far I have things that move and collide quite nicely.

While I understand collisions are internal to the engine, is there a strategy to know whether two things have collided? For example suppose I have a rocket moving across the screen and it hits a target, how can I tell the collision has happened so I can make some game logic run?

victorfisac commented 2 years ago

You should calculate it by yourself defining an area of collision (different to the one defined in PhysicsShape struct or reuse the same one). The way to detect that two physics bodies has collided is defined in the CheckCollisionCircleRect and the other functions.

But I think it's simpler if you detect it by your own using some data from the structs and do it in the main thread so you can apply whatever logic you want... raylib provides functions to check collisions between shapes just passing the shape properties as parameters.