[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [29416] branches/soc-2010-nicks/source: - added acceleration and turn speed parameters for obstacle simulation

Nick Samarin nicks1987 at bigmir.net
Fri Jun 11 23:13:59 CEST 2010


Revision: 29416
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=29416
Author:   nicks
Date:     2010-06-11 23:13:59 +0200 (Fri, 11 Jun 2010)

Log Message:
-----------
- added acceleration and turn speed parameters for obstacle simulation
- added debug visualization for object velocities 

Modified Paths:
--------------
    branches/soc-2010-nicks/source/blender/blenkernel/intern/sca.c
    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/KX_ConvertActuators.cpp
    branches/soc-2010-nicks/source/gameengine/Ketsji/KX_ObstacleSimulation.cpp
    branches/soc-2010-nicks/source/gameengine/Ketsji/KX_ObstacleSimulation.h
    branches/soc-2010-nicks/source/gameengine/Ketsji/KX_SteeringActuator.cpp
    branches/soc-2010-nicks/source/gameengine/Ketsji/KX_SteeringActuator.h

Modified: branches/soc-2010-nicks/source/blender/blenkernel/intern/sca.c
===================================================================
--- branches/soc-2010-nicks/source/blender/blenkernel/intern/sca.c	2010-06-11 20:38:51 UTC (rev 29415)
+++ branches/soc-2010-nicks/source/blender/blenkernel/intern/sca.c	2010-06-11 21:13:59 UTC (rev 29416)
@@ -416,6 +416,7 @@
 	bObjectActuator *oa;
 	bRandomActuator *ra;
 	bSoundActuator *sa;
+	bSteeringActuator *sta;
 	
 	if(act->data) MEM_freeN(act->data);
 	act->data= 0;
@@ -491,6 +492,9 @@
 		break;
 	case ACT_STEERING:
 		act->data = MEM_callocN(sizeof( bSteeringActuator), "steering act");
+		sta = act->data;
+		sta->acceleration = 3;
+		sta->turnspeed = 120;
 	default:
 		; /* this is very severe... I cannot make any memory for this        */
 		/* logic brick...                                                    */

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-11 20:38:51 UTC (rev 29415)
+++ branches/soc-2010-nicks/source/blender/editors/space_logic/logic_window.c	2010-06-11 21:13:59 UTC (rev 29416)
@@ -4273,6 +4273,9 @@
 	row = uiLayoutRow(layout, 0);
 	uiItemR(row, ptr, "distance", 0, NULL, 0);
 	uiItemR(row, ptr, "velocity", 0, NULL, 0);
+	row = uiLayoutRow(layout, 0);
+	uiItemR(row, ptr, "acceleration", 0, NULL, 0);
+	uiItemR(row, ptr, "turnspeed", 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-11 20:38:51 UTC (rev 29415)
+++ branches/soc-2010-nicks/source/blender/makesdna/DNA_actuator_types.h	2010-06-11 21:13:59 UTC (rev 29416)
@@ -219,6 +219,8 @@
 	int type;		/* 0=seek, 1=flee, 2=path following */
 	float dist;
 	float velocity;
+	float acceleration;
+	float turnspeed;
 	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-11 20:38:51 UTC (rev 29415)
+++ branches/soc-2010-nicks/source/blender/makesrna/intern/rna_actuator.c	2010-06-11 21:13:59 UTC (rev 29416)
@@ -1872,6 +1872,18 @@
 	RNA_def_property_ui_text(prop, "Velocity", "Velocity magnitude");
 	RNA_def_property_update(prop, NC_LOGIC, NULL);
 
+	prop= RNA_def_property(srna, "acceleration", PROP_FLOAT, PROP_NONE);
+	RNA_def_property_float_sdna(prop, NULL, "acceleration");
+	RNA_def_property_range(prop, 0.0, 1000.0);
+	RNA_def_property_ui_text(prop, "Acceleration", "Max acceleration");
+	RNA_def_property_update(prop, NC_LOGIC, NULL);
+
+	prop= RNA_def_property(srna, "turnspeed", PROP_FLOAT, PROP_NONE);
+	RNA_def_property_float_sdna(prop, NULL, "turnspeed");
+	RNA_def_property_range(prop, 0.0, 720.0);
+	RNA_def_property_ui_text(prop, "Turn speed", "Max turn speed");
+	RNA_def_property_update(prop, NC_LOGIC, NULL);
+
 	prop= RNA_def_property(srna, "distance", PROP_FLOAT, PROP_NONE);
 	RNA_def_property_float_sdna(prop, NULL, "dist");
 	RNA_def_property_range(prop, 0.0, 1000.0);

Modified: branches/soc-2010-nicks/source/gameengine/Converter/KX_ConvertActuators.cpp
===================================================================
--- branches/soc-2010-nicks/source/gameengine/Converter/KX_ConvertActuators.cpp	2010-06-11 20:38:51 UTC (rev 29415)
+++ branches/soc-2010-nicks/source/gameengine/Converter/KX_ConvertActuators.cpp	2010-06-11 21:13:59 UTC (rev 29416)
@@ -1058,8 +1058,9 @@
 				}
 
 				KX_SteeringActuator *tmpstact
-					= new KX_SteeringActuator(gameobj, mode, targetob, navmeshob,
-												stAct->velocity, stAct->dist, scene->GetObstacleSimulation());
+					= new KX_SteeringActuator(gameobj, mode, targetob, navmeshob,stAct->dist, 
+											stAct->velocity, stAct->acceleration, stAct->turnspeed,
+											scene->GetObstacleSimulation());
 				baseact = tmpstact;
 				break;
 			}

