JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
bbox collision detection.
authorJoshua Grams <josh@qualdan.com>
Thu, 7 Apr 2005 01:07:53 +0000 (01:07 +0000)
committerJoshua Grams <josh@qualdan.com>
Thu, 7 Apr 2005 01:07:53 +0000 (01:07 +0000)
Makefile
main.c
shape.c
shape.h

index 7a2fde7..bc7c6ad 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -17,7 +17,7 @@
 
 debug := $(if $(DEBUG),1,0)
 ldflags := $(shell sdl-config --libs) -lSDL_image -lSDL_mixer
-cflags := $(shell sdl-config --cflags) -Wall -DDEBUG=$(debug)
+cflags := $(shell sdl-config --cflags) -Wall -DDEBUG=$(debug) -ggdb
 
 my_objects := file.o score.o shape.o sound.o main.o $(if $(DEBUG),debug.o)
 libs := SFont.o
diff --git a/main.c b/main.c
index aab0d71..ad3ab56 100644 (file)
--- a/main.c
+++ b/main.c
@@ -113,6 +113,7 @@ struct spacedot sdot[MAXSPACEDOTS];
 char topline[1024];
 char *initerror = "";
 
+struct shape shipshape;
 float shipx,shipy = 240.0;     // X position, 0..XSIZE
 float shipdx,shipdy;   // Change in X position per tick.
 float rockrate,rockspeed;
@@ -543,6 +544,7 @@ init(int fullscreen) {
        // Load the spaceship graphic.
        NULLERROR(temp = IMG_Load(add_path("sprites/ship.png")));
        NULLERROR(surf_ship = SDL_DisplayFormat(temp));
+       get_shape(surf_ship, &shipshape);
 
        // Load the life indicator (small ship) graphic.
        NULLERROR(temp = IMG_Load(add_path("indicators/life.png")));
@@ -734,6 +736,13 @@ draw() {
        }
 
        if(!gameover && state == GAMEPLAY) {
+               for(i=0; i<MAXROCKS; i++) {
+                       if(rock[i].active) {
+                               if(collide(shipx-rock[i].x, shipy-rock[i].y, rock[i].shape, &shipshape)) 
+                                       bang = 1;
+                       }
+               }
+               /*
                SDL_LockSurface(surf_screen);
                raw_pixels = (Uint16 *) surf_screen->pixels;
                // Check that the black points on the ship are
@@ -746,6 +755,7 @@ draw() {
                        }
                }
                SDL_UnlockSurface(surf_screen);
+               */
        }
 
        // Draw all the little ships
@@ -886,6 +896,7 @@ gameloop() {
                                        rockptr->dy = rnd()-0.5;
                                        rockptr->type_number = random() % NROCKS;
                                        rockptr->image = surf_rock[rockptr->type_number];// [random()%NROCKS];
+                                       rockptr->shape = &rock_shapes[rockptr->type_number];
                                        rockptr->active = 1;
                                        rockptr->y = rnd()*(YSIZE + rockptr->image->h);
                                }
diff --git a/shape.c b/shape.c
index 9d0501c..92914cb 100644 (file)
--- a/shape.c
+++ b/shape.c
@@ -52,12 +52,11 @@ collide(int xdiff, int ydiff, struct shape *r, struct shape *s)
        int xov, yov;
 
        if(xdiff >= 0) xov = max(min(r->w-xdiff, s->w), 0);
-       else xov = -max(min(s->w+xdiff, r->w), 0);
+       else xov = min(-min(s->w+xdiff, r->w), 0);
 
        if(ydiff >= 0) yov = max(min(r->h-ydiff, s->h), 0);
-       else yov = -max(min(s->h+ydiff, r->h), 0);
+       else yov = min(-min(s->h+ydiff, r->h), 0);
 
-       if(xov == 0 && yov == 0) return 0;
-
-       return 0;
+       if(xov == 0 || yov == 0) return 0;  // bboxes hit?
+       else return 1;
 }
diff --git a/shape.h b/shape.h
index c9a43fa..a76951d 100644 (file)
--- a/shape.h
+++ b/shape.h
@@ -2,8 +2,8 @@
 #include <stdint.h>
 
 struct shape {
-       uint32_t w, h;
-       uint32_t mw;
+       int w, h;
+       int mw;
        uint32_t *mask;
 };