瀏覽代碼

Bug Fix

-Path bugfix when trying to set node distance
-Path groundwork for steering behavior
marauder2k7 5 年之前
父節點
當前提交
f88f2c32f7

+ 43 - 1
engine/source/2d/sceneobject/Path.cc

@@ -68,7 +68,7 @@ void Path::preIntegrate(const F32 totalTime, const F32 elapsedTime, DebugStats *
       Vector2 cDst = mNodes[pObj.mCurrNode].position;
       Vector2 cDst = mNodes[pObj.mCurrNode].position;
       F32 distance = (cDst - cPos).Length();
       F32 distance = (cDst - cPos).Length();
 
 
-      if (distance < (pObj.mMaxSpeed * elapsedTime) || distance <= cNode.distance)
+      if (distance < (pObj.mMaxSpeed * elapsedTime) || distance < mNodes[pObj.mCurrNode].distance)
       {
       {
          S32 nCount = mNodes.size();
          S32 nCount = mNodes.size();
          S32 end = nCount - 1;
          S32 end = nCount - 1;
@@ -199,10 +199,22 @@ void Path::detachObject(SceneObject * object)
 void Path::moveObject(PathObject& obj)
 void Path::moveObject(PathObject& obj)
 {
 {
    Vector2 cDest = mNodes[obj.mNextNode].position;
    Vector2 cDest = mNodes[obj.mNextNode].position;
+   F32 slowRad = mNodes[obj.mNextNode].distance;
    Vector2 oPos = obj.mObj->getPosition();
    Vector2 oPos = obj.mObj->getPosition();
    Vector2 dir = cDest - oPos;
    Vector2 dir = cDest - oPos;
+   Vector2 currVel = obj.mObj->getLinearVelocity();
    dir.Normalize();
    dir.Normalize();
 
 
+   Vector2 steer = seek(cDest, oPos, obj.mMaxSpeed, currVel, slowRad);
+
+   steer = truncate(steer, obj.mMaxSpeed);
+   steer = steer * 1 / 2;
+   currVel = currVel + steer;
+   currVel = truncate(currVel, obj.mMaxSpeed);
+   Vector2 pos = Vector2(oPos + currVel);
+
+   //obj.mObj->applyForce(pos);
+
    obj.mObj->setLinearVelocity(dir * obj.mMaxSpeed);
    obj.mObj->setLinearVelocity(dir * obj.mMaxSpeed);
 
 
    if (obj.mOrient)
    if (obj.mOrient)
@@ -216,6 +228,36 @@ void Path::moveObject(PathObject& obj)
 
 
 }
 }
 
 
+Vector2 Path::truncate(Vector2 vec, F32 max)
+{
+   F32 i = max;
+   i = i < 1.0f ? 1.0f : i;
+   vec = vec * max;
+   return vec;
+}
+
+Vector2 Path::seek(Vector2 target, Vector2 objPos, F32 max, Vector2 curr, F32 slowRad)
+{
+   Vector2 des = target - objPos;
+   F32 dist = des.Length();
+   des.Normalize();
+
+   if (dist < slowRad)
+   {
+      des = des * (max * dist / slowRad);
+   }
+   else
+   {
+      des = des * max;
+   }
+
+   Vector2 force = des - curr;
+
+   return force;
+}
+
+
+
 void Path::onDeleteNotify(SimObject* object)
 void Path::onDeleteNotify(SimObject* object)
 {
 {
    Vector<PathObject*>::iterator i;
    Vector<PathObject*>::iterator i;

+ 2 - 0
engine/source/2d/sceneobject/Path.h

@@ -99,6 +99,8 @@ public:
 private:
 private:
 
 
    void moveObject(PathObject& obj);
    void moveObject(PathObject& obj);
+   Vector2 truncate(Vector2 vec, F32 max);
+   Vector2 seek(Vector2 target, Vector2 objPos, F32 max, Vector2 curr, F32 slowRad);
 
 
    Vector<PathObject*> mObjs;
    Vector<PathObject*> mObjs;
    Vector<Node> mNodes;
    Vector<Node> mNodes;

+ 1 - 17
engine/source/2d/sceneobject/Path_ScriptBinding.h

@@ -87,23 +87,7 @@ ConsoleMethodWithDocs(Path, detachObject, ConsoleVoid, 3, 3, (sceneObject))
 ConsoleMethodWithDocs(Path, addNode, ConsoleVoid, 3, 6, (float x, float y, [float distance], [float weight]))
 ConsoleMethodWithDocs(Path, addNode, ConsoleVoid, 3, 6, (float x, float y, [float distance], [float weight]))
 {
 {
    Vector2 position;
    Vector2 position;
-   // Elements in the first argument.
-   U32 elementCount = Utility::mGetStringElementCount(argv[2]);
-
-   // ("x y")
-   if ((elementCount == 2) && (argc >= 3))
-      position = Utility::mGetStringElementVector(argv[2]);
-
-   // (x, y)
-   else if ((elementCount == 1) && (argc == 4))
-      position.Set(dAtof(argv[2]), dAtof(argv[3]));
-
-   // Invalid
-   else
-   {
-      Con::warnf("Path::addNode() - Invalid number of parameters!");
-      return;
-   }
+   position.Set(dAtof(argv[2]), dAtof(argv[3]));
 
 
    F32 distance = 0.0f;
    F32 distance = 0.0f;
    if (argc > 4)
    if (argc > 4)

+ 4 - 4
modules/PathToy/1/main.cs

@@ -50,10 +50,10 @@ function PathToy::reset( %this )
 function PathToy::createPath(%this)
 function PathToy::createPath(%this)
 {
 {
 	%path = new Path(SquarePath);
 	%path = new Path(SquarePath);
-	%path.addNode(10.0,10.0);
-	%path.addNode(-10.0,10.0);
-	%path.addNode(-10.0,-10.0);
-	%path.addNode(10.0,-10.0);
+	%path.addNode(10.0 , 10.0, 5.0);
+	%path.addNode(-10.0 , 10.0, 5.0);
+	%path.addNode(-10.0 , -10.0, 5.0);
+	%path.addNode(10.0 , -10.0, 5.0);
 	
 	
 	SandboxScene.add(%path);
 	SandboxScene.add(%path);
 }
 }

+ 0 - 1
modules/PyramidToy/1/main.cs

@@ -192,7 +192,6 @@ function PyramidToy::createPyramid( %this )
 	
 	
 	%map = new LightObject();
 	%map = new LightObject();
 	%map.setSize("15 15");
 	%map.setSize("15 15");
-	%map.setBodyType(Static);
 	%map.SceneLayer = 17;
 	%map.SceneLayer = 17;
 	%map.setBlendColor(0.5,0.5,1.0,1.0);
 	%map.setBlendColor(0.5,0.5,1.0,1.0);
 	SandboxScene.add(%map);
 	SandboxScene.add(%map);