Modified: branches/soc-2010-nicks/source/gameengine/Ketsji/KX_ObstacleSimulation.cpp
===================================================================
--- branches/soc-2010-nicks/source/gameengine/Ketsji/KX_ObstacleSimulation.cpp	2010-06-11 20:38:51 UTC (rev 29415)
+++ branches/soc-2010-nicks/source/gameengine/Ketsji/KX_ObstacleSimulation.cpp	2010-06-11 21:13:59 UTC (rev 29416)
@@ -36,13 +36,12 @@
 #include "KX_NavMeshObject.h"
 #include "KX_PythonInit.h"
 #include "DNA_object_types.h"
-#include <math.h>
+#include "BLI_math.h"
 
-#ifndef M_PI
-#define M_PI		3.14159265358979323846
-#endif
+inline float perp(const MT_Vector2& a, const MT_Vector2& b) { return a.x()*b.y() - a.y()*b.x(); }
+inline float lerp(float a, float b, float t) { return a + (b-a)*t; }
 
-int sweepCircleCircle(const MT_Vector3& pos0, const MT_Scalar r0, const MT_Vector2& v,
+static 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)
 {
@@ -64,10 +63,7 @@
 	return 1;
 }
 
-inline float perp(const MT_Vector2& a, const MT_Vector2& b) { return a.x()*b.y() - a.y()*b.x(); }
-
-
-int sweepCircleSegment(const MT_Vector3& pos0, const MT_Scalar r0, const MT_Vector2& v,
+static int sweepCircleSegment(const MT_Vector3& pos0, const MT_Scalar r0, const MT_Vector2& v,
 					   const MT_Vector3& pa, const MT_Vector3& pb, const MT_Scalar sr,
 					   float& tmin, float &tmax)
 {
@@ -143,7 +139,33 @@
 	return 1;
 }
 
+static bool inBetweenAngle(float a, float amin, float amax, float& t)
+{
+	if (amax < amin) amax += (float)M_PI*2;
+	if (a < amin-(float)M_PI) a += (float)M_PI*2;
+	if (a > amin+(float)M_PI) a -= (float)M_PI*2;
+	if (a >= amin && a < amax)
+	{
+		t = (a-amin) / (amax-amin);
+		return true;
+	}
+	return false;
+}
 
+static float interpolateToi(float a, const float* dir, const float* toi, const int ntoi)
+{
+	for (int i = 0; i < ntoi; ++i)
+	{
+		int next = (i+1) % ntoi;
+		float t;
+		if (inBetweenAngle(a, dir[i], dir[next], t))
+		{
+			return lerp(toi[i], toi[next], t);
+		}
+	}
+	return 0;
+}
+
 KX_ObstacleSimulation::KX_ObstacleSimulation()
 {
 
@@ -231,7 +253,7 @@
 }
 
 void KX_ObstacleSimulation::AdjustObstacleVelocity(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj, 
-													MT_Vector3& velocity)
+										MT_Vector3& velocity, MT_Scalar maxDeltaSpeed,MT_Scalar maxDeltaAngle)
 {
 }
 
@@ -282,7 +304,8 @@
 	return obstacle;
 }
 
