[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [29227] branches/soc-2010-nicks: added obstacle avoidance code; object movement is implemented via setting velocity

Nick Samarin nicks1987 at bigmir.net
Sat Jun 5 01:29:49 CEST 2010


Revision: 29227
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=29227
Author:   nicks
Date:     2010-06-05 01:29:49 +0200 (Sat, 05 Jun 2010)

Log Message:
-----------
added obstacle avoidance code; object movement is implemented via setting velocity

Modified Paths:
--------------
    branches/soc-2010-nicks/projectfiles_vc9/gameengine/ketsji/KX_ketsji.vcproj
    branches/soc-2010-nicks/source/blender/editors/space_logic/logic_window.c
    branches/soc-2010-nicks/source/blender/makesdna/DNA_actuator_types.h
    branches/soc-2010-nicks/source/blender/makesrna/intern/rna_actuator.c
    branches/soc-2010-nicks/source/gameengine/Converter/BL_BlenderDataConversion.cpp
    branches/soc-2010-nicks/source/gameengine/Converter/KX_ConvertActuators.cpp
    branches/soc-2010-nicks/source/gameengine/Ketsji/KX_Scene.cpp
    branches/soc-2010-nicks/source/gameengine/Ketsji/KX_Scene.h
    branches/soc-2010-nicks/source/gameengine/Ketsji/KX_SteeringActuator.cpp
    branches/soc-2010-nicks/source/gameengine/Ketsji/KX_SteeringActuator.h

Added Paths:
-----------
    branches/soc-2010-nicks/source/gameengine/Ketsji/KX_ObstacleSimulation.cpp
    branches/soc-2010-nicks/source/gameengine/Ketsji/KX_ObstacleSimulation.h

Modified: branches/soc-2010-nicks/projectfiles_vc9/gameengine/ketsji/KX_ketsji.vcproj
===================================================================
--- branches/soc-2010-nicks/projectfiles_vc9/gameengine/ketsji/KX_ketsji.vcproj	2010-06-04 22:23:34 UTC (rev 29226)
+++ branches/soc-2010-nicks/projectfiles_vc9/gameengine/ketsji/KX_ketsji.vcproj	2010-06-04 23:29:49 UTC (rev 29227)
@@ -556,6 +556,10 @@
 				>
 			</File>
 			<File
+				RelativePath="..\..\..\source\gameengine\Ketsji\KX_ObstacleSimulation.cpp"
+				>
+			</File>
+			<File
 				RelativePath="..\..\..\source\gameengine\Ketsji\KX_PhysicsObjectWrapper.cpp"
 				>
 			</File>
@@ -865,6 +869,10 @@
 				>
 			</File>
 			<File
+				RelativePath="..\..\..\source\gameengine\Ketsji\KX_ObstacleSimulation.h"
+				>
+			</File>
+			<File
 				RelativePath="..\..\..\source\gameengine\Ketsji\KX_OdePhysicsController.h"
 				>
 			</File>

Modified: branches/soc-2010-nicks/source/blender/editors/space_logic/logic_window.c
===================================================================
--- branches/soc-2010-nicks/source/blender/editors/space_logic/logic_window.c	2010-06-04 22:23:34 UTC (rev 29226)
+++ branches/soc-2010-nicks/source/blender/editors/space_logic/logic_window.c	2010-06-04 23:29:49 UTC (rev 29227)
@@ -4272,7 +4272,7 @@
 
 	row = uiLayoutRow(layout, 0);
 	uiItemR(row, ptr, "distance", 0, NULL, 0);
-	uiItemR(row, ptr, "movement", 0, NULL, 0);
+	uiItemR(row, ptr, "velocity", 0, NULL, 0);
 }
 
 

Modified: branches/soc-2010-nicks/source/blender/makesdna/DNA_actuator_types.h
===================================================================
--- branches/soc-2010-nicks/source/blender/makesdna/DNA_actuator_types.h	2010-06-04 22:23:34 UTC (rev 29226)
+++ branches/soc-2010-nicks/source/blender/makesdna/DNA_actuator_types.h	2010-06-04 23:29:49 UTC (rev 29227)
@@ -218,7 +218,7 @@
 	char pad[4];
 	int type;		/* 0=seek, 1=flee, 2=path following */
 	float dist;
-	float movement;
+	float velocity;
 	struct Object *target;
 	struct Object *navmesh;
 } bSteeringActuator;

Modified: branches/soc-2010-nicks/source/blender/makesrna/intern/rna_actuator.c
===================================================================
--- branches/soc-2010-nicks/source/blender/makesrna/intern/rna_actuator.c	2010-06-04 22:23:34 UTC (rev 29226)
+++ branches/soc-2010-nicks/source/blender/makesrna/intern/rna_actuator.c	2010-06-04 23:29:49 UTC (rev 29227)
@@ -1866,10 +1866,10 @@
 	RNA_def_property_ui_text(prop, "Behavior", "");
 	RNA_def_property_update(prop, NC_LOGIC, NULL);
 
-	prop= RNA_def_property(srna, "movement", PROP_FLOAT, PROP_NONE);
-	RNA_def_property_float_sdna(prop, NULL, "movement");
+	prop= RNA_def_property(srna, "velocity", PROP_FLOAT, PROP_NONE);
+	RNA_def_property_float_sdna(prop, NULL, "velocity");
 	RNA_def_property_range(prop, 0.0, 1000.0);
-	RNA_def_property_ui_text(prop, "Move", "Movement value");
+	RNA_def_property_ui_text(prop, "Velocity", "Velocity magnitude");
 	RNA_def_property_update(prop, NC_LOGIC, NULL);
 
 	prop= RNA_def_property(srna, "distance", PROP_FLOAT, PROP_NONE);

Modified: branches/soc-2010-nicks/source/gameengine/Converter/BL_BlenderDataConversion.cpp
===================================================================
--- branches/soc-2010-nicks/source/gameengine/Converter/BL_BlenderDataConversion.cpp	2010-06-04 22:23:34 UTC (rev 29226)
+++ branches/soc-2010-nicks/source/gameengine/Converter/BL_BlenderDataConversion.cpp	2010-06-04 23:29:49 UTC (rev 29227)
@@ -173,6 +173,7 @@
 #include "BL_DeformableGameObject.h"
 
 #include "KX_NavMeshObject.h"
+#include "KX_ObstacleSimulation.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -2638,6 +2639,20 @@
 	converter->RegisterWorldInfo(worldinfo);
 	kxscene->SetWorldInfo(worldinfo);
 
+	//create object representations for obstacle simulation
+	KX_ObstacleSimulation* obssimulation = kxscene->GetObstacleSimulation();
+	if (obssimulation)
+	{
+		for ( i=0;i<objectlist->GetCount();i++)
+		{
+			KX_GameObject* gameobj = static_cast<KX_GameObject*>(objectlist->GetValue(i));
+			if (gameobj->IsDynamic())
+			{
+				obssimulation->AddObstacleForObj(gameobj);
+			}
+		}
+	}
+
 #define CONVERT_LOGIC
 #ifdef CONVERT_LOGIC
 	// convert logic bricks, sensors, controllers and actuators
@@ -2689,10 +2704,8 @@
 			pathfinder->BuildNavMesh();
 			pathfinder->SetVisible(0, true);
 		}
-	}		
+	}
 	
-
-	
 	// Calculate the scene btree -
 	// too slow - commented out.
 	//kxscene->SetNodeTree(tf.MakeTree());

Modified: branches/soc-2010-nicks/source/gameengine/Converter/KX_ConvertActuators.cpp
===================================================================
--- branches/soc-2010-nicks/source/gameengine/Converter/KX_ConvertActuators.cpp	2010-06-04 22:23:34 UTC (rev 29226)
+++ branches/soc-2010-nicks/source/gameengine/Converter/KX_ConvertActuators.cpp	2010-06-04 23:29:49 UTC (rev 29227)
@@ -1059,7 +1059,7 @@
 
 				KX_SteeringActuator *tmpstact
 					= new KX_SteeringActuator(gameobj, mode, targetob, navmeshob,
-												stAct->movement, stAct->dist);
+												stAct->velocity, stAct->dist, scene->GetObstacleSimulation());
 				baseact = tmpstact;
 				break;
 			}

