Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » trouble with collision detection

This thread is locked; no one can reply to it. rss feed Print
trouble with collision detection
DuncanShine
Member #17,479
March 2020

I have a rectangular bitmap for my character, and a circular bitmap for the object he is supposed to collide with, however the issue is that it only registers collisions on one side of the circle, and on the other side it's substantially off. Note: There are multiple bitmaps that the character can have, and one of these is a slightly different width, so this may affect the results

Here is the code that detects collisions (Edgar Reynaldo wrote some of it):

#SelectExpand
1 int planetX = 500; 2 int planetY = 300; 3 4 int playerBottomX = playerX + al_get_bitmap_width(idle); 5 int playerBottomY = playerY + 64; 6 if (dir == NONE) { 7 playerBottomX = playerX + al_get_bitmap_width(idle) / 2; 8 playerBottomY = playerY + al_get_bitmap_height(idle); 9 } 10 else if (dir == RIGHT || dir == LEFT) { 11 playerBottomX = playerX + 31 / 2; 12 playerBottomY = playerY + 64; 13 } 14 15 int dx = (playerBottomX - planetX); 16 int dy = (playerBottomY - planetY); 17 double dzsq = (pow(dx, 2) + pow(dy, 2)); 18 double dz = sqrt(dzsq); 19 20 int dl = planetRadius + runningPlayerWidth; 21 cout << "x: " << dx << " y: " << dy << " dl: " << dl << " dz: " << dz; 22 if (dz <= dl) { 23 isColliding = true; 24 cout << " collision" << endl; 25 } 26 else { 27 isColliding = false; 28 cout << " no collision" << endl; 29 }

I have also included images that show where the character is registered as "colliding" with the circle, and also the relevant positional values.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

First - I didn't write your code - I gave you a formula for the distance from a circle. It looks like you're doing that correctly. What exactly is wrong?

Why are you adding the width of the sprite to the radius? What point on the sprite do you want to collide with the planet? The middle bottom? Then calculate the distance from that point to the circle.

DuncanShine
Member #17,479
March 2020

Yeah i was following a tutorial that used two circle sprites so error was in the adding width + radius part.
It all seems to work fine now, though i'm not sure how to make the player move back if they're colliding with the circle.

NiteHackr
Member #2,229
April 2002

Well, you add your movement to the player, check for collision, if there is a collision, you subtract the movement you just added so the player is where they were before they collided.

player.x += player.xmove;
if(collision happened) player.x -= player.xmove;

I also see a lot of "magic numbers" in your code. The " + 64" etc. What does 64 represent? You should put that into a variable or something like a "const" which is a constant. If that represents a width of a sprite for example, have "const int sprite_width = 64;" then you have " + sprite_width;" in your code so it is easier to understand what is going on (because in the future, you WILL forget what some of these numbers were for) and if you wish to change it, you just change one variable and everywhere it is used is updated.

DuncanShine
Member #17,479
March 2020

I changed how the sprite moves now; it works by just rotating the sprite about the middle of the circle because it was easier

piccolo
Member #3,163
January 2003
avatar

soon there will be no such this as collision detection i will be ridding us of it completely in the near future

wow
-------------------------------
i am who you are not am i

DuncanShine
Member #17,479
March 2020

One huge problem I have now is getting the coordinates of a rotated sprite

Go to: