• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
NemanjaBartolovic

Allegro 5 Collision Detection

1 post in this topic

Uhm, hello everyone, this is my first post on these forums. I'd like to receive some help on adding simple collision to a small pong project which I'm trying to make with Allegro5. Most of the code has just been assembled by reading the tutorials on the Allegro 5 wiki. Thing is, I don't know where/how to implement bounding box collision between, let's say, paddle 1 and bouncer. Here's the code, it's not too long.

[code]#include <iostream>
#include <cstdio>
#include <allegro5/allegro.h>

using namespace std;

const float FPS = 60;
const int SCREEN_W = 1024;
const int SCREEN_H = 768;
const int BOUNCER_SIZE = 40;
const int PADDLE_SIZE_X = 40;
const int PADDLE_SIZE_Y = 160;
enum MYKEYS{KEY_W, KEY_S};

int bounding_box_collision ( int b1_x, int b1_y, int b1_w, int b1_h, int b2_x, int b2_y, int b2_w, int b2_h )
{
if ( ( b1_x > b2_x + b2_w - 1 ) || // is b1 on the right side of b2?
( b1_y > b2_y + b2_h - 1 ) || // is b1 under b2?
( b2_x > b1_x + b1_w - 1 ) || // is b2 on the right side of b1?
( b2_y > b1_y + b1_h - 1 ) ) // is b2 under b1?
{
// no collision
return 0;
}

// collision
return 1;
}

int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display = NULL;
ALLEGRO_EVENT_QUEUE *event_queue = NULL;
ALLEGRO_TIMER *timer = NULL;
ALLEGRO_BITMAP *bouncer = NULL;
ALLEGRO_BITMAP *p1Paddle = NULL;
ALLEGRO_BITMAP *p2Paddle = NULL;
int randNum;
float bouncer_x = SCREEN_W / 2.0 - BOUNCER_SIZE / 2.0;
float bouncer_y = SCREEN_H / 2.0 - BOUNCER_SIZE / 2.0;
float p1Paddle_x = 10;
float p1Paddle_y = SCREEN_H / 2.0 - PADDLE_SIZE_Y / 2.0;
float p2Paddle_x = SCREEN_W - 10 - PADDLE_SIZE_X;
float p2Paddle_y = SCREEN_H / 2.0 - PADDLE_SIZE_Y / 2.0;
float bouncer_dx = -4.0, bouncer_dy = 4.0;
bool key[2] = {false, false};
bool doexit = false;
bool redraw = true;


if (!al_init())
{
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}

if(!al_install_keyboard())
{
fprintf(stderr, "failed to initialize the keyboard!\n");
return -1;
}

timer = al_create_timer(1.0 / FPS);
if (!timer)
{
fprintf(stderr, "failed to create timer!\n");
return -1;
}

display = al_create_display(SCREEN_W, SCREEN_H);

if (!display)
{
fprintf(stderr, "failed to create display!\n");
return -1;
}

bouncer = al_create_bitmap(BOUNCER_SIZE, BOUNCER_SIZE);
if (!bouncer)
{
fprintf(stderr, "failed to create bouncer bitmap!\n");
al_destroy_display(display);
al_destroy_timer(timer);
return -1;
}

p1Paddle = al_create_bitmap(PADDLE_SIZE_X, PADDLE_SIZE_Y);
if (!p1Paddle)
{
fprintf(stderr, "failed to create player one paddle bitmap!\n");
al_destroy_display(display);
al_destroy_timer(timer);
return -1;
}

p2Paddle = al_create_bitmap(PADDLE_SIZE_X, PADDLE_SIZE_Y);
if (!p2Paddle)
{
fprintf(stderr, "failed to create player two paddle bitmap!\n");
return -1;
}


al_set_target_bitmap(p1Paddle);

al_clear_to_color(al_map_rgb(0, 191, 255));

al_set_target_bitmap(p2Paddle);

al_clear_to_color(al_map_rgb(0, 255, 191));

al_set_target_bitmap(bouncer);

al_clear_to_color(al_map_rgb(255, 0, 255));

al_set_target_bitmap(al_get_backbuffer(display));

al_start_timer(timer);

