# bullet detection prob

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-28-2003
pode
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;         }```
• 10-29-2003
VirtualAce

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)); }```
• 10-29-2003
pode
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.
• 10-30-2003
VirtualAce
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)).
• 10-31-2003
pode
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
• 10-31-2003
VirtualAce
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) {   //Convert player's angle to radians   double radangle=DEGTORAD(thePlayer->GetAngle());     //Extract x and y components of angle   double bx=cos(radangle);   double by=sin(radangle);     //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   BulletTracker->Add(theBullet); } 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);    //Add to list   BulletTracker->AddBullet(theBullet); }```
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.
• 11-01-2003
pode
thanks man youve have helped alot.
• 11-01-2003
VirtualAce
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);```
• 11-01-2003
pode
how do you mean?
• 11-01-2003
pode
how can i find the "spritecolumn/row" and the "cellsizex/y"?
• 11-01-2003
VirtualAce
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.
• 11-02-2003
pode
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
• 11-02-2003
VirtualAce
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.
• 11-02-2003
pode
no it isnt a text game i posted the entire code at the bottom of the thread.:D
anyway that code is old ill show you the game instead.
and btw dont you use allegro?

here is the exe.
• 11-04-2003
[kcb] Ch33s3
Missing some dll.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last