• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Space game question

## Recommended Posts

Ok... all you guys prolly will think this is stupid. But, I want a GOOD, CLEAN method of checking to see if the character''s sprite(a ship) got hit by an enemy bullet. I''ll show you guys the code that I WOULD do, if I were going to make a messy way of doing this.
  int CheckHit() { for(int i=0; i<10; i++) { //10 is the number of enemies on //screen if(enemies[i].bullet.x>character.x && enemies [i].bullet.x
I didn''t include the bullet.y, because I figured you guys could figure it out... I think this code looks really ugly, and also, each enemy can shoot 2 bullets at a time. Adding that would make this method look even less clean. Anyone have any good ideas? Sponge Factory --Muzlack

##### Share on other sites
Separate the Enemies from the Bullets. Dynamic memory allocation wouldn''t hurt, either.

Some code from the CheckForHits() function in "Donuts3D", the shooter included in the DirectX 8.0 sdk:
// Check if bullet is in radius of objectFLOAT fDistance = D3DXVec3Length( &(pBullet->vPos - pTarget->vPos) );if( fDistance < (pTarget->fSize + pBullet->fSize) ){// BOOM!} `
In a nutshell, these two lines behave as such:
1) Calculate the distance from the center of the bullet to the center of the target
2) if the distance between them is less than their combined radius, then there''s a collision.
Not really sure if this is any better- a bunch of calculating, and a single comparison as opposed to some simple adding, and a bunch of comparison.

Anyways... there''s my \$.02.

---email--- Tok ----surf----
~The Feature Creep of the Family~

##### Share on other sites

1) Store bounding information in a separate structure, with a generic function to test for collisions. Your character''s ship is going to collide with things other than bullets (enemy ships, power-ups, space junk, the sun, whatever), and you don''t want a separate function for each thing. The collision test is going to be essentially what you have there, but cleaner because you''re not messing with the arrays or enemy/character structures.

2) Don''t leave bullets as members of an enemy ship. Once fired, store the bullet as a separate object. That way, later on down the road when you decide to include missles, you don''t have to have a separate set of checks for those.

3) Try to keep your functions as simple and generic as possible.

4) Personally, being a c++ guy, I wouldn''t access member variables directly, I''d use inlined accessor functions. It makes your code more readable, more maintainable, and able to handle change better.

Hope this helps.

Take care,
Bill

##### Share on other sites
Well, 1st, when you call I would have CheckHit call a generic Intersect() function which just takes X, Y values and Size values (in a structure preferably). You''ll be checking more than if your ship hits a bullet or enemy.

Also, in your intersect function, you can decide there whether to use bounding box, radius checking (or both), or bitmask checking. In my spce shooter, I started with bounding box, then went to bounding box and radius, and finally, I''m using only bitmasks (so it is true intersections).

##### Share on other sites
Hehe... just fyi tok, I''m just using ddraw, not any of that d3d stuff (yet) ... I think I will make an intersect function, and I would just make a struct that contains a x1,y1,x2,y2 for each intersecting part? Then I''d make a function to automatically collision detect it, but the thing I am wondering is a clean way of moving all the bullets without having everything "sloppy" I guess you could say... I hope you guys understand what I mean.

Sponge Factory
--Muzlack

##### Share on other sites
Here''s how it''s done:

If you want a smooth collision detection, you''ll need to detect the actual collision INSIDE your MOB drawing rtne. (MOB = Movable Object Block)

If you don''t need such smoothness, just do boundary comparisons as you proposed, but once you detect two bounds overlapping, handle them at once and take them out of the comparison, so they won''t affect the boundary checks for the rest of the MOBs.

I''ve never seen a c++ routine do this fast enough, so if I were you, I''d assemble it. Then you may call it from your program.

-o-

##### Share on other sites
Here''s how it''s done:

If you want a smooth collision detection, you''ll need to detect the actual collision INSIDE your MOB drawing rtne. (MOB = Movable Object Block)

If you don''t need such smoothness, just do boundary comparisons as you proposed, but once you detect two bounds overlapping, handle them at once and take them out of the comparison, so they won''t affect the boundary checks for the rest of the MOBs.

I''ve never seen a c++ routine do this fast enough, so if I were you, I''d assemble it. Then you may call it from your program.

-o-

when all else fails..

• ### Forum Statistics

• Total Topics
627711
• Total Posts
2978744

• 21
• 14
• 12
• 36
• 30