Question About Collisions


skate2live92

Member
Joined
Sep 6, 2005
Messages
145
Im modifying the source to shoot a little, and i ran into a problem. If i want to kill the player when a bullet collides with an enemy what would i do. Ive searched the docs for collisions and havent found much (on flamingbird).

Im not sure if this is what to do

Code:
 if(COLLISION(TYPE bullet1))WXYZ;end
(or at least i think so)
I just dont know what to put where WXYZ is to kill "ship".
NOTE:Ive been putting this if statement under the enemy1 process.
 
I don't know what source you're modifying but collision detection can be easy and it can be difficult. For the sake of simplicity, I assume that you're doing 2d collisions and not 3d. I can't help you with potential functionality built-in to the code to use, but I can help I suppose.

I would need more information but essentially if the bullet follows a linear path, you can get the slope of that line and see if it is to intersect with a bounding box around your target, or you can test if the bullet is inside of the bounding box. There are different tricks you can do to speed this up. You can also make a 'collision map' which is essentially either a 8-bit, 4-bit, or 2-bit map (depending on your needs). Essentially you can make an array 320*240 of either of those bit depths and have a value representing NULL, a target, and a bullet. As the bullet travels you update its placement in the map, and same goes for the target... for instance...


00000000000000000000000
00000000000000000000000
00000000000000222000000
00000000000000222000000
00000100000000000000000
00000000000000000000000

0 is empty screen space ..
1 is your bullet
2 is your target

you update the player position or the bullet position for drawing and then test and update it in the collision map, if that bit is set, then you have a collision.
 
Fenix does pixel-perfect collisions for you with the collision() command perch :) . What you want is the signal() command, which kills a process or all instances of a process type, and eventually iteratively kills all it's children. Check it's documentation here . What it comes down to in your situation would be something like

Code:
if(collision(type bullet1))
  signal(type ship,s_kill);
end

This kills all instances of the ship type. If you only have one, that does exactly what you want. If you would want to kill the bullet colliding with the enemy that runs that code too, you would catch the returning value of the collision command, which is the ID of the colliding process(in this case a bullet1). That included you'd have something like

Code:
if(bullet_id = collision(type bullet1))
  signal(type ship,s_kill);
  signal(bullet_id,s_kill);
end

EDIT: I saw your NOTE at the bottom, make sure the code is in the main loop, or it will be checked only once on instance startup. Of course, if you find this trivial I haven't said anything. :)
 
Back
Top