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
int pos(int x, int y)
if(mouse_b & 1&&!fired)
skottang = atan2( ( ( 240 ) - ( mouse_y ) ) , ( ( 240 ) - ( mouse_x ) ) ); //240 =centerscreen
fired = 0;
Use this instead:
#define MIN(a,b) (if (a<b)?a:b)
int FastDist2D(int dx,int dy)
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.
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)).
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
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.
then when the bullet is moving
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:
Then to test for collisions you could extract the tile positions of each bullet.
void FireBullet(Player *thePlayer)
//Convert player's angle to radians
//Extract x and y components of angle
//Create new bullet with components
Bullet *theBullet=new Bullet(bx,by);
//Set bullet speed
//Set bullet to active so it will be updated
//Add new bullet to bullet list
void FireBulletAt(Player *theShooter,NPC *theTarget)
//Get NPCs location
//Get Player's location
//Get differences of components
//Compute total distance
//Compute bullet components
//Create new bullet
Bullet *theBullet=new Bullet(bx,by);
//Add to list
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.
thanks man youve have helped alot.
By the way your sprite x and y's are found by taking the starting x,y of your grid and doing this:
how can i find the "spritecolumn/row" and the "cellsizex/y"?
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.
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.
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.
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.