# bullet detection prob

This is a discussion on bullet detection prob within the Game Programming forums, part of the General Programming Boards category; in my tile game the bullets detection is wrong i use the mouse to get the direction for the bullets ...

1. ## bullet detection prob

in my tile game the bullets detection is wrong
i use the mouse to get the direction for the bullets
but they wont go all the way

here is some code
Code:
```
int pos(int x, int y)
{
return level[(y/TILE_SIZE)][(x/TILE_SIZE)%MAPSIZE];
}

//main()

if(mouse_b & 1&&!fired)
{

fired=1;
bulletx=240;
bullety=240;

skottang = atan2( ( ( 240 ) - ( mouse_y ) )  ,  ( ( 240 ) - ( mouse_x ) ) );  //240 =centerscreen

}
if(fired)
{

bulletx-=5 *cos(skottang);
bullety-=5 *sin(skottang);
}
if(pos(bulletx,bullety)==1)
{
fired = 0;
}```

Code:
```diffx=mousex-bulletstartx;
diffy=mousey-bulletstarty;
dist=FastDist2D(diffx,diffy);

bulletvelx=diffx\(double)dist;
bulletvely=diffy\(double)dist;

bulletx+=(bulletvelx*speed);
bullety+=(bulletvely*speed);

#define MIN(a,b) (if (a<b)?a:b)

int FastDist2D(int dx,int dy)
{
int mn=min(dx,dy);

return (dx+dy-(mn>>1)-(mn>>2)+(mn>>4));
}```

3. Bubba can you explain your code a bit?
when i ran it i got some errors so i would like to know how you do it.

4. It's a normalized vector. Subtract the two coords to get a vector and then normalize it or divide each component by the total distance. You probably got some type conversion errors. If you don't want to use FastDist2D then use sqrt((diffx*diffx)+(diffy*diffy)).

5. but you know shooting the bullets isnt my problem the problem is
the collisions does anyone got an exampleon shooting in a tilemap.
and not just shoot straight forward i mean shooting at all directions from the center of the screen.
ive tried everything and it seems very strange

6. You seem to be confused here. You can shoot in all directions even in a tile map. The only thing you need to figure out is where the bullet is in your tilemap.

starttilerow=playertilerow;
starttilecol=playertilecol;

then when the bullet is moving

bullettilerow=starttilerow+(int)(bulletx/tilesizex);
bullettilecol=starttilecol+(int)(bullety/tilesizey);

This will tell you which tile your bullet is in. For more collision detection simply put a bounding box around your sprites in the tiles and test against that.

My method will allow you to have smooth movement of bullets and at the same time correctly determine a hit or not.

Your method is too tile based. Your bullets would track very ugly and choppy.

So here is some code to help:

Code:
```void FireBullet(Player *thePlayer)
{

//Extract x and y components of angle

//Create new bullet with components
Bullet *theBullet=new Bullet(bx,by);

//Set bullet speed
theBullet->SetSpeed(2.0);

//Set bullet to active so it will be updated
theBullet->SetActive(TRUE);

//Add new bullet to bullet list

}

void FireBulletAt(Player *theShooter,NPC *theTarget)
{
//Get NPCs location
double npcx=theTarget->GetX();
double npcy=theTarget->GetY();

//Get Player's location
double px=theShooter->GetX();
double py=theShooter->GetY();

//Get differences of components
double diffx=px-npcx;
double diffy=py-npcy;

//Compute total distance
int dist=FastDist2D(diffx,diffy);

//Compute bullet components
double bx=diffx/(double)dist;
double by=diffy/(double)dist;

//Create new bullet
Bullet *theBullet=new Bullet(bx,by);

//Activate it
theBullet->SetActive(TRUE);

}```
Then to test for collisions you could extract the tile positions of each bullet.

int column=Bullet->Extract(COLUMN);
int row=Bullet->Extract(ROW);

or something like that.

Personally I would stick with the x,y coords of each object that way you don't have to know which tile the bullet is in. Just because it's a tile map does not mean that everything is tile based. Your sprites can still have x and y coordinates and this is what you should test against.

If you are shooting bullets at the mouse -> the mouse would be the NPC or the target. To make you sprite turn to face what you are shooting at is simply done by comparing the mouse x,y to your player's x,y:

if (mousex<playerx && mousey<playery) Player->SetPicture(NORTHWEST);
...

and so on for each of the 8 directions. For a perfect angle using 3D characters and exact angles -> you already know how to get the angle using arctangent.

7. thanks man youve have helped alot.

8. No problem.

By the way your sprite x and y's are found by taking the starting x,y of your grid and doing this:

Code:
```spritex=mapstartx+(spritecolumn*cellsizex);
spritey=mapstarty+(spriterow*cellsizey);```

9. how do you mean?

10. how can i find the "spritecolumn/row" and the "cellsizex/y"?

11. You already know the sprite column and row. It's where the sprite or character is in your tile map. Your cellsize is how big each tile is on x and y - usually the same value.

I assume your tile map is a 2D array.

12. thanks Bubba.
code too, after you had requested it.
since you didnt reply to it i wonder if you saw it again.
the reason im asking you is that after all your help i still cant find
a solution to the problem. so i thought maybe if you run the game you will find one.
thanks

13. So this is a text game?? I'm lost.

Can you run small DOS apps? I could show you this real quick in code. I can show you in DirectX as well but I don't want to confuse you with all the DX and COM crap.

Doggone this XP era crap. Impossible for me to explain and show you what's going on because no DOS in XP and DirectX would only serve to confuse you.

Perhaps I'll code a small DOS emulator for XP - better than the one provided. Off to flashdaddee's assembly board to see if my idea is even possible or not.

14. no it isnt a text game i posted the entire code at the bottom of the thread.
anyway that code is old ill show you the game instead.
and btw dont you use allegro?

here is the exe.

15. Missing some dll.

Page 1 of 2 12 Last