Thread: bullet detection prob

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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 04:23 PM.

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