[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [20019] trunk/blender/source/gameengine: BGE: New function GameLogic.setMaxLogicFrame() to allow better control over the time spent on logic.

Benoit Bolsee benoit.bolsee at online.be
Fri May 1 18:35:06 CEST 2009


Revision: 20019
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=20019
Author:   ben2610
Date:     2009-05-01 18:35:06 +0200 (Fri, 01 May 2009)

Log Message:
-----------
BGE: New function GameLogic.setMaxLogicFrame() to allow better control over the time spent on logic.

This function sets the maximum number of logic frame executed per render frame.
Valid values: 1..5

This function is useful to control the amount of processing consumed by logic.
By default, up to 5 logic frames can be executed per render frame. This is fine
as long as the time spent on logic is negligible compared to the render time. 
If it's not the case, the default value will drag the performance of the game
down by executing unnecessary logic frames that take up most of the CPU time. 

You can avoid that by lowering the value with this function. 
The drawback is less precision in the logic system to physics and I/O activity.
	
Note that it does not affect the physics system: physics will still run 
at full frame rate (actually up to 5 times the ticrate).
You can further control the render frame rate with GameLogic.setLogicTicRate().

Modified Paths:
--------------
    trunk/blender/source/gameengine/Ketsji/KX_KetsjiEngine.cpp
    trunk/blender/source/gameengine/Ketsji/KX_KetsjiEngine.h
    trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp
    trunk/blender/source/gameengine/Physics/BlOde/OdePhysicsEnvironment.cpp
    trunk/blender/source/gameengine/Physics/BlOde/OdePhysicsEnvironment.h
    trunk/blender/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp
    trunk/blender/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.h
    trunk/blender/source/gameengine/Physics/Dummy/DummyPhysicsEnvironment.cpp
    trunk/blender/source/gameengine/Physics/Dummy/DummyPhysicsEnvironment.h
    trunk/blender/source/gameengine/Physics/Sumo/SumoPhysicsEnvironment.cpp
    trunk/blender/source/gameengine/Physics/Sumo/SumoPhysicsEnvironment.h
    trunk/blender/source/gameengine/Physics/common/PHY_IPhysicsEnvironment.h
    trunk/blender/source/gameengine/PyDoc/GameLogic.py

Modified: trunk/blender/source/gameengine/Ketsji/KX_KetsjiEngine.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_KetsjiEngine.cpp	2009-05-01 15:19:47 UTC (rev 20018)
+++ trunk/blender/source/gameengine/Ketsji/KX_KetsjiEngine.cpp	2009-05-01 16:35:06 UTC (rev 20019)
@@ -97,6 +97,7 @@
 };
 
 double KX_KetsjiEngine::m_ticrate = DEFAULT_LOGIC_TIC_RATE;
+int	   KX_KetsjiEngine::m_maxLogicFrame = 5;
 double KX_KetsjiEngine::m_anim_framerate = 25.0;
 double KX_KetsjiEngine::m_suspendedtime = 0.0;
 double KX_KetsjiEngine::m_suspendeddelta = 0.0;
@@ -399,6 +400,7 @@
 	m_firstframe = true;
 	m_bInitialized = true;
 	m_ticrate = DEFAULT_LOGIC_TIC_RATE;
