View Single Post
  #1 (permalink)  
Old 03-25-2008, 03:09 PM
Maurice_Z Maurice_Z is offline
Learning Programmer
 
Join Date: Nov 2007
Location: Poland
Posts: 35
Credits: 0
Rep Power: 4
Maurice_Z is on a distinguished road
Send a message via ICQ to Maurice_Z Send a message via AIM to Maurice_Z Send a message via MSN to Maurice_Z Send a message via Yahoo to Maurice_Z
Question Geometry, Projectiles, efficency

It is rather difficult to put it short. But I will try:

In my game, I use geometry to decide collision between objects, as it is the fastest method. So, if I want to check, if a pistol shot hits ball, I just make a few tests to see if the line intersects with circle (The pistol shot is 1 pixel big). But if I want to have bigger projectile, say plasma ball, I have to do at least two tests, but three should be actually. Why? Wel, look at the picture.

Here we have a plasma ball (The red circle) flying left. There is a black ball, and black rectangle too. Now we can clearly see, that the plasma DO hits the ball and the rectangle. Now, if I only make one test, ie. geometry check LineVSCircle where line is the middle path of plasma, it won't hit. So that's why I have to do more checks. But if I'll make only two checks (the Top and the bottom path, the green lines), then LineVSRectangle won't detect, that plasma hits the rectangle. So I have to do three checks. But that's not the only problem.
Every time I do detection if projectile hits enemies I have to check which LINES hit the enemy, then determine the closest one, and then check if the Hit enemy is the closest enemy lying on the projectile path. The whole code looks basically like this:
Code:
line1[]=Hit Check against the enemy and top line (Upper green)
line1[]=Hit Check against the enemy and middle line (Middle white)
line1[]=Hit Check against the enemy and bottom line (Lower green)
//Here I note distance if the hit occured
if line1Hit then line1Dist=distance(startPosition,intersectionPosition)
if line2Hit then line2Dist=distance(startPosition,intersectionPosition)
if line3Hit then line3Dist=distance(startPosition,intersectionPosition)

//Here I have to check which was the closest, which is quite crumped thing
if line1Hit and (!line2Hit or line1Dist<line2Dist){
 if line1Hit and (!line3Hit or line1Dist<line3Dist){
  intersectionPoint=line1Intersection
 }
} elseif line2Hit and (!line3Hit or line2Dist<line3Dist){
 intersectionPoint=line2Intersection
} else {
 if line3Hit{intersectionPoint=line3Intersection}
}
/If there is intersection point, then check if this hit is the closest
if intersectionPoint{
 tempDistance=distance(startPoint,intesectionPoint)
 if tempDistance<closestHitEnemyDistance{
  closestHitEnemyDistance=tempDistance
  hitEnemyID=ID
 }
}
Tha would be something like this. It ain't no pretty I'm afraid, and it is slow. So What I wanted to ask, if there is a faster way to do something like this?

Thanks in advance and thanks for reading. If something is unclear just ask, I'll appreciate all help ^^

Last edited by Maurice_Z; 03-25-2008 at 03:13 PM. Reason: Typo in title
Reply With Quote

Sponsored Links