[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [31348] branches/soc-2010-nicks/source: added option for automatic facing in steering actuator

Nick Samarin nicks1987 at bigmir.net
Sun Aug 15 14:58:13 CEST 2010


Revision: 31348
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=31348
Author:   nicks
Date:     2010-08-15 14:58:13 +0200 (Sun, 15 Aug 2010)

Log Message:
-----------
added option for automatic facing in steering actuator

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_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-08-15 12:32:37 UTC (rev 31347)
+++ branches/soc-2010-nicks/source/blender/blenkernel/intern/sca.c	2010-08-15 12:58:13 UTC (rev 31348)
@@ -472,6 +472,8 @@
 		sta->turnspeed = 120.f;
 		sta->dist = 1.f;
 		sta->velocity= 3.f;
+		sta->flag = ACT_STEERING_AUTOMATICFACING;
+		sta->facingaxis = 1;
 	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-08-15 12:32:37 UTC (rev 31347)
+++ branches/soc-2010-nicks/source/blender/editors/space_logic/logic_window.c	2010-08-15 12:58:13 UTC (rev 31348)
@@ -4348,6 +4348,12 @@
 	uiItemR(row, ptr, "acceleration", 0, NULL, 0);
 	uiItemR(row, ptr, "turnspeed", 0, NULL, 0);
 	row = uiLayoutRow(layout, 0);
+	uiItemR(row, ptr, "facing", 0, NULL, 0);
+	if (RNA_boolean_get(ptr, "facing"))
+	{
+		uiItemR(row, ptr, "facingaxis", 0, NULL, 0);	
+	}
+	row = uiLayoutRow(layout, 0);
 	uiItemR(row, ptr, "selfterminated", 0, NULL, 0);
 	if (RNA_enum_get(ptr, "mode")==ACT_STEERING_PATHFOLLOWING)
 	{

Modified: branches/soc-2010-nicks/source/blender/makesdna/DNA_actuator_types.h
===================================================================
--- branches/soc-2010-nicks/source/blender/makesdna/DNA_actuator_types.h	2010-08-15 12:32:37 UTC (rev 31347)
+++ branches/soc-2010-nicks/source/blender/makesdna/DNA_actuator_types.h	2010-08-15 12:58:13 UTC (rev 31348)
@@ -224,8 +224,9 @@
 } bArmatureActuator;
 
 typedef struct bSteeringActuator {
-	char pad[7];
+	char pad[5];
 	char flag;
+	short facingaxis;
 	int type;		/* 0=seek, 1=flee, 2=path following */
 	float dist;
 	float velocity;
@@ -525,6 +526,7 @@
 /* steeringactuator->flag */
 #define ACT_STEERING_SELFTERMINATED   1
 #define ACT_STEERING_ENABLEVISUALIZATION   2
+#define ACT_STEERING_AUTOMATICFACING   4
 
 #endif
 

Modified: branches/soc-2010-nicks/source/blender/makesrna/intern/rna_actuator.c
===================================================================
--- branches/soc-2010-nicks/source/blender/makesrna/intern/rna_actuator.c	2010-08-15 12:32:37 UTC (rev 31347)
+++ branches/soc-2010-nicks/source/blender/makesrna/intern/rna_actuator.c	2010-08-15 12:58:13 UTC (rev 31348)
@@ -1892,6 +1892,15 @@
 		{ACT_STEERING_PATHFOLLOWING, "PATHFOLLOWING", 0, "Path following", ""},
 		{0, NULL, 0, NULL, NULL}};
 
+	static EnumPropertyItem facingaxis_items[] ={
+		{1, "X", 0, "X", ""},
+		{2, "Y", 0, "Y", ""},
+		{3, "Z", 0, "Z", ""},
+		{4, "-X", 0, "-X", ""},
+		{5, "-Y", 0, "-Y", ""},
+		{6, "-Z", 0, "-Z", ""},
+		{0, NULL, 0, NULL, NULL}};
+
 	srna= RNA_def_struct(brna, "SteeringActuator", "Actuator");
 	RNA_def_struct_ui_text(srna, "Steering Actuator", "");
 	RNA_def_struct_sdna_from(srna, "bSteeringActuator", "data");
@@ -1956,6 +1965,18 @@
 	RNA_def_property_ui_text(prop, "NavMesh Object", "Navigation mesh");
 	RNA_def_property_pointer_funcs(prop, NULL, "rna_SteeringActuator_navmesh_set", NULL, NULL);
 	RNA_def_property_update(prop, NC_LOGIC, NULL);
+
+	prop= RNA_def_property(srna, "facing", PROP_BOOLEAN, PROP_NONE);
+	RNA_def_property_boolean_sdna(prop, NULL, "flag", ACT_STEERING_AUTOMATICFACING);
+	RNA_def_property_ui_text(prop, "Facing", "Enable automatic facing");
+	RNA_def_property_update(prop, NC_LOGIC, NULL);
+
+	prop= RNA_def_property(srna, "facingaxis", PROP_ENUM, PROP_NONE);
+	RNA_def_property_enum_sdna(prop, NULL, "facingaxis");
+	RNA_def_property_enum_items(prop, facingaxis_items);
+	RNA_def_property_ui_text(prop, "Axis", "Axis for automatic facing");
+	RNA_def_property_update(prop, NC_LOGIC, NULL);
+	
 }
 
 void RNA_def_actuator(BlenderRNA *brna)

