# HG changeset patch # User zzzzrrr # Date 1238065376 14400 # Node ID cad384ad349eea09eb0b3d6e3544b3c176286d9c # Parent 6efd0830715b167d1c903a275318c63a6331faff avoid diff -r 6efd0830715b -r cad384ad349e ai/ai.d --- a/ai/ai.d Wed Mar 25 15:25:25 2009 -0400 +++ b/ai/ai.d Thu Mar 26 07:02:56 2009 -0400 @@ -59,11 +59,13 @@ // Elementary steering AI steer.update(); //st = steer.steerToAvoidObstacles(0.25, m_world.bodyList); + st = steer.avoid(maxPredictionTime, m_world.bodyList); if(st == bzVec2.zeroVect) { st = steer.steerForPursuit(enemy.state, maxPredictionTime); chase(enemy); } else { + Stdout(st.x)(",")(st.y).newline; avoid(); } @@ -114,12 +116,10 @@ } else { ship.turnLeft(); } - - ship.state.turn = true; - - if(abs(angle) < PI/4) { + + //if(abs(angle) < PI/4) { ship.thrust(); - } + //} } } diff -r 6efd0830715b -r cad384ad349e ai/steer.d --- a/ai/steer.d Wed Mar 25 15:25:25 2009 -0400 +++ b/ai/steer.d Thu Mar 26 07:02:56 2009 -0400 @@ -182,6 +182,64 @@ return avoidance; } + + bzVec2 avoid(float minTimeToCollision, bzBody obstacles) { + + float avoidMargin = 1.0f; + float maxLookahead = minTimeToCollision * m_speed; + + // Make sure we're moving + if (m_velocity.length > 0) + { + for (bzBody o = obstacles; o; o = o.next) { + + if(o is m_body) continue; + + // Find the distance from the line we're moving along to the obstacle. + bzVec2 movementNormal = m_velocity; + movementNormal.normalize(); + bzVec2 characterToObstacle = o.position - m_position; + + real distanceSquared = bzDot(characterToObstacle, movementNormal); + distanceSquared = characterToObstacle.length - + distanceSquared*distanceSquared; + + // Check for collision + // Find radius of obstacle + float oRad = 0; + for (bzShape shape = o.shapeList; shape; shape = shape.next) { + if(shape.sweepRadius > oRad) { + oRad = shape.sweepRadius; + } + } + + real radius = oRad + avoidMargin; + if (distanceSquared < radius*radius) + { + // Find how far along our movement vector the closest pass is + real distanceToClosest = bzDot(characterToObstacle, movementNormal); + + // Make sure this isn't behind us and is closer than our lookahead. + if (distanceToClosest > 0 && distanceToClosest < maxLookahead) + { + // Find the closest point + bzVec2 closestPoint = + o.position + movementNormal*distanceToClosest; + + // Find the point of avoidance + bzVec2 target = + o.position + + (closestPoint - o.position).length * + (oRad + avoidMargin); + + return target; + } + } + } + } + return bzVec2.zeroVect; + + } void findNextIntersectionWithSphere(bzBody obs, inout PathIntersection intersection, float mdc) { diff -r 6efd0830715b -r cad384ad349e melee/contactListener.d --- a/melee/contactListener.d Wed Mar 25 15:25:25 2009 -0400 +++ b/melee/contactListener.d Thu Mar 26 07:02:56 2009 -0400 @@ -38,21 +38,13 @@ import openmelee.melee.melee; +/* enum ContactState { e_contactAdded, e_contactPersisted, e_contactRemoved } - -struct ContactPoint { - bzShape shape1; - bzShape shape2; - bzVec2 normal; - bzVec2 position; - bzVec2 velocity; - bzContactID id; - ContactState state; -} +*/ // bzWorld contact callback class ContactListener : bzContactListener @@ -76,7 +68,7 @@ cp.position = point.position; cp.normal = point.normal; cp.id = point.id; - cp.state = ContactState.e_contactAdded; + //cp.state = ContactState.e_contactAdded; ++melee.pointCount; } @@ -93,7 +85,7 @@ cp.position = point.position; cp.normal = point.normal; cp.id = point.id; - cp.state = ContactState.e_contactPersisted; + //cp.state = ContactState.e_contactPersisted; ++melee.pointCount; } @@ -110,7 +102,7 @@ cp.position = point.position; cp.normal = point.normal; cp.id = point.id; - cp.state = ContactState.e_contactRemoved; + //cp.state = ContactState.e_contactRemoved; ++melee.pointCount; } diff -r 6efd0830715b -r cad384ad349e melee/melee.d --- a/melee/melee.d Wed Mar 25 15:25:25 2009 -0400 +++ b/melee/melee.d Thu Mar 26 07:02:56 2009 -0400 @@ -79,6 +79,16 @@ bool enableTOI; } +struct ContactPoint { + bzShape shape1; + bzShape shape2; + bzVec2 normal; + bzVec2 position; + bzVec2 velocity; + bzContactID id; + //ContactState state; +} + class Melee { Settings settings;