-void KX_ObstacleSimulationTOI::AdjustObstacleVelocity(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj, MT_Vector3& velocity)
+void KX_ObstacleSimulationTOI::AdjustObstacleVelocity(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj, 
+										MT_Vector3& velocity, MT_Scalar maxDeltaSpeed, MT_Scalar maxDeltaAngle)
 {
 	int nobs = m_obstacles.size();
 	int obstidx = std::find(m_obstacles.begin(), m_obstacles.end(), activeObst) - m_obstacles.begin();
@@ -387,10 +410,35 @@
 		tc->toie[iter] = tmine;
 	}
 
+	if (activeObst->m_vel.length() > 0.1)
+	{
+		// Constrain max turn rate.
+		float cura = atan2(activeObst->m_vel.y(),activeObst->m_vel.x());
+		float da = bestDir - cura;
+		if (da < -M_PI) da += (float)M_PI*2;
+		if (da > M_PI) da -= (float)M_PI*2;
+		if (da < -maxDeltaAngle)
+		{
+			bestDir = cura - maxDeltaAngle;
+			bestToi = min(bestToi, interpolateToi(bestDir, tc->dir, tc->toi, tc->n));
+		}
+		else if (da > maxDeltaAngle)
+		{
+			bestDir = cura + maxDeltaAngle;
+			bestToi = min(bestToi, interpolateToi(bestDir, tc->dir, tc->toi, tc->n));
+		}
+	}
+
 	// Adjust speed when time of impact is less than min TOI.
 	if (bestToi < m_minToi)
 		vmax *= bestToi/m_minToi;
 
+	// Constrain velocity change.
+	const float curSpeed = (float) activeObst->m_vel.length();
+	float deltaSpeed =  vmax - curSpeed; 
+	CLAMP(deltaSpeed, -maxDeltaSpeed, maxDeltaSpeed);
+	vmax = curSpeed + deltaSpeed;
+
 	// New steering velocity.
 	vel.x() = cosf(bestDir) * vmax;
 	vel.y() = sinf(bestDir) * vmax;

Modified: branches/soc-2010-nicks/source/gameengine/Ketsji/KX_ObstacleSimulation.h
===================================================================
--- branches/soc-2010-nicks/source/gameengine/Ketsji/KX_ObstacleSimulation.h	2010-06-11 20:38:51 UTC (rev 29415)
+++ branches/soc-2010-nicks/source/gameengine/Ketsji/KX_ObstacleSimulation.h	2010-06-11 21:13:59 UTC (rev 29416)
@@ -85,7 +85,7 @@
 	KX_Obstacle* GetObstacle(KX_GameObject* gameobj);
 	void UpdateObstacles();	
 	virtual void AdjustObstacleVelocity(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj, 
-										MT_Vector3& velocity);
+								MT_Vector3& velocity, MT_Scalar maxDeltaSpeed,MT_Scalar maxDeltaAngle);
 
 }; /* end of class KX_ObstacleSimulation*/
 
@@ -116,7 +116,7 @@
 	KX_ObstacleSimulationTOI();
 	~KX_ObstacleSimulationTOI();
 	virtual void AdjustObstacleVelocity(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj, 
-										MT_Vector3& velocity);
+									MT_Vector3& velocity, MT_Scalar maxDeltaSpeed,MT_Scalar maxDeltaAngle);
 };
 
 #endif

Modified: branches/soc-2010-nicks/source/gameengine/Ketsji/KX_SteeringActuator.cpp
===================================================================
--- branches/soc-2010-nicks/source/gameengine/Ketsji/KX_SteeringActuator.cpp	2010-06-11 20:38:51 UTC (rev 29415)
+++ branches/soc-2010-nicks/source/gameengine/Ketsji/KX_SteeringActuator.cpp	2010-06-11 21:13:59 UTC (rev 29416)
@@ -32,11 +32,14 @@
 * ***** END GPL LICENSE BLOCK *****
 */
 
+#include "BLI_math.h"
 #include "KX_SteeringActuator.h"
 #include "KX_GameObject.h"
 #include "KX_NavMeshObject.h"
 #include "KX_ObstacleSimulation.h"
+#include "KX_PythonInit.h"
 
+
 /* ------------------------------------------------------------------------- */
 /* Native functions                                                          */
 /* ------------------------------------------------------------------------- */
@@ -45,14 +48,18 @@
 									int mode,
 									KX_GameObject *target,
 									KX_GameObject *navmesh,
+									MT_Scalar distance,
 									MT_Scalar velocity, 
-									MT_Scalar distance,
+									MT_Scalar acceleration,									
+									MT_Scalar turnspeed,
 									KX_ObstacleSimulation* simulation)	 : 
 	SCA_IActuator(gameobj, KX_ACT_STEERING),

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list