Jump to content

Geometry, Projectiles, efficency

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Maurice_Z

Maurice_Z

    Learning Programmer

  • Members
  • PipPipPip
  • 41 posts
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.
Posted Image
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:

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 ^^

Edited by Maurice_Z, 25 March 2008 - 12:13 PM.
Typo in title


#2
2stamlers

2stamlers

    Learning Programmer

  • Members
  • PipPipPip
  • 46 posts
You need to read a little bit more about collision detection as I am sure there is better way to do it then your way. I am sure that there are developed algorithms and you just pick the one that suits your needs. Just google it.

Cheers and good luck with the code.

#3
Maurice_Z

Maurice_Z

    Learning Programmer

  • Members
  • PipPipPip
  • 41 posts
Easy to say. I've been crawling google many times, quite often without a result. This time too. Or it might be, that I've just missed a link, but I don't think so.
Well, I'll try to look some more on Google, but if anyone can help me here I'd be mostly glad :).