I realize my code is C but the section I have questions about is the same as C++ (I think).
I am having problems registering a hit when a shot is at point (1,1) and an asteroid is at point (0,0).
I am using a Makefile that looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
CFLAGS = -g
LDFLAGS += -lm -lglut -lGL
# need these flags on OSX
#CFLAGS += -I/usr/local/include
#LDFLAGS += -L/usr/local/lib -lglut -framework OpenGL
SRC = asteroids.c rgbcolors.c graphui.c
all: roids
asteroids: $(SRC)
gcc $(CFLAGS) $(SRC) -o asteroids $(LDFLAGS)
clean:
rm -f asteroids *.o
|
The screen size is 1 by 1 with the top right being (1,1) and bottom left being (0,0). The ship starts with its center at (0.5, 0.5). I used the 8 ghost ships algorithm (each one screen length and or width away at N,S,E,W and NE, NW, SW, SE positions relative to center ship) for the toroidal effect (basically adding or subtracting 1.0 to the (x,y) coordinates of the center ship depending on position of ghost ship as described). Same thing was done for the asteroids. However, I am having problems with the collision detection between shots and asteroids in that a shot at (1,1) will not hit an asteroid at (0,0).
The conditional statement will return 1 (true) if it is a hit and 0 (false) if it is a miss.
Please take a look below and see why a shot at (1,1) will not hit an asteroid at (0,0).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
int i=0, j=0;
//covers 9 cases for 9 worlds but still not passing shot at (1,1) and roid at (0,0)
for(i=-1; i<=1;i++)
{
for(j=-1; j<=1;j++)
{
if((shot->x+i) >= (roid->x+i)-(roid->width/2.0) &&
(shot->x+i) <= (roid->x+i) + (roid->width/2.0) &&
(shot->y+j) >= (roid->y+j) - (roid->height/2.0) &&
(shot->y+j) <= (roid->y+j) + (roid->height/2.0))
{
return 1;
}
else
continue;
}
}
//try the corners
//if a shot is in any corner and a roid is in any corner, it should register a hit
if(shot->x==0 && shot->y==0 || shot->x == 0 && shot->y==1 || shot->x==1 && shot->y==0 || shot->x==1 || shot->y==1)//|| shot->x==1 && shot->y==0
{
if(roid->x ==0 && roid->y==0 || roid->x==0 && roid->y==1 || roid->x==1 && roid->y==0 || roid->x==1 && roid->y ==1)
{
return 1;
}
}
return 0;
|