mac-10
Still Fresh
Hi
I am having a bit of a problem will my collision. I am putting a simple space shooter together to help me get into the SDL. You’ll have to forgive for my C code this is the first C I have ever produced and I haven't got my 2X yet have it a Xmas gift so I have to wait.
The problem is the collision code is not running the way I expected it. Basically the bullets are hitting the sprites the enemies it seems really imprecise. I can’t tell if it the way I am looping through the sprites or the collide func.
here is the code:
Thanks
Mac
I am having a bit of a problem will my collision. I am putting a simple space shooter together to help me get into the SDL. You’ll have to forgive for my C code this is the first C I have ever produced and I haven't got my 2X yet have it a Xmas gift so I have to wait.
The problem is the collision code is not running the way I expected it. Basically the bullets are hitting the sprites the enemies it seems really imprecise. I can’t tell if it the way I am looping through the sprites or the collide func.
here is the code:
Code:
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include "SDL/SDL.h"
#define PHYSICSFPS 90
int gLastTick;
int shipSpeed = 2;
int shipX = 20, shipY = 120;
int shipMaxFrame = 3;
int Max_Main_Bullets = 10;
int Max_Small_Bullets_1 = 10;
int Max_Small_Bullets_2 = 10;
int MaxEmenies = 10;
int Max_Back_Grounds = 7;
int exsFrame = 0;
int gKeyLeft = 0;
int gKeyRight = 0;
int gKeyUp = 0;
int gKeyDown = 0;
int i = 0;
int j = 0;
SDL_Surface *screen, *backgroundimage, *shipimage ,*bulletimage, *flameimage, *enemyimage;
typedef struct EnemySprite
{
int ObjNo, X, Y, Width, Height, Active, Frame;
}EnemySprite;
EnemySprite enemy[20];
typedef struct BulletSprite
{
int ObjNo, X, Y, Width, Height, Active;
}BulletSprite;
BulletSprite bullet[10];
typedef struct bgElements
{
int Frame, X, Y, Active;
}bgElements;
bgElements background[7];
// Load in an image and convert it to the display format
// for faster blitting.
SDL_Surface * ImageLoad(char *file)
{
SDL_Surface *temp1, *temp2;
temp1 = SDL_LoadBMP(file);
temp2 = SDL_DisplayFormat(temp1);
SDL_FreeSurface(temp1);
return temp2;
}
void addBullet(void){
int flag = 1;
for(i=1;i < Max_Main_Bullets; i++)
{
if(flag && bullet[i].Active == 0){
bullet[i].Active = 1;
flag = 0;
bullet[i].X = shipX + 45;
bullet[i].Y = shipY + 15;
}
}
}
void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}
void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_Rect dest2;
dest2.x = x2;
dest2.y = y2;
dest2.w = w;
dest2.h = h;
SDL_BlitSurface(img, &dest2, screen, &dest);
}
void animateflame(){
exsFrame += 1;
if(exsFrame > 3) exsFrame = 0;
DrawIMG(flameimage,shipX - 8,shipY,22,26,21*exsFrame,0);
}
/* Find out if we hint anything */
short int Sprite_Collide( int sprite1X , int sprite1Width , int sprite1Y , int sprite1Height, int sprite2X , int sprite2Width , int sprite2Y , int sprite2Height )
{
double left1, left2;
double right1, right2;
double top1, top2;
double bottom1, bottom2;
left1 = sprite1X+sprite1Width*0.1;
left2 = sprite2X+sprite2Width*0.1;
top1 = sprite1Y+sprite1Height*0.1;
top2 = sprite2Y+sprite2Height*0.1;
right1 = sprite1X+sprite1Width*0.9;
right2 = sprite2X+sprite2Width*0.9;
bottom1 = sprite1Y+sprite1Height*0.9;
bottom2 = sprite2Y+sprite2Height*0.9;
if (bottom1 < top2) return 0;
if (top1 > bottom2) return 0;
if (right1 < left2) return 0;
if (left1 > right2) return 0;
return 1;
}
/* start things */
void init(){
for(i=1;i < Max_Main_Bullets; i++)
{
bullet[i].X = 0;
bullet[i].Y = 0;
bullet[i].Active = 0;
bullet[i].ObjNo = i;
bullet[i].Width = 9;
bullet[i].Height = 9;
}
for(i=1;i < Max_Back_Grounds; i++)
{
background[i].X = rand()%500+52;
background[i].Y = rand()%160;
background[i].Active = 1;
background[i].Frame = rand()%4;
}
for(i=1;i < MaxEmenies; i++)
{
enemy[i].X = rand()%500+400;
enemy[i].Y = rand()%160;
enemy[i].Active = 1;
enemy[i].ObjNo = i;
enemy[i].Frame = rand()%4;
bullet[i].Width = 60;
bullet[i].Height = 60;
}
shipimage = ImageLoad("data/ship/ship.bmp");
flameimage = ImageLoad("data/ship/exs.bmp");
bulletimage = ImageLoad("data/bullet/bullet.bmp");
backgroundimage = ImageLoad("data/bg/background.bmp");
enemyimage = ImageLoad("data/enemy.bmp");
SDL_SetColorKey(shipimage, SDL_SRCCOLORKEY,SDL_MapRGB(shipimage->format, 0, 255, 0));
SDL_SetColorKey(flameimage, SDL_SRCCOLORKEY,SDL_MapRGB(flameimage->format, 0, 255, 0));
SDL_SetColorKey(bulletimage, SDL_SRCCOLORKEY,SDL_MapRGB(bulletimage->format, 0, 255, 0));
SDL_SetColorKey(enemyimage, SDL_SRCCOLORKEY,SDL_MapRGB(enemyimage->format, 0, 0, 0));
SDL_SetColorKey(backgroundimage, SDL_SRCCOLORKEY,SDL_MapRGB(backgroundimage->format, 0, 0, 0));
gLastTick = SDL_GetTicks();
}
void bullethitenemies(){
for(i=1;i < Max_Main_Bullets; i++){
if(bullet[i].Active){
for(j=1;j < MaxEmenies; j++){
if( enemy[j].Active ){
if(Sprite_Collide( bullet[i].X , bullet[i].Width , bullet[i].Y , bullet[i].Height, enemy[j].X , enemy[j].Width , enemy[j].Y , enemy[j].Height )){
bullet[i].Active=0;
enemy[j].Active=0;
}// if
}// if
}// for
}// if
}// for
}
void render(){
while(SDL_GetTicks() - gLastTick <= 1000/PHYSICSFPS);
bullethitenemies();
if (gKeyLeft) shipX -= shipSpeed;
if (gKeyRight) shipX += shipSpeed;
if (gKeyUp) shipY -= shipSpeed;
if (gKeyDown) shipY += shipSpeed;
if(shipX > 280) shipX = 280;
if(shipX < 1) shipX = 1;
if(shipY > 215) shipY = 215;
if(shipY < 1) shipY = 1;
SDL_FillRect(screen,0,0);
for(i=1;i < Max_Back_Grounds; i++)
{
if(background[i].Active){
background[i].X = background[i].X - 1;
DrawIMG(backgroundimage, background[i].X ,background[i].Y,52,88,52*background[i].Frame,0);
if(background[i].X < -52){
background[i].X=320+rand()%10;
background[i].Frame = rand()%5;
background[i].Y = rand()%160;
}
}
}
for(i=1;i < Max_Main_Bullets; i++)
{
if(bullet[i].Active){
bullet[i].X = bullet[i].X + 1;
DrawIMG(bulletimage, bullet[i].X ,bullet[i].Y,9,9,9*3,0);
if(bullet[i].X > 300) bullet[i].Active=0;
}
}
for(i=1;i < MaxEmenies; i++)
{
enemy[i].X = enemy[i].X - 1;
if(enemy[i].Active){
DrawIMG(enemyimage, enemy[i].X ,enemy[i].Y,34,33,34*enemy[i].Frame,0);
}
if(enemy[i].X < -52){
enemy[i].X=320+rand()%10;
enemy[i].Y = rand()%160;
enemy[i].Active = 1;
}
}
animateflame();
DrawIMG(shipimage,shipX,shipY,43,29,42*2,0);
SDL_Flip(screen);
gLastTick = SDL_GetTicks();
}
int main (int argc, char *argv[])
{
char *msg;
int done;
srand(time(NULL));
Uint8* keys;
/* Initialize SDL */
if (SDL_Init (SDL_INIT_VIDEO) < 0)
{
sprintf (msg, "Couldn't initialize SDL: %s\n", SDL_GetError ());
free (msg);
exit (1);
}
atexit (SDL_Quit);
screen = SDL_SetVideoMode (320, 240, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);
if (screen == NULL)
{
sprintf (msg, "Couldn't set 320x240x16 video mode: %s\n",SDL_GetError ());
free (msg);
exit (2);
}
SDL_WM_SetCaption ("GET Lose", NULL);
/* Initialize Data */
init();
done = 0;
while (!done)
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_LEFT:
gKeyLeft = 1;
break;
case SDLK_RIGHT:
gKeyRight = 1;
break;
case SDLK_UP:
gKeyUp = 1;
break;
case SDLK_DOWN:
gKeyDown = 1;
break;
case SDLK_SPACE:
addBullet();
break;
}
break;
case SDL_KEYUP:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
return 0;
case SDLK_LEFT:
gKeyLeft = 0;
break;
case SDLK_RIGHT:
gKeyRight = 0;
break;
case SDLK_UP:
gKeyUp = 0;
break;
case SDLK_DOWN:
gKeyDown = 0;
break;
}
break;
case SDL_QUIT:
done = 1;
break;
default:
break;
}
}
render();
}
return 0;
}
Thanks
Mac