Added: branches/soc-2010-nicks/source/gameengine/Ketsji/KX_ObstacleSimulation.cpp
===================================================================
--- branches/soc-2010-nicks/source/gameengine/Ketsji/KX_ObstacleSimulation.cpp	                        (rev 0)
+++ branches/soc-2010-nicks/source/gameengine/Ketsji/KX_ObstacleSimulation.cpp	2010-06-04 23:29:49 UTC (rev 29227)
@@ -0,0 +1,245 @@
+/**
+* Simulation for obstacle avoidance behavior
+*
+* $Id$
+*
+* ***** BEGIN GPL LICENSE BLOCK *****
+*
+* This program is free software; you can redistribute it and/or
+* modify it under the terms of the GNU General Public License
+* as published by the Free Software Foundation; either version 2
+* of the License, or (at your option) any later version. The Blender
+* Foundation also sells licenses for use in proprietary software under
+* the Blender License.  See http://www.blender.org/BL/ for information
+* about this.
+*
+* This program is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with this program; if not, write to the Free Software Foundation,
+* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+*
+* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
+* All rights reserved.
+*
+* The Original Code is: all of this file.
+*
+* Contributor(s): none yet.
+*
+* ***** END GPL LICENSE BLOCK *****
+*/
+
+#include "KX_ObstacleSimulation.h"
+#include "KX_GameObject.h"
+#include "DNA_object_types.h"
+#include "math.h"
+#define M_PI       3.14159265358979323846
+
+int sweepCircleCircle(const MT_Vector3& pos0, const MT_Scalar r0, const MT_Vector2& v,
+					  const MT_Vector3& pos1, const MT_Scalar r1,
+					  float& tmin, float& tmax)
+{
+	static const float EPS = 0.0001f;
+	MT_Vector2 c0(pos0.x(), pos0.y());
+	MT_Vector2 c1(pos1.x(), pos1.y());
+	MT_Vector2 s = c1 - c0;
+	MT_Scalar  r = r0+r1;
+	float c = s.length2() - r*r;
+	float a = v.length2();
+	if (a < EPS) return 0;	// not moving
+
+	// Overlap, calc time to exit.
+	float b = MT_dot(v,s);
+	float d = b*b - a*c;
+	if (d < 0.0f) return 0; // no intersection.
+	tmin = (b - sqrtf(d)) / a;
+	tmax = (b + sqrtf(d)) / a;
+	return 1;
+}
+
+
+KX_ObstacleSimulation::KX_ObstacleSimulation()
+{
+
+}
+
+KX_ObstacleSimulation::~KX_ObstacleSimulation()
+{
+	for (size_t i=0; i<m_obstacles.size(); i++)
+	{
+		KX_Obstacle* obs = m_obstacles[i];
+		delete obs;
+	}
+	m_obstacles.clear();
+}
+void KX_ObstacleSimulation::AddObstacleForObj(KX_GameObject* gameobj)
+{
+	KX_Obstacle* obstacle = new KX_Obstacle();
+	struct Object* blenderobject = gameobj->GetBlenderObject();
+	obstacle->m_rad = blenderobject->inertia; //.todo use radius of collision shape bound sphere 
+	obstacle->m_gameObj = gameobj;
+	m_obstacles.push_back(obstacle);
+}
+
+void KX_ObstacleSimulation::UpdateObstacles()
+{
+	for (size_t i=0; i<m_obstacles.size(); i++)
+	{
+		KX_Obstacle* obs = m_obstacles[i];
+		obs->m_pos = obs->m_gameObj->NodeGetWorldPosition();
+		obs->m_vel.x() = obs->m_gameObj->GetLinearVelocity().x();
+		obs->m_vel.y() = obs->m_gameObj->GetLinearVelocity().y();
+	}
+}
+
+KX_Obstacle* KX_ObstacleSimulation::GetObstacle(KX_GameObject* gameobj)
+{
+	for (size_t i=0; i<m_obstacles.size(); i++)
+	{
+		if (m_obstacles[i]->m_gameObj == gameobj)
+			return m_obstacles[i];
+	}
+
+	return NULL;
+}
+
+void KX_ObstacleSimulation::AdjustObstacleVelocity(KX_Obstacle* activeObst, MT_Vector3& velocity)
+{
+}
+
+KX_ObstacleSimulationTOI::KX_ObstacleSimulationTOI():
+	m_avoidSteps(32),
+	m_minToi(0.5f),
+	m_maxToi(1.2f),
+	m_angleWeight(4.0f),
+	m_toiWeight(1.0f),
+	m_collisionWeight(100.0f)
+{
+	
+}
+
+KX_ObstacleSimulationTOI::~KX_ObstacleSimulationTOI()
+{
+	for (size_t i=0; i<m_toiCircles.size(); i++)
+	{
+		TOICircle* toi = m_toiCircles[i];
+		delete toi;
+	}
+	m_toiCircles.clear();
+}
+
+void KX_ObstacleSimulationTOI::AddObstacleForObj(KX_GameObject* gameobj)
+{
+	KX_ObstacleSimulation::AddObstacleForObj(gameobj);
+	m_toiCircles.push_back(new TOICircle());
+}
+
+void KX_ObstacleSimulationTOI::AdjustObstacleVelocity(KX_Obstacle* activeObst, MT_Vector3& velocity)
+{
+	int nobs = m_obstacles.size();
+	int obstidx = std::find(m_obstacles.begin(), m_obstacles.end(), activeObst) - m_obstacles.begin();
+	if (obstidx == nobs)
+		return; 
+	TOICircle* tc = m_toiCircles[obstidx];
+
+	MT_Vector2 vel(velocity.x(), velocity.y());
+	float vmax = (float) velocity.length();
+	float odir = (float) atan2(velocity.y(), velocity.x());
+
+	MT_Vector2 ddir = vel;
+	ddir.normalize();
+
+	float bestScore = FLT_MAX;
+	float bestDir = odir;
+	float bestToi = 0;
+
+	tc->n = m_avoidSteps;
+	tc->minToi = m_minToi;
+	tc->maxToi = m_maxToi;
+
+	const int iforw = m_avoidSteps/2;
+	const float aoff = (float)iforw / (float)m_avoidSteps;
+
+	for (int iter = 0; iter < m_avoidSteps; ++iter)
+	{
+		// Calculate sample velocity
+		const float ndir = ((float)iter/(float)m_avoidSteps) - aoff;
+		const float dir = odir+ndir*M_PI*2;
+		MT_Vector2 svel;
+		svel.x() = cosf(dir) * vmax;
+		svel.y() = sinf(dir) * vmax;
+
+		// Find min time of impact and exit amongst all obstacles.
+		float tmin = m_maxToi;
+		float tmine = 0;
+		for (int i = 0; i < nobs; ++i)
+		{

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list