JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
bump version to 0.5.8
[vor.git] / sprite.c
index aadf22d..923e967 100644 (file)
--- a/sprite.c
+++ b/sprite.c
@@ -1,7 +1,7 @@
 #include <math.h>
 #include <stdlib.h>
 #include <string.h>
-#include "config.h"
+#include "vorconfig.h"
 #include "common.h"
 #include "globals.h"
 #include "sprite.h"
@@ -48,7 +48,7 @@ get_shape(Sprite *s)
                exit(1);
        }
 
-       SDL_LockSurface(s->image);
+       if(SDL_MUSTLOCK(s->image)) { SDL_LockSurface(s->image); }
        px = s->image->pixels;
        transp = s->image->format->colorkey;
        p = s->mask;
@@ -62,7 +62,7 @@ get_shape(Sprite *s)
                }
                px = (uint16_t *) ((uint8_t *) px + s->image->pitch - 2*s->image->w);
        }
-       SDL_UnlockSurface(s->image);
+       if(SDL_MUSTLOCK(s->image)) { SDL_UnlockSurface(s->image); }
 }
 
 
@@ -88,8 +88,8 @@ init_sprites(void)
        load_sprites();
 
        grid_size = grid_size * 3 / 2;
-       gw = (XSIZE-1 + 2*grid_size) / grid_size;
-       gh = (YSIZE-1 + 2*grid_size) / grid_size;
+       gw = (XSIZE + 2*grid_size) / grid_size; // -grid-size to XSIZE inclusive (so sprites can be just off either edge)
+       gh = (YSIZE + 2*grid_size) / grid_size;
 
        sprites[0] = malloc(2 * gw * gh * sizeof(Sprite *));
        sprites[1] = (void *)sprites[0] + gw * gh * sizeof(Sprite *);
@@ -105,6 +105,10 @@ static inline Sprite **
 square(int x, int y, int set)
 {
        int b = (x+grid_size)/grid_size + gw*((y+grid_size)/grid_size);
+       if(b >= gw*gh || b < 0) {
+               fprintf(stderr, "square(%i, %i, %i) = %i\n", x, y, set, b);
+               ((int*)0)[0] = 0;
+       }
        return &sprites[set][b];
 }
 
@@ -165,6 +169,8 @@ move_sprites(void)
 }
 
 
+// xov: number of bits of overlap
+// bit: number of bits in from the left edge of amask where bmask is
 static int
 line_collide(int xov, unsigned bit, uint32_t *amask, uint32_t *bmask)
 {
@@ -182,6 +188,8 @@ line_collide(int xov, unsigned bit, uint32_t *amask, uint32_t *bmask)
        return false;
 }
 
+// xov: number of bits/pixels of horizontal overlap
+// yov: number of bits/pixels of vertical overlap
 static int
 mask_collide(int xov, int yov, Sprite *a, Sprite *b)
 {
@@ -195,8 +203,8 @@ mask_collide(int xov, int yov, Sprite *a, Sprite *b)
                bmask = b->mask;
        } else {
                yov = -yov;
-               amask = a->mask;
-               bmask = b->mask + ((b->h - yov) * b->mask_w) + word;
+               amask = a->mask + word;
+               bmask = b->mask + ((b->h - yov) * b->mask_w);
        }
 
        for(y=0; y<yov; y++) {
@@ -250,48 +258,12 @@ collisions(void)
        }
 }
 
