allegro 4 object interaction + object collision

Started by
1 comment, last by Analizer 11 years ago
Hello.
I have been doing allegro programming since January this year and I'm trying to set-up an object interaction but I'm not sure where to start.
I'm doing a small adventure game and I want a character to pick up stuff from the ground for the game to end. I was looking for solution online but can't find anything that would help me out.
I've got everything done (animation, background etc) apart from the objects and collision which I need help with.
How can I set-up object collision? What I mean is when my character will walk into a wall in the middle of the screen it would stop him and if there is a doorway he would go through.
Also when the character walks into an object for example a gold coin or a stick how can I make it disappear from the screen when he interacts with it?
Thanks, Adrian.
EDIT: here is my code; (I also have a camera class header file but it will not be needed) - void moveCharacter(void) is where the collision should happen I guess.

#include <allegro.h> //allegro library
#include "camera.h" //including camera class

#define BACK 1
#define FRONT 2
#define LEFT 3
#define RIGHT 4
/* defining character movements which will be used for
animation in void characterAnimation later on*/

volatile long speed_counter = 0; // A long integer which will store the value of the timer

void increment_speed_counter();

void moveCharacter(void); //character movement
void characterAnimation(void);
void collisionSide(void); //colision up-down
void collisionUp(void); //collision left-right
void change_frame(void);

int character_y; //character movement left-right integer
int character_x;//character movement up-down integer
int direction;
int resolution; 
int frame;
int movement;
int character_offset;
int character_width;

camera GameCamera; //declaring the camera class as a global veriable

BITMAP *character_right;
BITMAP *character_left;
BITMAP *character_front;
BITMAP *character_back;
BITMAP *tree;
BITMAP *background; //declaring background bitmap
BITMAP *buffer; //pointer to the buffer

int main(void) // main program starts
{
allegro_init(); //initialising allegro
    install_keyboard(); //installing keyboard
install_timer(); //timer so the game will run at the same speed on all PCs
LOCK_VARIABLE(speed_counter); // Used to set the timer which regulates the game speed
LOCK_FUNCTION(increment_speed_counter); //speed counter
set_color_depth(32); // colour depth

    resolution=set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0); //setting game window resolution
if(0 != resolution)
{
allegro_message("Graphics error. Check dekstop resolution and try again.");
return 0;
}

GameCamera.Init(); // initializing the camera for screen changing

tree = load_bitmap("tree.bmp",NULL);
if(!tree)
{
allegro_message("Error! No tree.bmp in this folder..."); //if no character.bmp in game folder showing error message
return 0;
}

character_right = load_bitmap("character_right.bmp",NULL);// ********************************************************
if(!character_right)
{
allegro_message("Error! No character_right.bmp in this folder..."); //if no character.bmp in game folder showing error message
return 0;
}

character_left = load_bitmap("character_left.bmp",NULL);// ********************************************************
if(!character_left)
{
allegro_message("Error! No character_left.bmp in this folder..."); //if no character.bmp in game folder showing error message
return 0;
}

character_back = load_bitmap("character_back.bmp",NULL);// ********************************************************
if(!character_back)
{
allegro_message("Error! No character_up.bmp in this folder..."); //if no character.bmp in game folder showing error message
return 0;
}

character_front = load_bitmap("character_front.bmp",NULL);// ********************************************************
if(!character_front)
{
allegro_message("Error! No character_front.bmp in this folder..."); //if no character.bmp in game folder showing error message
return 0;
}

background = load_bitmap("background.bmp",NULL);// ********************************************************
if(!background)
{
allegro_message("Error! No background.bmp in this folder..."); //if no background.bmp in game folder showing error message
return 0;
}

character_x = 93;
character_y = SCREEN_H /2;
buffer = create_bitmap(SCREEN_W, SCREEN_H);
install_int(moveCharacter,6);
install_int(change_frame,100);