+	m_maxLogicFrame = 5;
 	
 	if (m_game2ipo)
 	{
@@ -511,7 +513,8 @@
 
 bool KX_KetsjiEngine::NextFrame()
 {
-
+	double timestep = 1.0/m_ticrate;
+	double framestep = timestep;
 //	static hidden::Clock sClock;
 
 m_logger->StartLog(tc_services, m_kxsystem->GetTimeInSeconds(),true);
@@ -520,7 +523,7 @@
 //sClock.reset();
 
 if (m_bFixedTime)
-	m_clockTime += 1./m_ticrate;
+	m_clockTime += timestep;
 else
 {
 
@@ -554,18 +557,24 @@
 	{
 	
 	//	printf("framedOut: %d\n",frames);
-		m_frameTime+=(frames-frameOut)*(1.0/m_ticrate);
+		m_frameTime+=(frames-frameOut)*timestep;
 		frames = frameOut;
 	}
 	
 
 	bool doRender = frames>0;
 
+	if (frames > m_maxLogicFrame)
+	{
+		framestep = (frames*timestep)/m_maxLogicFrame;
+		frames = m_maxLogicFrame;
+	}
+		
 	while (frames)
 	{
 	
 
-		m_frameTime += 1.0/m_ticrate;
+		m_frameTime += framestep;
 		
 		for (sceneit = m_scenes.begin();sceneit != m_scenes.end(); ++sceneit)
 		// for each scene, call the proceed functions
@@ -644,7 +653,7 @@
 		
 				// Perform physics calculations on the scene. This can involve 
 				// many iterations of the physics solver.
-				scene->GetPhysicsEnvironment()->proceedDeltaTime(m_frameTime,1.0/m_ticrate);//m_deltatimerealDeltaTime);
+				scene->GetPhysicsEnvironment()->proceedDeltaTime(m_frameTime,timestep,framestep);//m_deltatimerealDeltaTime);
 
 				m_logger->StartLog(tc_scenegraph, m_kxsystem->GetTimeInSeconds(), true);
 				SG_SetActiveStage(SG_STAGE_PHYSICS2_UPDATE);
@@ -717,7 +726,7 @@
 				// Perform physics calculations on the scene. This can involve 
 				// many iterations of the physics solver.
 				m_logger->StartLog(tc_physics, m_kxsystem->GetTimeInSeconds(), true);
-				scene->GetPhysicsEnvironment()->proceedDeltaTime(m_clockTime,0.f);
+				scene->GetPhysicsEnvironment()->proceedDeltaTime(m_clockTime,timestep,timestep);
 				// Update scenegraph after physics step. This maps physics calculations
 				// into node positions.		
 				m_logger->StartLog(tc_scenegraph, m_kxsystem->GetTimeInSeconds(), true);
@@ -1729,6 +1738,16 @@
 	m_ticrate = ticrate;
 }
 
+int KX_KetsjiEngine::GetMaxLogicFrame()
+{
+	return m_maxLogicFrame;
+}
+
+void KX_KetsjiEngine::SetMaxLogicFrame(int frame)
+{
+	m_maxLogicFrame = frame;
+}
+
 double KX_KetsjiEngine::GetAnimFrameRate()
 {
 	return m_anim_framerate;

Modified: trunk/blender/source/gameengine/Ketsji/KX_KetsjiEngine.h
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_KetsjiEngine.h	2009-05-01 15:19:47 UTC (rev 20018)
+++ trunk/blender/source/gameengine/Ketsji/KX_KetsjiEngine.h	2009-05-01 16:35:06 UTC (rev 20019)
@@ -103,6 +103,7 @@
 	double				m_previousClockTime;//previous clock time
 	double				m_remainingTime;
 
+	static int				m_maxLogicFrame;	/* maximum number of consecutive logic frame */
 	static double			m_ticrate;
 	static double			m_anim_framerate; /* for animation playback only - ipo and action */
 
@@ -283,6 +284,14 @@
 	 * Sets the number of logic updates per second.
 	 */
 	static void SetTicRate(double ticrate);
+	/**
+	 * Gets the maximum number of logic frame before render frame
+	 */
+	static int KX_KetsjiEngine::GetMaxLogicFrame();
+	/**
+	 * Sets the maximum number of logic frame before render frame
+	 */
+	static void KX_KetsjiEngine::SetMaxLogicFrame(int frame);
 
 	/**
 	 * Gets the framerate for playing animations. (actions and ipos)

Modified: trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp
===================================================================
--- trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp	2009-05-01 15:19:47 UTC (rev 20018)
+++ trunk/blender/source/gameengine/Ketsji/KX_PythonInit.cpp	2009-05-01 16:35:06 UTC (rev 20019)
@@ -288,6 +288,21 @@
 	return PyFloat_FromDouble(KX_KetsjiEngine::GetTicRate());
 }
 
+static PyObject* gPySetMaxLogicFrame(PyObject*, PyObject* args)
+{
+	int frame;
+	if (!PyArg_ParseTuple(args, "i:setMaxLogicFrame", &frame))
+		return NULL;
+	
+	KX_KetsjiEngine::SetMaxLogicFrame(frame);
+	Py_RETURN_NONE;
+}
+
+static PyObject* gPyGetMaxLogicFrame(PyObject*)
+{
+	return PyInt_FromLong(KX_KetsjiEngine::GetMaxLogicFrame());
+}
+
 static PyObject* gPySetPhysicsTicRate(PyObject*, PyObject* args)
 {
 	float ticrate;
@@ -479,6 +494,8 @@
 	{"setGravity",(PyCFunction) gPySetGravity, METH_O, (PY_METHODCHAR)"set Gravitation"},
 	{"getSpectrum",(PyCFunction) gPyGetSpectrum, METH_NOARGS, (PY_METHODCHAR)"get audio spectrum"},
 	{"stopDSP",(PyCFunction) gPyStopDSP, METH_VARARGS, (PY_METHODCHAR)"stop using the audio dsp (for performance reasons)"},
+	{"getMaxLogicFrame", (PyCFunction) gPyGetMaxLogicFrame, METH_NOARGS, (PY_METHODCHAR)"Gets the max number of logic frame per render frame"},
+	{"setMaxLogicFrame", (PyCFunction) gPySetMaxLogicFrame, METH_VARARGS, (PY_METHODCHAR)"Sets the max number of logic frame per render frame"},
 	{"getLogicTicRate", (PyCFunction) gPyGetLogicTicRate, METH_NOARGS, (PY_METHODCHAR)"Gets the logic tic rate"},
 	{"setLogicTicRate", (PyCFunction) gPySetLogicTicRate, METH_VARARGS, (PY_METHODCHAR)"Sets the logic tic rate"},
 	{"getPhysicsTicRate", (PyCFunction) gPyGetPhysicsTicRate, METH_NOARGS, (PY_METHODCHAR)"Gets the physics tic rate"},

Modified: trunk/blender/source/gameengine/Physics/BlOde/OdePhysicsEnvironment.cpp
===================================================================
--- trunk/blender/source/gameengine/Physics/BlOde/OdePhysicsEnvironment.cpp	2009-05-01 15:19:47 UTC (rev 20018)
+++ trunk/blender/source/gameengine/Physics/BlOde/OdePhysicsEnvironment.cpp	2009-05-01 16:35:06 UTC (rev 20019)
@@ -75,7 +75,7 @@
 
 
 
-bool		ODEPhysicsEnvironment::proceedDeltaTime(double  curTime,float timeStep1)
+bool		ODEPhysicsEnvironment::proceedDeltaTime(double  curTime,float timeStep1,float interval)
 {
 
 	float deltaTime = timeStep1;

Modified: trunk/blender/source/gameengine/Physics/BlOde/OdePhysicsEnvironment.h
===================================================================
--- trunk/blender/source/gameengine/Physics/BlOde/OdePhysicsEnvironment.h	2009-05-01 15:19:47 UTC (rev 20018)
+++ trunk/blender/source/gameengine/Physics/BlOde/OdePhysicsEnvironment.h	2009-05-01 16:35:06 UTC (rev 20019)
@@ -44,7 +44,7 @@
 
 
 // Perform an integration step of duration 'timeStep'.
-	virtual	bool		proceedDeltaTime(double  curTime,float timeStep);
+	virtual	bool		proceedDeltaTime(double  curTime,float timeStep,float interval);
 	virtual	void		setFixedTimeStep(bool useFixedTimeStep,float fixedTimeStep);
 	virtual	float		getFixedTimeStep();
 

Modified: trunk/blender/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp
===================================================================
--- trunk/blender/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp	2009-05-01 15:19:47 UTC (rev 20018)
+++ trunk/blender/source/gameengine/Physics/Bullet/CcdPhysicsEnvironment.cpp	2009-05-01 16:35:06 UTC (rev 20019)
@@ -623,7 +623,7 @@
 			m_dynamicsWorld->debugDrawWorld();
 }
 
-bool	CcdPhysicsEnvironment::proceedDeltaTime(double curTime,float timeStep)
+bool	CcdPhysicsEnvironment::proceedDeltaTime(double curTime,float timeStep,float interval)
 {
 	std::set<CcdPhysicsController*>::iterator it;
 	int i;
@@ -633,14 +633,9 @@
 		(*it)->SynchronizeMotionStates(timeStep);
 	}
 
-	processFhSprings(curTime,timeStep);
-
 	float subStep = timeStep / float(m_numTimeSubSteps);
-	for (i=0;i<m_numTimeSubSteps;i++)
-	{
-//			m_dynamicsWorld->stepSimulation(subStep,20,1./240.);//perform always a full simulation step
-			m_dynamicsWorld->stepSimulation(subStep,0);//perform always a full simulation step
-	}
+	i = m_dynamicsWorld->stepSimulation(interval,25,subStep);//perform always a full simulation step
+	processFhSprings(curTime,i*subStep);
 
 	for (it=m_controllers.begin(); it!=m_controllers.end(); it++)
 	{
@@ -692,9 +687,11 @@
 
 };
 
-void	CcdPhysicsEnvironment::processFhSprings(double curTime,float timeStep)
+void	CcdPhysicsEnvironment::processFhSprings(double curTime,float interval)
 {
 	std::set<CcdPhysicsController*>::iterator it;
+	// dynamic of Fh spring is based on a timestep of 1/60
+	int numIter = (int)(interval*60.0001f);
 	
 	for (it=m_controllers.begin(); it!=m_controllers.end(); it++)
 	{
@@ -706,8 +703,6 @@
 			//printf("has Fh or RotFh\n");
 			//re-implement SM_FhObject.cpp using btCollisionWorld::rayTest and info from ctrl->getConstructionInfo()
 			//send a ray from {0.0, 0.0, 0.0} towards {0.0, 0.0, -10.0}, in local coordinates
-
-			
 			CcdPhysicsController* parentCtrl = ctrl->getParentCtrl();
 			btRigidBody* parentBody = parentCtrl?parentCtrl->GetRigidBody() : 0;
 			btRigidBody* cl_object = parentBody ? parentBody : body;
@@ -754,82 +749,78 @@
 					btVector3 normal = resultCallback.m_hitNormalWorld;
 					normal.normalize();
 
-					
-					if (ctrl->getConstructionInfo().m_do_fh) 
+					for (int i=0; i<numIter; i++)
 					{
-						btVector3 lspot = cl_object->getCenterOfMassPosition()
-							+ rayDirLocal * resultCallback.m_closestHitFraction;
+						if (ctrl->getConstructionInfo().m_do_fh) 
+						{
+							btVector3 lspot = cl_object->getCenterOfMassPosition()
+								+ rayDirLocal * resultCallback.m_closestHitFraction;
 
 
+								
+
+							lspot -= hit_object->getCenterOfMassPosition();
+							btVector3 rel_vel = cl_object->getLinearVelocity() - hit_object->getVelocityInLocalPoint(lspot);
+							btScalar rel_vel_ray = ray_dir.dot(rel_vel);
+							btScalar spring_extent = 1.0 - distance / hitObjShapeProps.m_fh_distance; 
 							
+							btScalar i_spring = spring_extent * hitObjShapeProps.m_fh_spring;
+							btScalar i_damp =   rel_vel_ray * hitObjShapeProps.m_fh_damping;
+							
+							cl_object->setLinearVelocity(cl_object->getLinearVelocity() + (-(i_spring + i_damp) * ray_dir)); 

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list