I am trying to get my racing game to recognise when two images have hit eachother when this happens I then want the game to display "Player 1 or 2 wins" and then quit the game. My code is very long so i will not post unless i need to. Im am using visual C++ and have looked at other stuff on the net but cannot seem to find anything that helps.

Dani AI

Generated

As suggested, start with axis-aligned bounding boxes (AABB): it is simple, fast, and usually good for racing-game collisions. Represent each sprite as a rectangle (x,y,width,height), update the rect each frame from the sprite position, then test overlap. The example below assumes a top-left origin with y increasing downward (common in Win32/SDL/GDI).

struct Rect { float x, y, w, h; };

bool AABBOverlap(const Rect& a, const Rect& b)
{
    return a.x < b.x + b.w &&
           a.x + a.w > b.x &&
           a.y < b.y + b.h &&
           a.y + a.h > b.y;
}

Call this after moving both cars. On true, set a gameOver flag and record the winner; then stop normal updates and render a "Player X wins" message for a short time before exiting the main loop so resources are freed cleanly (avoid calling exit in the middle of rendering). Use integer or float coordinates consistently and account for sprite origin (some frameworks position sprites by center, others by top-left).

Quick troubleshooting: draw the bounding boxes on-screen to verify alignment; log positions and sizes each frame for failing cases; check that the collision check runs after position updates. If you need rotation use SAT (separating axis theorem); for pixel-perfect collisions use alpha masks, but both are more complex and slower than AABB. For two cars, AABB is usually sufficient and easiest to integrate in Visual C++.

Recommended Answers

All 2 Replies

That looks like what im looking for, i just wish i knew how to implement it into my code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.