Modified: branches/soc-2010-nicks/source/gameengine/Converter/KX_ConvertActuators.cpp
===================================================================
--- branches/soc-2010-nicks/source/gameengine/Converter/KX_ConvertActuators.cpp	2010-08-15 12:32:37 UTC (rev 31347)
+++ branches/soc-2010-nicks/source/gameengine/Converter/KX_ConvertActuators.cpp	2010-08-15 12:58:13 UTC (rev 31348)
@@ -1059,11 +1059,12 @@
 
 				bool selfTerminated = (stAct->flag & ACT_STEERING_SELFTERMINATED) !=0;
 				bool enableVisualization = (stAct->flag & ACT_STEERING_ENABLEVISUALIZATION) !=0;
+				short facingMode = (stAct->flag & ACT_STEERING_AUTOMATICFACING) ? stAct->facingaxis : 0;
 				KX_SteeringActuator *tmpstact
 					= new KX_SteeringActuator(gameobj, mode, targetob, navmeshob,stAct->dist, 
 								stAct->velocity, stAct->acceleration, stAct->turnspeed, 
 								selfTerminated, stAct->updateTime,
-								scene->GetObstacleSimulation(), enableVisualization);
+								scene->GetObstacleSimulation(), facingMode, enableVisualization);
 				baseact = tmpstact;
 				break;
 			}

Modified: branches/soc-2010-nicks/source/gameengine/Ketsji/KX_SteeringActuator.cpp
===================================================================
--- branches/soc-2010-nicks/source/gameengine/Ketsji/KX_SteeringActuator.cpp	2010-08-15 12:32:37 UTC (rev 31347)
+++ branches/soc-2010-nicks/source/gameengine/Ketsji/KX_SteeringActuator.cpp	2010-08-15 12:58:13 UTC (rev 31348)
@@ -38,6 +38,7 @@
 #include "KX_NavMeshObject.h"
 #include "KX_ObstacleSimulation.h"
 #include "KX_PythonInit.h"
+#include "KX_PyMath.h"
 
 
 /* ------------------------------------------------------------------------- */
@@ -55,6 +56,7 @@
 									bool  isSelfTerminated,
 									int pathUpdatePeriod,
 									KX_ObstacleSimulation* simulation,
+									short facingmode,
 									bool enableVisualization)	 : 
 	SCA_IActuator(gameobj, KX_ACT_STEERING),
 	m_mode(mode),
@@ -69,9 +71,11 @@
 	m_isActive(false),	
 	m_simulation(simulation),	
 	m_enableVisualization(enableVisualization),
+	m_facingMode(facingmode),
 	m_obstacle(NULL),
 	m_pathLen(0),
-	m_wayPointIdx(-1)
+	m_wayPointIdx(-1),
+	m_steerVec(MT_Vector3(0, 0, 0))
 {
 	m_navmesh = static_cast<KX_NavMeshObject*>(navmesh);
 	if (m_navmesh)
@@ -81,6 +85,13 @@
 	
 	if (m_simulation)
 		m_obstacle = m_simulation->GetObstacle((KX_GameObject*)gameobj);
+	KX_GameObject* parent = ((KX_GameObject*)gameobj)->GetParent();
+	if (m_facingMode>0 && parent)
+	{
+		m_parentlocalmat = parent->GetSGNode()->GetLocalOrientation();
+	}
+	else
+		m_parentlocalmat.setIdentity();
 } 
 
 KX_SteeringActuator::~KX_SteeringActuator()
@@ -175,7 +186,7 @@
 		MT_Vector3 vectotarg = targpos - mypos;
 		MT_Vector3 vectotarg2d = vectotarg;
 		vectotarg2d.z() = 0;
-		MT_Vector3 steervec = MT_Vector3(0, 0, 0);
+		m_steerVec = MT_Vector3(0, 0, 0);
 		bool apply_steerforce = false;
 		bool terminate = true;
 
@@ -184,8 +195,8 @@
 				if (vectotarg2d.length2()>m_distance*m_distance)
 				{
 					terminate = false;
-					steervec = vectotarg;
-					steervec.normalize();
+					m_steerVec = vectotarg;
+					m_steerVec.normalize();
 					apply_steerforce = true;
 				}
 				break;
