JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
added area back to get_shape, bounce() uses area.
authorJoshua Grams <josh@qualdan.com>
Thu, 23 Mar 2006 15:40:06 +0000 (15:40 +0000)
committerJoshua Grams <josh@qualdan.com>
Thu, 23 Mar 2006 15:40:06 +0000 (15:40 +0000)
fixed 'sticking to rocks' bug in bounce().

sprite.c
sprite.h

index a772668..ec9fb01 100644 (file)
--- a/sprite.c
+++ b/sprite.c
@@ -33,6 +33,7 @@ get_shape(Sprite *s)
        uint16_t *px, transp;
        uint32_t bits = 0, bit, *p;
 
        uint16_t *px, transp;
        uint32_t bits = 0, bit, *p;
 
+       s->area = 0;
        if(s->image->format->BytesPerPixel != 2) {
                fprintf(stderr, "get_shape(): not a 16-bit image!\n");
                exit(1);
        if(s->image->format->BytesPerPixel != 2) {
                fprintf(stderr, "get_shape(): not a 16-bit image!\n");
                exit(1);
@@ -55,7 +56,7 @@ get_shape(Sprite *s)
                bit = 0;
                for(x=0; x<s->image->w; x++) {
                        if(!bit) { bits = 0; bit = 0x80000000; }
                bit = 0;
                for(x=0; x<s->image->w; x++) {
                        if(!bit) { bits = 0; bit = 0x80000000; }
-                       if(*px++ != transp) { bits |= bit; }
+                       if(*px++ != transp) { bits |= bit; s->area++; }
                        bit >>= 1;
                        if(!bit || x == s->image->w - 1) { *(p++) = bits; }
                }
                        bit >>= 1;
                        if(!bit || x == s->image->w - 1) { *(p++) = bits; }
                }
@@ -285,19 +286,35 @@ pixel_collides(float x, float y)
        return false;
 }
 
        return false;
 }
 
+
+float
+sprite_mass(Sprite *s)
+{
+       if(s->type == SHIP_SPRITE) return s->area;
+       else if(s->type == ROCK_SPRITE) return 3*s->area;
+       else return 0;
+}
+
 void
 bounce(Sprite *a, Sprite *b)
 {
        float x, y, n;
 void
 bounce(Sprite *a, Sprite *b)
 {
        float x, y, n;
-       float na, nb;
+       float da, db;
+       float ma, mb, mr;
 
 
+       // (x, y) is unit vector pointing from A's center to B's center.
        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;
 
        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);
+       // velocities along (x, y), or 0 if already moving away.
+       da = max(x*a->dx + y*a->dy, 0);
+       db = min(x*b->dx + y*b->dy, 0);
+
+       // mass ratio
+       ma = sprite_mass(a); mb = sprite_mass(b);
+       if(ma && mb) mr = mb/ma; else mr = 1;
 
 
-       a->dx += x*(nb-na); a->dy += y*(nb-na);
-       b->dx += x*(na-nb); b->dy += y*(na-nb);
+       a->dx += x*(db*mr - da); a->dy += y*(db*mr - da);
+       b->dx += x*(da/mr - db); b->dy += y*(da/mr - db);
 }
 }
index 953d303..e1216cf 100644 (file)
--- a/sprite.h
+++ b/sprite.h
@@ -22,6 +22,7 @@ struct sprite {
        int w, h;
        int mask_w;
        uint32_t *mask;
        int w, h;
        int mask_w;
        uint32_t *mask;
+       uint32_t area;
 };
 
 Sprite *free_sprites[N_TYPES];  // lists of free sprites, by type.
 };
 
 Sprite *free_sprites[N_TYPES];  // lists of free sprites, by type.
@@ -50,6 +51,7 @@ struct ship {
        int w, h;
        int mask_w;
        uint32_t *mask;
        int w, h;
        int mask_w;
        uint32_t *mask;
+       uint32_t area;
        // SHIP extras
        int lives;
        int jets;
        // SHIP extras
        int lives;
        int jets;
@@ -65,6 +67,7 @@ struct rock {
        int w, h;
        int mask_w;
        uint32_t *mask;
        int w, h;
        int mask_w;
        uint32_t *mask;
+       uint32_t area;
        // ROCK extras
        int type;
 };
        // ROCK extras
        int type;
 };