frame =0;
character_offset =0;
character_width =60; //width of the character bitmap. No images exceed 60 pixels
direction = FRONT;
movement = 0;

    while(!key[KEY_ESC])
    {
blit(background,buffer,GameCamera.getcameraX(),GameCamera.getcameraY(),0,0,SCREEN_W,SCREEN_H); //drawing the background to the screen

switch(direction) //animation function for character movement
{
case RIGHT: 
switch(frame)
{
case 0:
character_offset= 0;
character_width= 50;
break;

case 1:
character_offset= 51;
character_width= 74;
break;

case 2:
character_offset= 125;
character_width= 65;
break;

case 3:
character_offset= 190;
character_width= 58;
break;
}

if(movement==0)
{
character_offset= 0;
character_width= 50;
}

masked_blit(character_right,buffer,character_offset,0,character_x,character_y,character_width,104);
break;

case LEFT: 
switch(frame)
{
case 0:
character_offset= 0;
character_width= 61;
break;

case 1:
character_offset= 62;
character_width= 72;
break;

case 2:
character_offset= 134;
character_width= 58;
break;

case 3:
character_offset= 192;
character_width= 63;
break;
}

if(movement==0)
{
character_offset= 0;
character_width= 61;
}
masked_blit(character_left,buffer,character_offset,0,character_x,character_y,character_width,104);
break;

case FRONT: 
switch(frame)
{
case 0:
character_offset= 0;
character_width= 59;
break;

case 1:
character_offset= 60;
character_width= 66;
break;

case 2:
character_offset= 126;
character_width= 67;
break;

case 3:
character_offset= 193;
character_width= 64;
break;
}

if(movement==0)
{
character_offset= 0;
character_width= 59;
}
masked_blit(character_front,buffer,character_offset,0,character_x,character_y,character_width,104);
break;

case BACK: 
switch(frame)
{
case 0:
character_offset= 0;
character_width= 59;
break;

case 1:
character_offset= 60;
character_width= 62;
break;

case 2:
character_offset= 122;
character_width= 66;
break;

case 3:
character_offset= 188;
character_width= 57;
break;
}

if(movement==0)
{
character_offset= 0;
character_width= 59;
}
masked_blit(character_back,buffer,character_offset,0,character_x,character_y,character_width,104);
break;
}

blit(buffer,screen,0,0,0,0,SCREEN_W,SCREEN_H);
clear_bitmap(buffer); //clearing the bitmap
    }
    return 0;

destroy_bitmap(character_right);
destroy_bitmap(character_left);
destroy_bitmap(character_back);
destroy_bitmap(character_front);
destroy_bitmap(buffer);
destroy_bitmap(screen); 
// destroying all bitmaps allows the computer to clear it from the system memory
}
END_OF_MAIN()

void moveCharacter(void) //character movement function
{
int tempx;
int tempy;
     movement=0;

     if(key[KEY_LEFT])
     {
         movement = 1;
         direction = LEFT;
         character_x-=1;
         tempx=GameCamera.getcameraX();

         if((character_x < 0) && (tempx == 0 ))
         {
             character_x = 0;
             GameCamera.setcameraX(0);
         }

         if((character_x < 0) && (tempx == 800 ))
         {
             character_x = 800;
             GameCamera.setcameraX(0);
         }
         if((character_x < 0) && (tempx == 1600 ))
         {
             character_x = 800;
             GameCamera.setcameraX(800);
         }
         if((character_x < 0) && (tempx == 2400 ))
         {
             character_x = 800;
             GameCamera.setcameraX(1600);
         }
     }

     if(key[KEY_RIGHT])
     {
         movement =1;
         direction = RIGHT;
         character_x+=1;
         tempx=GameCamera.getcameraX();

if (character_x > 0 && (character_x == 600)); 

         if((character_x > 800) && (tempx == 0 ))
         {
             character_x = 0;
             GameCamera.setcameraX(800);
         }
         if((character_x > 800) && (tempx == 800))
         {
             character_x = 0;
             GameCamera.setcameraX(1600);
         }
         if((character_x > 800) && (tempx == 1600 ))
         {
             character_x = 0;
             GameCamera.setcameraX(2400);
         }
         if((character_x > 800 ) && (tempx == 2400) )
         {
             character_x = 800;
             GameCamera.setcameraX(2400);
         }
     }

     if(key[KEY_UP])
     {
         movement = 1;
         direction = BACK;
         character_y-=1;
         tempy=GameCamera.getcameraY();

         if((character_y < 0) && (tempy == 0 ))
         {
             character_y = 0;
             GameCamera.setcameraY(0);
         }
         if((character_y < 0) && (tempy == 600 ))
         {
             character_y = 600;
             GameCamera.setcameraY(0);
         }
         if((character_y < 0) && (tempy == 1200 ))
         {
             character_y = 600;
             GameCamera.setcameraY(600);
         }
         if((character_y < 0) && (tempy == 1800 ))
         {
             character_y = 600;
             GameCamera.setcameraY(1200);
         }
     }

     if(key[KEY_DOWN])
     {
         movement =1;
         direction = FRONT;
         character_y +=1;
         tempy=GameCamera.getcameraY();

         if((character_y > 600) && (tempy == 0 ))
         {
             character_y = 0;
             GameCamera.setcameraY(600);
         }

         if((character_y > 600) && (tempy == 600 ))
         {
             character_y = 0;
             GameCamera.setcameraY(1200);
         }
         if((character_y > 600) && (tempy == 1200 ))
         {
             character_y = 0;
             GameCamera.setcameraY(1800);
         }
         if((character_y > 600 ) && (tempy == 1800 ))
         {
             character_y = 600;
             GameCamera.setcameraY(1800);
         }
}
}
void change_frame() // A function to increment the speed counter
{
  frame++; // increment the speed counter by one.
  if(frame > 3)
 frame=0;
}
END_OF_FUNCTION(change_frame); </code>

Advertisement

From what I can see, you already have a form of collision with the edges of the screen:


if(key[KEY_LEFT])
     {
         movement = 1;
         direction = LEFT;
         character_x-=1;
         tempx=GameCamera.getcameraX();

         if((character_x < 0) && (tempx == 0 ))
         {
             character_x = 0;
             GameCamera.setcameraX(0);
         }

...
}

Where you detect if the character coordinates hit a certain edge. If the character does, you set the coordinates to that of the edge, preventing the character from moving off-screen. The same sort of theory can be applied to the most basic collision type, collision between two rectangles.

You check if the coordinates of one object overlaps those of a second object. If they do, they fire functions that react to this; i.e. walking over gold increases the gold count of the character by 1. A quick example of this is:


int x1, y1, w1, h1; //object one
int x2, y2, w2, h2; //object two

// check if 1 or more of the points overlap
if(x1 < x2 + w2
|| x1 + w1 > x2
|| y1 < y2 + h2
|| y1 + h1 > y2)
{
     // if they do, then react to that event here.
     do_function_related_to_collision_here(); // increase gold by 1, pick-up item etc.
}

This is a good place to start, but doing this all over the code can get lengthy, and messy. At this point you will want to look into encapsulating this into a function of its own, but that can be for further research.

On a side note, you also make use of alot of magic numbers in your code, think about changing these to constants as it will become harder to track bugs later.

Regards,

Stitchs.

Thanks for your reply.

Another issue I have got with my game is that the character goes outside the window box. The default window size of my game is 800x600 pixels at one time. If you check the void moveCharacter function you can see the camera class taking place. The problem is the character can go to the right and bottom with no end. I have added this bit of code to main.cpp:

if ( character_x < 30 )
character_x = 30;
else if (character_x >800 )
character_x = 800;

The character stops at 800 pixels.

However.

if ( character_x < 30 )
character_x = 30;
else if (character_x >1200 )
character_x = 1200;

If I change it to a value that is higher than the window size (800x600) no collision is taking place. Any suggestion on how to fix that when the character reaches 1800 pixels accross?

This topic is closed to new replies.

Advertisement