event_queue = al_create_event_queue();
if (!event_queue)
{
fprintf(stderr, "failed to create event_queue!\n");
al_destroy_bitmap(bouncer);
al_destroy_display(display);
al_destroy_timer(timer);
return -1;
}

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

al_clear_to_color(al_map_rgb(0, 0, 0));

al_flip_display();

al_start_timer(timer);

while(!doexit)
{
ALLEGRO_EVENT ev;
al_wait_for_event(event_queue, &ev);

if (ev.type == ALLEGRO_EVENT_TIMER)
{
bounding_box_collision ( p1Paddle_x, p1Paddle_y, PADDLE_SIZE_X, PADDLE_SIZE_Y, bouncer_x, bouncer_y, BOUNCER_SIZE, BOUNCER_SIZE );

if (bouncer_x < 0 || bouncer_x > SCREEN_W - BOUNCER_SIZE)
{
bouncer_dx = -bouncer_dx;
}
if (bouncer_y < 0 || bouncer_y > SCREEN_H - BOUNCER_SIZE)
{
bouncer_dy = -bouncer_dy;
}

bouncer_x += bouncer_dx;
bouncer_y += bouncer_dy;

if (bouncer_y + PADDLE_SIZE_Y / 2 >= SCREEN_H)
{
p2Paddle_y = SCREEN_H - PADDLE_SIZE_Y / 2;
}
else if(bouncer_y - PADDLE_SIZE_Y / 2 < 0)
{
p2Paddle_y = PADDLE_SIZE_Y / 2;
}
else
{
p2Paddle_y = bouncer_y;
}

if(key[KEY_W] && p1Paddle_y > 0 )
{
p1Paddle_y -= 4;
}

if(key[KEY_S] && p1Paddle_y < SCREEN_H - PADDLE_SIZE_Y)
{
p1Paddle_y += 4;
}

redraw = true;
}
else if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
{
break;
}
else if(ev.type == ALLEGRO_EVENT_KEY_DOWN)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_W:
key[KEY_W] = true;
break;
case ALLEGRO_KEY_S:
key[KEY_S] = true;
break;
}
}
else if(ev.type == ALLEGRO_EVENT_KEY_UP)
{
switch(ev.keyboard.keycode)
{
case ALLEGRO_KEY_W:
key[KEY_W] = false;
break;
case ALLEGRO_KEY_S:
key[KEY_S] = false;
break;
case ALLEGRO_KEY_ESCAPE:
doexit = true;
break;
}
}

if (redraw && al_is_event_queue_empty(event_queue))
{
redraw = false;
al_clear_to_color(al_map_rgb(0, 0, 0));
al_draw_bitmap(p1Paddle, p1Paddle_x, p1Paddle_y, 0);
al_draw_bitmap(p2Paddle, p2Paddle_x, p2Paddle_y - PADDLE_SIZE_Y / 2, 0);
al_draw_bitmap(bouncer, bouncer_x, bouncer_y, 0);
al_flip_display();
}
}

al_destroy_bitmap(bouncer);
al_destroy_bitmap(p1Paddle);
al_destroy_bitmap(p2Paddle);
al_destroy_timer(timer);
al_destroy_display(display);
al_destroy_event_queue(event_queue);

return 0;
}[/code]

Thank you for your time. And one more thing: would you advise me to continue working further with Allegro, or should I get another library set?

FallenShard
0

Share this post


Link to post
Share on other sites
All you need to do is: check in your logic code if paddle1 and bouncer's collision boxes are touching. You can assume they're always touching, and use the following statements to set collision to false.

[code]
collision = true;

if (paddle1_bottom < bouncer_top)
{
collision = false;
}

if (paddle1_top > bouncer_bottom)
{
collision = false;
}

if (paddle1_right < bouncer_left)
{
collision = false;
}

if (paddle1_left > bouncer_right)
{
collision = false;
}
[/code]

Use whatever tools you feel complete the job. New programmers spend way too much time on what tools and languages they should be using, and almost no time on completing actual projects. If the games you're currently making surpass the ability of Allegro, then yes, move on! This is ultimately a choice you will have to make, as you will be the one coding with that library.
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0