will it collide..
Monkey Forums/Monkey Programming/will it collide..
| ||
Trying to find a really fast way to detect if something is going to collide or come close to colliding with another object. one object is static the others move at randomly set vectors and speeds, any thoughts ? |
| ||
the most obvious way is probably moving circle to static circle dynamic collision. It's probably the most accurate and fastest way of doing it using vectors. |
| ||
What I went with was a check of the angle from the object to the potential impactor compared to the angle the object is actually traveling along, if the variance is low enough I assume a potential impact, if not I assume a lower threat level. needs some tweaking tho. the method, in case anyone has a super cool way of doing it. Method GetThreatLevel:Int() Local threat:Int = YELLOW_INDICATOR Local px:Int = (MapOffsetX + Self.vec.x) Local py:Int = (MapOffsetY + Self.vec.y) Local cx:Int = (MapOffsetX + (SCREEN_WIDTH / 2)) Local cy:Int = (MapOffsetY + (SCREEN_HEIGHT / 2)) DrawLine(px, py, cx, cy) Local attackAngle:float = ATan2(cx - px, cy - py) If Self.vec.angle > attackAngle - 20 And Self.vec.angle < attackAngle + 20 Then threat = RED_INDICATOR ElseIf Self.vec.angle > attackAngle - 30 And Self.vec.angle < attackAngle + 30 Then threat = ORANGE_INDICATOR ElseIf Self.vec.angle > attackAngle - 85 And Self.vec.angle < attackAngle + 85 Then threat = YELLOW_INDICATOR Else threat = GREEN_INDICATOR EndIf Return threat End Method |
| ||
this should tell you how far is the PATH of the enemy from the center of the player(player x,y): vx = player.position.x - enemy.position.x vy = player.position.y - enemy.position.y distance = Abs(enemy.direction.x * vy - enemy.direction.y * vx) direction is the single unit movement vector. direction.x = Cos(angle) direction.y = Sin(angle) formula is using cross projection to solve the problem. |
| ||
. |
| ||
I was going to use distance as part of my threat system but in the end I did not feel it was needed, I am actually thinking about removing all the green indicators so that I would then only be showing those that indicate objects traveling toward a collision, it should be more than enough of an alert for the player. |
| ||
Yea stay simple. those formulas are too much for some programmers minds. it's best to stick with something you understand. |
| ||
. |