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. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    479

    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;
    	}

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    Use this instead:

    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. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    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. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    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. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    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. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    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.
    Last edited by VirtualAce; 10-31-2003 at 03:23 PM.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    thanks man youve have helped alot.

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    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. #9
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    how do you mean?

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    how can i find the "spritecolumn/row" and the "cellsizex/y"?

  11. #11
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    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. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    thanks Bubba.
    btw Bubba i had a nother thread about this where i posted my
    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.
    here is the thread
    i was talking about.
    thanks

  13. #13
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    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.
    Last edited by VirtualAce; 11-02-2003 at 08:25 AM.

  14. #14
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    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.
    Attached Files Attached Files

  15. #15
    Registered User [kcb] Ch33s3's Avatar
    Join Date
    Nov 2003
    Posts
    7
    Missing some dll.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deal or No Deal listbox prob
    By kryptkat in forum Windows Programming
    Replies: 5
    Last Post: 03-30-2009, 06:53 PM
  2. pic movement
    By pode in forum Game Programming
    Replies: 31
    Last Post: 08-21-2002, 09:30 PM
  3. bounding box collision detection
    By DavidP in forum Game Programming
    Replies: 7
    Last Post: 07-07-2002, 11:43 PM
  4. Allegro help-o
    By Vicious in forum Game Programming
    Replies: 109
    Last Post: 06-11-2002, 08:38 PM
  5. Replies: 4
    Last Post: 05-03-2002, 09:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21