Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Need simple collision and hit box

This thread is locked; no one can reply to it. rss feed Print
Need simple collision and hit box
matsumoto
Member #16,849
April 2018

Hi , I searched on several sites but there was too much detail and I could not find a subject that describes a simple collision code. That's why I did not understand

how can I hit the boxes with ball and then bounce the ball ?

----------------- This is my code : ----------------------

#include <stdio.h>
#include <allegro5\allegro.h>
#include <allegro5\allegro_font.h>
#include <allegro5\allegro_ttf.h>
#include <allegro5\allegro_native_dialog.h>
#include <allegro5/allegro_primitives.h>
#include<allegro5/allegro_image.h>

const int width = 1024;
const int height = 768;

enum KEYS { LEFT, RIGHT };

struct ball { //KEEPS TRACK OF BALL VARIABLES
float x;
float y;
float vely;
int radius;

};
int main(void) {

int FPS = 60;
int score = 0;
int imagew = width / 2 + 150;
int imageh = 150;

bool done = true;
bool keys[2] = { false,false };

ball myBall; //Make a ball
myBall.x = width / 2;
myBall.y = height / 2;
myBall.vely = 0;
myBall.radius = 15;
//addon init
al_init();
al_init_font_addon();
al_init_ttf_addon();
al_init_primitives_addon();
al_install_keyboard();
al_init_image_addon();
//Allegro variable
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_BITMAP *image;
ALLEGRO_FONT *font = al_load_ttf_font("arial.ttf", 24, 0);

image = al_load_bitmap("image.png");
al_convert_mask_to_alpha(image, al_map_rgb(106, 76, 48));

display = al_create_display(width, height);

event_queue = al_create_event_queue();
timer = al_create_timer(1.0 / FPS);

al_register_event_source(event_queue, al_get_keyboard_event_source());
al_register_event_source(event_queue, al_get_timer_event_source(timer));
al_register_event_source(event_queue, al_get_display_event_source(display));

al_start_timer(timer);
while (done) {
al_draw_textf(font, al_map_rgb(255, 255, 255), 45, 5, ALLEGRO_ALIGN_CENTER, "Score: %d", score);
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);

myBall.y += myBall.vely;
myBall.vely += 0.25; //Ball's velocity downward increases (gravity)
printf("%f -- %f \n", myBall.vely, myBall.y + myBall.radius);

if (myBall.y + myBall.radius > 600 && myBall.vely > 0) { //Ball has hit the floor and is moving downward
myBall.y = 600; //Set the ball to just on top of the floor
myBall.vely = -myBall.vely; //Bounce

}

if (ev.type == ALLEGRO_EVENT_KEY_DOWN) {
switch (ev.keyboard.keycode) {
case ALLEGRO_KEY_LEFT:
keys[LEFT] = true; break;
case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = true; break;
}
}
else if (ev.type == ALLEGRO_EVENT_KEY_UP) {
switch (ev.keyboard.keycode) {
case ALLEGRO_KEY_LEFT:
keys[LEFT] = false; break;
case ALLEGRO_KEY_RIGHT:
keys[RIGHT] = false; break;
}
if (ev.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
done = false;
}
else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
done = false;
}
else if (ev.type == ALLEGRO_EVENT_TIMER) { // Smoother movement...
myBall.x -= keys[LEFT] * 15;
myBall.x += keys[RIGHT] * 15;

imageh += 3;

}

al_clear_to_color(al_map_rgb(0, 0, 0));
al_draw_bitmap(image, imagew, imageh, 0);
al_draw_circle(myBall.x, myBall.y, myBall.radius, al_map_rgb(0, 255, 0), 2);
al_flip_display();

}
al_destroy_display(display);
al_destroy_bitmap(image);
}

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

You can use <code>code goes here</code> tags to post code, and the paper and pencil icon on your post to edit it.

You're looking for AABB or Rectangle vs Circle collision detection. Rectangle overlap checks are fairly easy, the second is harder.

For an AABB check, the algorithm goes :
1. If left of r1 is right of the right of r2, no collision.
2. If right of r1 is left of the left of r2, no collision.
3. If top of r1 is below the bottom of r2, no collision.
4. If bottom of r1 is above the top of r2, no collision.
5. Collision detected.

Go to: