[Allergo] Collisions?

External libraries (GTK, GSL, SDL, Allegro, OpenGL, etc) questions.
Swos2008
Posts: 91
Joined: May 15, 2008 13:21

[Allergo] Collisions?

Postby Swos2008 » Apr 04, 2010 22:48

At the moment...

I think I got the collisions right but for some reason...it not printing on the screen to tell me that the Collisions is working!!?

Why that?

Code: Select all

#include "allegro.bi"
Option Explicit

#define WHITE makecol(255,255,255)
#define SCREEN_WIDTH  640                     
#define SCREEN_HEIGHT 480
#define Depth 32
#define Speed 3

'------------------------------------------------------------------------------
Dim shared Buffer       As bitmap ptr
Dim shared Background   AS Bitmap ptr
Dim shared Dog          As Bitmap Ptr
Dim shared Stick        As Bitmap Ptr
'------------------------------------------------------------------------------

'------------------------------------------------------------------------------
' You must Declare the function without you will get error when you run it!
'------------------------------------------------------------------------------
Declare Function Allergo_Setup
Declare Function Load_Images
Declare Sub incRement_speed_counter cdecl()
Declare Function Move_Player
Declare Function Render_Sprites
Declare Function Shut_down
'------------------------------------------------------------------------------

Common Shared speed_counter As  Long

speed_counter = 0

Allergo_Setup

Load_Images

Dim shared Dog_x_position   as integer
Dim shared Dog_y_position   as integer

Dim Shared Stick_x_position as integer
Dim Shared Stick_y_position as integer

' Varibles
Dog_x_position = SCREEN_W/2 - Dog->w/2
Dog_y_position = SCREEN_H/2 - Dog->h/2

Stick_x_position = 200
Stick_y_position = 200

Dim Shared Dog_bb_left    as integer
Dim Shared Dog_bb_right   as integer
Dim Shared Dog_bb_top     as integer
Dim Shared Dog_bb_bottom  as integer

Dim Shared Stick_bb_left   as integer
Dim Shared Stick_bb_right  as integer
Dim Shared Stick_bb_top    as integer
Dim Shared Stick_bb_bottom as integer

Dog_x_position   = 0   ' Set the x position of image 1 to 0.
Dog_y_position   = 0   ' Set the y position of image 1 to 0.
Stick_x_position = 100 ' Set the x position of image 2 to 100.
Stick_y_position = 100 ' Set the y position of image 2 to 100.

Dog_bb_left   = Dog_x_position
                           
' The same idea goes for the top "side" */
Dog_bb_top    = Dog_y_position

Dog_bb_right  = (Dog_bb_left + Dog->w)
   
' Use the same idea for the bottom. */
Dog_bb_bottom = (Dog_bb_top + Dog->h)
   
' Now, we will create the box for image 2 using the same principles. */
Stick_bb_left    =  Stick_x_position
Stick_bb_top     =  Stick_y_position
Stick_bb_right   = (Stick_bb_left + Stick->w)
Stick_bb_bottom  = (Stick_bb_top + Stick->h)

Dim Show_bbox as integer
Dim Collision as integer

show_bbox = 1  ' True
collision = 0  ' False


' Timer
LOCK_VARIABLE(speed_counter) ' Used to set the timer - which regulates the game's
LOCK_FUNCTION(increment_speed_counter)' speed.
install_int_ex(@incRement_speed_counter, BPS_TO_TIMER(60))

   
' main loop
While (Not Key(KEY_ESC))
    While(speed_counter > 0)
       
            Move_Player    ' Move the Daft Dog..heh heh
       
           ' Update image 1's bounding box */
         Dog_bb_left     = Dog_x_position
         Dog_bb_top      = Dog_y_position
         Dog_bb_right    = (Dog_bb_left + Dog->w)
         Dog_bb_bottom   = (Dog_bb_top + Dog->h)

         ' Update image 2's bounding box. */
         Stick_bb_left   = Stick_x_position
         Stick_bb_top    = Stick_y_position
         Stick_bb_right  = (Stick_bb_left + Stick->w)
         Stick_bb_bottom = (Stick_bb_top  + Stick->h)
         
         if(Dog_bb_bottom < Stick_bb_top) then
            collision = 1
         else if(Dog_bb_top > Stick_bb_bottom) then
            collision = 1               
         else if(Dog_bb_right < Stick_bb_left) then
            collision = 1               
         else if(Dog_bb_left > Stick_bb_right) then
            collision = 1
         endif
        endif
      endif
   endif
     
   if show_bbox = 1 then
      ' Draw image1's bounding box. */
      line(buffer, Dog_bb_left, Dog_bb_top,    Dog_bb_right, Dog_bb_top   , makecol(255,0,0))
      line(buffer, Dog_bb_left, Dog_bb_bottom, Dog_bb_right, Dog_bb_bottom, makecol(255,0,0))
      line(buffer, Dog_bb_left, Dog_bb_top,    Dog_bb_left,  Dog_bb_bottom, makecol(255,0,0))
      line(buffer, Dog_bb_right,Dog_bb_top,    Dog_bb_right, Dog_bb_bottom, makecol(255,0,0))

      ' Draw image2's bounding box. */
      line(buffer, Stick_bb_left , Stick_bb_top   , Stick_bb_right, Stick_bb_top   , makecol(255,0,0))
      line(buffer, Stick_bb_left , Stick_bb_bottom, Stick_bb_right, Stick_bb_bottom, makecol(255,0,0))
      line(buffer, Stick_bb_left , Stick_bb_top   , Stick_bb_left , Stick_bb_bottom, makecol(255,0,0))
      line(buffer, Stick_bb_right, Stick_bb_top   , Stick_bb_right, Stick_bb_bottom, makecol(255,0,0))         
    endif
   
    if collision = 1 then
       textout(buffer,font,"COLLISIONS! ", 0, 0,15)
    endif
   
        speed_counter -=1
    Wend
    Render_Sprites ' Draw everythings
   
Wend

Shut_down

'------------------------------------------------------------------------------     
' All the functions blow on what I have made!
'------------------------------------------------------------------------------
function Allergo_Setup
    allegro_init
    install_keyboard
    install_timer
    set_color_depth Depth
    set_gfx_mode GFX_AUTODETECT_WINDOWED, SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0
end function

function Load_Images
   ' Background = load_bitmap("GFX\grass.bmp", NULL)
   Dog        = load_bitmap("GFX\Dog.bmp"       , NULL)       
    Stick      = load_bitmap("GFX\Stick.bmp"     , NULL)
    buffer = create_bitmap(640,480)
end function

function Move_Player
      if (key(KEY_UP)) then
          Dog_y_position=Dog_y_position-speed
      endif
     
      if (key(KEY_Down)) then
          Dog_y_position=Dog_y_position+speed
      endif
     
      if (key(KEY_LEFT)) then
          Dog_x_position=Dog_x_position-speed
      endif
     
      if (key(KEY_RIGHT)) then
          Dog_x_position=Dog_x_position+speed
      endif
     
      ' To stop the doggy moving off the player area
      IF Dog_x_position<0    Then Dog_x_position=0
      IF Dog_x_position>=610 Then Dog_x_position=610     
     
      IF Dog_y_position<0    Then Dog_y_position=0
      IF Dog_y_position>=450 Then Dog_x_position=450
       
end function

Function Render_Sprites   
      ' draw the sprite   
      clear_bitmap(buffer)
      ' draw_sprite(buffer, Background, 0,0)     
      draw_sprite(buffer, Dog  ,Dog_x_position,Dog_y_position)
      draw_sprite(buffer, Stick,Stick_x_position, Stick_y_position)
     
      vsync  '<- I added this
     
      blit(buffer, Screen, 0,0,0,0,640,480)   
End Function

'-------------------------------------------------------------------------------
' Timer
'-------------------------------------------------------------------------------
Sub incRement_speed_counter cdecl ()
    speed_counter+=1
End Sub
End_OF_FUNCTION(incRement_speed_counter)


function Shut_down   
    ' delete the bitmap
    destroy_bitmap(buffer)
    'destroy_bitmap(Background)
    destroy_bitmap(dog)
    destroy_bitmap(Stick)
    remove_keyboard
    allegro_exit   
end function
Ophelius
Posts: 428
Joined: Feb 26, 2006 1:57

Postby Ophelius » Apr 05, 2010 0:13

To get a bounding box to work correctly, you have to check if any of Sprite1's opposite corners are inside Sprite2's bounding box, OR if Sprite2's opposite corners are inside Sprite1's bounding box. That's because if you are only checking Sprite1's corners, and if Sprite1 is larger than Sprite2, and if Sprite1 is covering the entire Sprite2, then it won't detect because even though Sprite1 is touching Sprite2, Sprite1's corners aren't. So you also have to check Sprite2's corners as well:

Code: Select all


If ( ((S1.X1 >= S2.X1 AND S1.Y1 >= S2.Y1) AND _
   (S1.X1 <= S2.X2 AND S1.Y1 <= S2.Y2)) OR _
   ((S1.X2 >= S2.X1 AND S1.Y2 >= S2.Y1) AND _
   (S1.X2 <= S2.X2 AND S1.Y2 <= S2.Y2)) ) OR _

   ( ((S2.X1 >= S1.X1 AND S2.Y1 >= S1.Y1) AND _
   (S2.X1 <= S1.X2 AND S2.Y1 <= S1.Y2)) OR _
   ((S2.X2 >= S1.X1 AND S2.Y2 >= S1.Y1) AND _
   (S2.X2 <= S1.X2 AND S2.Y2 <= S1.Y2)) ) THEN Collision = 1



This is 1 entire line. I used the _ symbol to split it up which FB can understand. Draw this on paper to understand it. It helps to see it. There's ways to optimize this, but it's a start. This code is untested.

Edit: Here's a link with more info:
http://www.gamedev.net/reference/articles/article735.asp
Swos2008
Posts: 91
Joined: May 15, 2008 13:21

Postby Swos2008 » Apr 15, 2010 20:38

Thanks Ophelius

I had week break from programming and now I am back to it
I let you know how i get on :)

Return to “Libraries”

Who is online

Users browsing this forum: No registered users and 3 guests