@@ -193,8 +204,8 @@
 				if (vectotarg2d.length2()<m_distance*m_distance)
 				{
 					terminate = false;
-					steervec = -vectotarg;
-					steervec.normalize();
+					m_steerVec = -vectotarg;
+					m_steerVec.normalize();
 					apply_steerforce = true;
 				}
 				break;
@@ -228,7 +239,7 @@
 								waypoint.setValue(&m_path[3*m_wayPointIdx]);
 						}
 
-						steervec = waypoint - mypos;
+						m_steerVec = waypoint - mypos;
 						apply_steerforce = true;
 
 						
@@ -248,10 +259,10 @@
 		{
 			bool isdyna = obj->IsDynamic();
 			if (isdyna)
-				steervec.z() = 0;
-			if (!steervec.fuzzyZero())
-				steervec.normalize();
-			MT_Vector3 newvel = m_velocity*steervec;
+				m_steerVec.z() = 0;
+			if (!m_steerVec.fuzzyZero())
+				m_steerVec.normalize();
+			MT_Vector3 newvel = m_velocity*m_steerVec;
 
 			//adjust velocity to avoid obstacles
 			if (m_simulation && m_obstacle /*&& !newvel.fuzzyZero()*/)
@@ -264,6 +275,7 @@
 					KX_RasterizerDrawDebugLine(mypos, mypos + newvel, MT_Vector3(0.,1.,0.));
 			}
 
+			HandleActorFace(newvel);
 			if (isdyna)
 			{
 				//temporary solution: set 2D steering velocity directly to obj
@@ -295,6 +307,96 @@
 	return true;
 }
 
+const MT_Vector3& KX_SteeringActuator::GetSteeringVec()
+{
+	if (m_isActive)
+		return m_steerVec;
+	else
+		return MT_Vector3(0, 0, 0);
+}
+
+void KX_SteeringActuator::HandleActorFace(MT_Vector3& velocity)
+{
+	if (m_facingMode==0)
+		return;
+	MT_Vector3 dir = velocity;
+	if (dir.fuzzyZero())
+		return;	
+	dir.normalize();
+	MT_Vector3 up(0,0,1);
+	MT_Vector3 left;
+	MT_Matrix3x3 mat;
+	switch (m_facingMode)
+	{
+	case 1: // TRACK X
+		{
+			left  = dir.safe_normalized();
+			dir = -(left.cross(up)).safe_normalized();
+			break;
+		};
+	case 2:	// TRACK Y
+		{
+			left  = (dir.cross(up)).safe_normalized();
+			break;
+		}
+
+	case 3: // track Z
+		{
+			left = up.safe_normalized();
+			up = dir.safe_normalized();
+			dir = left;
+			left  = (dir.cross(up)).safe_normalized();
+			break;
+		}
+
+	case 4: // TRACK -X
+		{
+			left  = -dir.safe_normalized();
+			dir = -(left.cross(up)).safe_normalized();
+			break;
+		};
+	case 5: // TRACK -Y
+		{
+			left  = (-dir.cross(up)).safe_normalized();
+			dir = -dir;
+			break;
+		}
+	case 6: // track -Z
+		{
+			left = up.safe_normalized();
+			up = -dir.safe_normalized();
+			dir = left;
+			left  = (dir.cross(up)).safe_normalized();
+			break;
+		}
+	}
+	mat.setValue (
+		left[0], dir[0],up[0], 
+		left[1], dir[1],up[1],
+		left[2], dir[2],up[2]
+	);
+
+	KX_GameObject* curobj = (KX_GameObject*) GetParent();
+	
+	KX_GameObject* parentObject = curobj->GetParent();
+	if(parentObject)
+	{ 
+		MT_Point3 localpos;
+		localpos = curobj->GetSGNode()->GetLocalPosition();
+		MT_Matrix3x3 parentmatinv;
+		parentmatinv = parentObject->NodeGetWorldOrientation ().inverse ();				
+		mat = parentmatinv * mat;
+		mat = m_parentlocalmat * mat;
+		curobj->NodeSetLocalOrientation(mat);
+		curobj->NodeSetLocalPosition(localpos);
+	}
+	else
+	{
+		curobj->NodeSetLocalOrientation(mat);
+	}
+
+}
+
 #ifndef DISABLE_PYTHON
 
 /* ------------------------------------------------------------------------- */
@@ -338,6 +440,7 @@
 	KX_PYATTRIBUTE_FLOAT_RW("turnspeed", 0.0f, 720.0f, KX_SteeringActuator, m_turnspeed),
 	KX_PYATTRIBUTE_BOOL_RW("selfterminated", KX_SteeringActuator, m_isSelfTerminated),

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list