changeset 21:cad384ad349e

avoid
author zzzzrrr <mason.green@gmail.com>
date Thu, 26 Mar 2009 07:02:56 -0400
parents 6efd0830715b
children 4fce5596d1f6
files ai/ai.d ai/steer.d melee/contactListener.d melee/melee.d
diffstat 4 files changed, 78 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- 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();
-		}
+		//}
     }
 
 }
--- 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) {
--- 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;
 	}
--- 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;