-Sprite *
-hit_in_square(Sprite *r, Sprite *s)
-{
-       for(; r; r=r->next)
-               if(collide(r, s)) break;
-       return r;
-}
-
-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;
-       t = (s->y + grid_size) / grid_size;
-       b = (s->y + s->h + grid_size) / grid_size;
-       sq = &sprites[set][l + t*gw];
-
-       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((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((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 && (c = hit_in_square(*(sq+1+gw), s))) return c;
-       return NULL;
-}
-
 int
 pixel_collide(Sprite *s, int x, int y)
 {
        uint32_t pmask;
+
+       if(!COLLIDES(s)) return false;
        
        if(x < s->x || y < s->y || x >= s->x + s->w || y >= s->y + s->h) return 0;
 
@@ -300,59 +272,147 @@ pixel_collide(Sprite *s, int x, int y)
        return s->mask[(y*s->mask_w) + (x>>5)] & pmask;
 }
 
-int
+Sprite *
 pixel_hit_in_square(Sprite *r, float x, float y)
 {
        for(; r; r=r->next) {
-               if(COLLIDES(r) && pixel_collide(r, x, y)) return 1;
+               if(COLLIDES(r) && pixel_collide(r, x, y)) return r;
        }
        return 0;
 }
 
-int
+Sprite *
 pixel_collides(float x, float y)
 {
        int l, t;
        Sprite **sq;
+       Sprite *ret;
 
        l = (x + grid_size) / grid_size; t = (y + grid_size) / grid_size;
        sq = &sprites[set][l + t*gw];
-       if(pixel_hit_in_square(*sq, x, y)) return true;
-       if(l > 0 && pixel_hit_in_square(*(sq-1), x, y)) return true;
-       if(t > 0 && pixel_hit_in_square(*(sq-gw), x, y)) return true;
-       if(l > 0 && t > 0 && pixel_hit_in_square(*(sq-1-gw), x, y)) return true;
-       return false;
+       if((ret = pixel_hit_in_square(*sq, x, y))) return ret;
+       if(l > 0 && (ret = pixel_hit_in_square(*(sq-1), x, y))) return ret;
+       if(t > 0 && (ret = pixel_hit_in_square(*(sq-gw), x, y))) return ret;
+       if(l > 0 && t > 0 && (ret = pixel_hit_in_square(*(sq-1-gw), x, y))) return ret;
+       return 0;
 }
 
 
-static float
+float
 sprite_mass(Sprite *s)
 {
        if(s->type == SHIP) return s->area;
-       else if(s->type == ROCK) return 3*s->area;
+       else if(s->type == ROCK) return 3 * s->area;
        else return 0;
 }
 
+/*
+ * BOUNCE THEORY
+ *
+ * ******************  In 1 Dimension  *****************
+ *
+ * For now we will imagine bouncing A and B off each other in 1 dimension (along
+ * a line). We can safely save the other dimension for later.
+ *
+ * A and B are the same weight, and are both traveling 1m/sec, to collide right
+ * at the origin. With perfect bounciness, their full momentum is reversed.
+ *
+ * If we cut the weight of A down by half, then the center of our colision will
+ * drift towards A (the speeds of A and B are not simply reversed as in our last
+ * example.) However, there is always a place between A and B on the line (I'll
+ * call it x) such that the speeds of A and B relative to x, are simply
+ * reversed. Thus we can find the new speed for A like so:
+ *
+ *     new A = x -(A - x)
+ *
+ *     new B = x -(B - x)
+ * 
+ * or, simply:
+ *
+ *     new A = 2x - A
+ *
+ *     new B = 2x - B
+ *
+ *
+ * this point x is the sort of center of momentum. If, instead of bouncing, A
+ * and B just globbed together, x would be center of the new glob.
+ *
+ * x is the point where there's an equal amount of force coming in from both
+ * sides. ie the weighted average of the speeds of A and B.
+ *
+ * average force = (A force + B force) / total mass
+ *
+ * x.speed = (a.speed * a.mass + b.speed * b.mass) / (a.mass + b.mas)
+ *
+ * then we apply the formula above for calculating the new A and B.
+ *
+ *
+ *
+ *
+ * ******************  In 2 Dimensions  *****************
+ *
+ * OK, that's how we do it in 1D. Now we need to deal with 2D.
+ * 
+ * Imagine (or draw) the two balls just as they are bouncing off each other.
+ * Imagine drawing a line through the centers of the balls. The balls are
+ * exerting force on each other only along this axis. So if we rotate
+ * everything, we can do our earlier 1D math along this line.
+ *
+ * It doesn't matter what direction the balls are going in, they only exert
+ * force on each other along this line. What we will do is to compute the part
+ * of the balls' momentum that is going along this line, and bounce it according
+ * to our math above. The other part is unaffected by the bounce, and we can
+ * just leave it alone.
+ *
+ * To get this component of the balls' momentum, we can use the dot product.
+ *
+ *     dot(U, V) = length(U) * length(V) * cos(angle between U and V)
+ *
+ * If U is a length 1 vector, then dot(U, V) is the length of the component of V
+ * in the direction of U.  So the components of V are:
+ *
+ *     U * dot(U, V)      parallel to U
+ *
+ *     V - U * dot(U, V)  perpendicular to U
+ *
+ * To do the actual bounce, we compute the unit vector between the center of the
+ * two balls, compute the components of the balls' speeds along this vector (A
+ * and B), and then bounce them according to the math above:
+ *
+ *     new A = 2x - A
+ *
+ *     new B = 2x - B
+ *
+ * But we rewrite it in relative terms:
+ *
+ *     new A = A + 2(x-A)
+ *
+ *     new B = B + 2(x-B)
+ */
+
 void
 bounce(Sprite *a, Sprite *b)
 {
-       float x, y, n;
-       float va, vb;
-       float ma, mb, mr;
+       float x, y, n;  // (x, y) is unit vector from a to b.
+       float va, vb;   // va, vb are balls' speeds along (x, y)
+       float ma, mb;   // ma, mb are the balls' masses.
+       float vc;       // vc is the "center of momentum"
 
        // (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;
 
-       // velocities along (x, y), or 0 if already moving away.
-       va = max(x*a->dx + y*a->dy, 0);
-       vb = min(x*b->dx + y*b->dy, 0);
+       // velocities along (x, y)
+       va = x*a->dx + y*a->dy;
+       vb = x*b->dx + y*b->dy;
+       if(vb-va > 0) return;  // don't bounce if we're already moving away.
 
-       // mass ratio
+       // get masses and compute "center" speed
        ma = sprite_mass(a); mb = sprite_mass(b);
-       if(ma && mb) mr = mb/ma; else mr = 1;
+       vc = (va*ma + vb*mb) / (ma+mb);
 
-       a->dx += x*(mb*vb - ma*va)/ma; a->dy += y*(mb*vb - ma*va)/ma;
-       b->dx += x*(ma*va - mb*vb)/mb; b->dy += y*(ma*va - mb*vb)/mb;
+       // bounce off the center speed.
+       a->dx += 2*x*(vc-va); a->dy += 2*y*(vc-va);
+       b->dx += 2*x*(vc-vb); b->dy += 2*y*(vc-vb);
 }