JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
bouncy ship :)
authorJoshua Grams <josh@qualdan.com>
Thu, 23 Mar 2006 14:36:15 +0000 (14:36 +0000)
committerJoshua Grams <josh@qualdan.com>
Thu, 23 Mar 2006 14:36:15 +0000 (14:36 +0000)
main.c
sprite.c
sprite.h

diff --git a/main.c b/main.c
index 097969c..b075f0d 100644 (file)
--- a/main.c
+++ b/main.c
@@ -510,7 +510,10 @@ draw() {
        }
 
        if(state == GAMEPLAY) {
-               bang = collides(SPRITE(&ship));
+               Sprite *r = collides(SPRITE(&ship));
+               if(r) {
+                       bounce(r, SPRITE(&ship));
+               }
        }
 
        ms_frame = SDL_GetTicks() - ms_end;
index 38dae30..45cd0f9 100644 (file)
--- a/sprite.c
+++ b/sprite.c
@@ -1,3 +1,4 @@
+#include <math.h>
 #include <stdlib.h>
 #include <string.h>
 #include "config.h"
@@ -210,20 +211,20 @@ collide(Sprite *a, Sprite *b)
        else return mask_collide(xov, yov, a, b);
 }
 
-int
+Sprite *
 hit_in_square(Sprite *r, Sprite *s)
 {
-       for(; r; r=r->next) {
-               if(collide(r, s)) return true;
-       }
-       return false;
+       for(; r; r=r->next)
+               if(collide(r, s)) break;
+       return r;
 }
 
-int
+Sprite *
 collides(Sprite *s)
 {
        int l, r, t, b;
        Sprite **sq;
+       Sprite *c;
 
        l = (s->x + grid_size) / grid_size;
        r = (s->x + s->w + grid_size) / grid_size;
@@ -231,21 +232,21 @@ collides(Sprite *s)
        b = (s->y + s->h + grid_size) / grid_size;
        sq = &sprites[set][l + t*gw];
 
-       if(hit_in_square(*sq, s)) return true;
-       if(l > 0 && hit_in_square(*(sq-1), s)) return true;
-       if(t > 0 && hit_in_square(*(sq-gw), s)) return true;
-       if(l > 0 && t > 0 && hit_in_square(*(sq-1-gw), s)) return true;
+       if((c = hit_in_square(*sq, s))) return c;
+       if(l > 0 && (c = hit_in_square(*(sq-1), s))) return c;
+       if(t > 0 && (c = hit_in_square(*(sq-gw), s))) return c;
+       if(l > 0 && t > 0 && (c = hit_in_square(*(sq-1-gw), s))) return c;
 
        if(r > l) {
-               if(hit_in_square(*(sq+1), s)) return true;
-               if(t > 0 && hit_in_square(*(sq+1-gw), s)) return true;
+               if((c = hit_in_square(*(sq+1), s))) return c;
+               if(t > 0 && hit_in_square(*(sq+1-gw), s)) return c;
        }
        if(b > t) {
-               if(hit_in_square(*(sq+gw), s)) return true;
-               if(l > 0 && hit_in_square(*(sq-1+gw), s)) return true;
+               if((c = hit_in_square(*(sq+gw), s))) return c;
+               if(l > 0 && (c = hit_in_square(*(sq-1+gw), s))) return c;
        }
-       if(r > l && b > t && hit_in_square(*(sq+1+gw), s)) return true;
-       return false;
+       if(r > l && b > t && (c = hit_in_square(*(sq+1+gw), s))) return c;
+       return NULL;
 }
 
 int
@@ -283,3 +284,20 @@ pixel_collides(float x, float y)
        if(l > 0 && t > 0 && pixel_hit_in_square(*(sq-1-gw), x, y)) return true;
        return false;
 }
+
+void
+bounce(Sprite *a, Sprite *b)
+{
+       float x, y, n;
+       float na, nb;
+
+       x = (b->x + b->w / 2) - (a->x + a->w / 2);
+       y = (b->y + b->h / 2) - (a->y + a->h / 2);
+       n = sqrt(x*x + y*y); x /= n; y /= n;
+
+       na = (x*a->dx + y*a->dy); // sqrt(a->dx*a->dx + a->dy*a->dy);
+       nb = (x*b->dx + y*b->dy); // sqrt(b->dx*b->dx + b->dy*b->dy);
+
+       a->dx += x*(nb-na); a->dy += y*(nb-na);
+       b->dx += x*(na-nb); b->dy += y*(na-nb);
+}
index 4c0cefd..953d303 100644 (file)
--- a/sprite.h
+++ b/sprite.h
@@ -31,10 +31,12 @@ void add_sprite(Sprite *s);
 void move_sprite(Sprite *s);
 void move_sprites(void);
 
-int collides(Sprite *s);
+Sprite *collides(Sprite *s);
 int pixel_collides(float x, float y);
 void load_sprite(Sprite *sprite, char *filename);
 
+void bounce(Sprite *a, Sprite *b);
+
 
 // extended sprites