2015-07-14 22:30:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "PhysicsServerExample.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-11-23 04:50:32 +00:00
|
|
|
#include "PhysicsServerSharedMemory.h"
|
2015-07-14 22:30:17 +00:00
|
|
|
|
|
|
|
#include "SharedMemoryCommon.h"
|
|
|
|
|
|
|
|
|
|
|
|
class PhysicsServerExample : public SharedMemoryCommon
|
|
|
|
{
|
|
|
|
PhysicsServerSharedMemory m_physicsServer;
|
|
|
|
|
2015-08-06 18:59:31 +00:00
|
|
|
bool m_wantsShutdown;
|
2015-07-14 22:30:17 +00:00
|
|
|
|
2015-08-06 18:59:31 +00:00
|
|
|
bool m_isConnected;
|
2015-09-03 21:18:22 +00:00
|
|
|
btClock m_clock;
|
2015-11-01 20:48:15 +00:00
|
|
|
bool m_replay;
|
2015-07-14 22:30:17 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2016-03-10 22:36:46 +00:00
|
|
|
PhysicsServerExample(GUIHelperInterface* helper, SharedMemoryInterface* sharedMem=0);
|
2015-07-14 22:30:17 +00:00
|
|
|
|
|
|
|
virtual ~PhysicsServerExample();
|
|
|
|
|
|
|
|
virtual void initPhysics();
|
|
|
|
|
|
|
|
virtual void stepSimulation(float deltaTime);
|
|
|
|
|
2015-10-29 18:25:50 +00:00
|
|
|
void enableCommandLogging()
|
|
|
|
{
|
|
|
|
m_physicsServer.enableCommandLogging(true,"BulletPhysicsCommandLog.bin");
|
|
|
|
}
|
2015-10-30 17:30:48 +00:00
|
|
|
|
|
|
|
void replayFromLogFile()
|
|
|
|
{
|
2015-11-01 20:48:15 +00:00
|
|
|
m_replay = true;
|
2015-10-30 17:30:48 +00:00
|
|
|
m_physicsServer.replayFromLogFile("BulletPhysicsCommandLog.bin");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-14 22:30:17 +00:00
|
|
|
|
|
|
|
virtual void resetCamera()
|
|
|
|
{
|
|
|
|
float dist = 5;
|
|
|
|
float pitch = 50;
|
|
|
|
float yaw = 35;
|
|
|
|
float targetPos[3]={0,0,0};//-3,2.8,-2.5};
|
|
|
|
m_guiHelper->resetCamera(dist,pitch,yaw,targetPos[0],targetPos[1],targetPos[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool wantsTermination();
|
2015-08-06 18:59:31 +00:00
|
|
|
virtual bool isConnected();
|
2015-08-07 07:13:26 +00:00
|
|
|
virtual void renderScene();
|
|
|
|
virtual void exitPhysics(){}
|
|
|
|
|
|
|
|
virtual void physicsDebugDraw(int debugFlags);
|
2015-09-04 18:28:08 +00:00
|
|
|
|
|
|
|
btVector3 getRayTo(int x,int y);
|
|
|
|
|
|
|
|
virtual bool mouseMoveCallback(float x,float y)
|
|
|
|
{
|
2015-11-01 20:48:15 +00:00
|
|
|
if (m_replay)
|
|
|
|
return false;
|
|
|
|
|
2015-09-04 18:28:08 +00:00
|
|
|
CommonRenderInterface* renderer = m_guiHelper->getRenderInterface();
|
|
|
|
|
|
|
|
if (!renderer)
|
|
|
|
{
|
|
|
|
btAssert(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
btVector3 rayTo = getRayTo(int(x), int(y));
|
|
|
|
btVector3 rayFrom;
|
|
|
|
renderer->getActiveCamera()->getCameraPosition(rayFrom);
|
|
|
|
m_physicsServer.movePickedBody(rayFrom,rayTo);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual bool mouseButtonCallback(int button, int state, float x, float y)
|
|
|
|
{
|
2015-11-01 20:48:15 +00:00
|
|
|
if (m_replay)
|
|
|
|
return false;
|
|
|
|
|
2015-09-04 18:28:08 +00:00
|
|
|
CommonRenderInterface* renderer = m_guiHelper->getRenderInterface();
|
|
|
|
|
|
|
|
if (!renderer)
|
|
|
|
{
|
|
|
|
btAssert(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CommonWindowInterface* window = m_guiHelper->getAppInterface()->m_window;
|
|
|
|
|
|
|
|
|
|
|
|
if (state==1)
|
|
|
|
{
|
|
|
|
if(button==0 && (!window->isModifierKeyPressed(B3G_ALT) && !window->isModifierKeyPressed(B3G_CONTROL) ))
|
|
|
|
{
|
|
|
|
btVector3 camPos;
|
|
|
|
renderer->getActiveCamera()->getCameraPosition(camPos);
|
|
|
|
|
|
|
|
btVector3 rayFrom = camPos;
|
|
|
|
btVector3 rayTo = getRayTo(int(x),int(y));
|
|
|
|
|
|
|
|
m_physicsServer.pickBody(rayFrom, rayTo);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
if (button==0)
|
|
|
|
{
|
|
|
|
m_physicsServer.removePickingConstraint();
|
|
|
|
//remove p2p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//printf("button=%d, state=%d\n",button,state);
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-07 07:13:26 +00:00
|
|
|
virtual bool keyboardCallback(int key, int state){return false;}
|
|
|
|
|
|
|
|
virtual void setSharedMemoryKey(int key)
|
|
|
|
{
|
|
|
|
m_physicsServer.setSharedMemoryKey(key);
|
|
|
|
}
|
|
|
|
|
2015-08-06 18:59:31 +00:00
|
|
|
|
2015-07-14 22:30:17 +00:00
|
|
|
};
|
|
|
|
|
2016-03-10 22:36:46 +00:00
|
|
|
PhysicsServerExample::PhysicsServerExample(GUIHelperInterface* helper, SharedMemoryInterface* sharedMem)
|
2015-07-14 22:30:17 +00:00
|
|
|
:SharedMemoryCommon(helper),
|
2016-03-10 22:36:46 +00:00
|
|
|
m_physicsServer(sharedMem),
|
2015-08-06 18:59:31 +00:00
|
|
|
m_wantsShutdown(false),
|
2015-11-01 20:48:15 +00:00
|
|
|
m_isConnected(false),
|
|
|
|
m_replay(false)
|
2015-07-14 22:30:17 +00:00
|
|
|
{
|
|
|
|
b3Printf("Started PhysicsServer\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PhysicsServerExample::~PhysicsServerExample()
|
|
|
|
{
|
|
|
|
bool deInitializeSharedMemory = true;
|
|
|
|
m_physicsServer.disconnectSharedMemory(deInitializeSharedMemory);
|
2015-08-06 18:59:31 +00:00
|
|
|
m_isConnected = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhysicsServerExample::isConnected()
|
|
|
|
{
|
|
|
|
return m_isConnected;
|
2015-07-14 22:30:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsServerExample::initPhysics()
|
|
|
|
{
|
|
|
|
///for this testing we use Z-axis up
|
|
|
|
int upAxis = 2;
|
|
|
|
m_guiHelper->setUpAxis(upAxis);
|
|
|
|
|
2015-08-07 07:13:26 +00:00
|
|
|
#if 0
|
|
|
|
|
2015-07-14 22:30:17 +00:00
|
|
|
createEmptyDynamicsWorld();
|
2015-08-07 07:13:26 +00:00
|
|
|
|
2015-07-14 22:30:17 +00:00
|
|
|
//todo: create a special debug drawer that will cache the lines, so we can send the debug info over the wire
|
|
|
|
btVector3 grav(0,0,0);
|
|
|
|
grav[upAxis] = 0;//-9.8;
|
|
|
|
this->m_dynamicsWorld->setGravity(grav);
|
|
|
|
|
2015-08-07 07:13:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
m_isConnected = m_physicsServer.connectSharedMemory( m_guiHelper);
|
2015-07-14 22:30:17 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool PhysicsServerExample::wantsTermination()
|
|
|
|
{
|
|
|
|
return m_wantsShutdown;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PhysicsServerExample::stepSimulation(float deltaTime)
|
|
|
|
{
|
2015-11-01 20:48:15 +00:00
|
|
|
if (m_replay)
|
2015-09-08 20:39:09 +00:00
|
|
|
{
|
2015-11-01 20:48:15 +00:00
|
|
|
for (int i=0;i<100;i++)
|
|
|
|
m_physicsServer.processClientCommands();
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
btClock rtc;
|
|
|
|
btScalar endTime = rtc.getTimeMilliseconds() + deltaTime*btScalar(800);
|
|
|
|
|
|
|
|
while (rtc.getTimeMilliseconds()<endTime)
|
|
|
|
{
|
|
|
|
m_physicsServer.processClientCommands();
|
|
|
|
}
|
2015-09-08 20:39:09 +00:00
|
|
|
}
|
2015-07-14 22:30:17 +00:00
|
|
|
}
|
|
|
|
|
2015-08-07 07:13:26 +00:00
|
|
|
void PhysicsServerExample::renderScene()
|
|
|
|
{
|
|
|
|
///debug rendering
|
|
|
|
m_physicsServer.renderScene();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhysicsServerExample::physicsDebugDraw(int debugDrawFlags)
|
|
|
|
{
|
|
|
|
///debug rendering
|
|
|
|
m_physicsServer.physicsDebugDraw(debugDrawFlags);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-07-14 22:30:17 +00:00
|
|
|
|
|
|
|
|
2015-09-04 18:28:08 +00:00
|
|
|
btVector3 PhysicsServerExample::getRayTo(int x,int y)
|
|
|
|
{
|
|
|
|
CommonRenderInterface* renderer = m_guiHelper->getRenderInterface();
|
|
|
|
|
|
|
|
if (!renderer)
|
|
|
|
{
|
|
|
|
btAssert(0);
|
|
|
|
return btVector3(0,0,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
float top = 1.f;
|
|
|
|
float bottom = -1.f;
|
|
|
|
float nearPlane = 1.f;
|
|
|
|
float tanFov = (top-bottom)*0.5f / nearPlane;
|
|
|
|
float fov = btScalar(2.0) * btAtan(tanFov);
|
|
|
|
|
|
|
|
btVector3 camPos,camTarget;
|
|
|
|
renderer->getActiveCamera()->getCameraPosition(camPos);
|
|
|
|
renderer->getActiveCamera()->getCameraTargetPosition(camTarget);
|
|
|
|
|
|
|
|
btVector3 rayFrom = camPos;
|
|
|
|
btVector3 rayForward = (camTarget-camPos);
|
|
|
|
rayForward.normalize();
|
|
|
|
float farPlane = 10000.f;
|
|
|
|
rayForward*= farPlane;
|
|
|
|
|
|
|
|
btVector3 rightOffset;
|
|
|
|
btVector3 cameraUp=btVector3(0,0,0);
|
|
|
|
cameraUp[m_guiHelper->getAppInterface()->getUpAxis()]=1;
|
|
|
|
|
|
|
|
btVector3 vertical = cameraUp;
|
|
|
|
|
|
|
|
btVector3 hor;
|
|
|
|
hor = rayForward.cross(vertical);
|
|
|
|
hor.normalize();
|
|
|
|
vertical = hor.cross(rayForward);
|
|
|
|
vertical.normalize();
|
|
|
|
|
|
|
|
float tanfov = tanf(0.5f*fov);
|
|
|
|
|
|
|
|
|
|
|
|
hor *= 2.f * farPlane * tanfov;
|
|
|
|
vertical *= 2.f * farPlane * tanfov;
|
|
|
|
|
|
|
|
btScalar aspect;
|
|
|
|
float width = float(renderer->getScreenWidth());
|
|
|
|
float height = float (renderer->getScreenHeight());
|
|
|
|
|
|
|
|
aspect = width / height;
|
|
|
|
|
|
|
|
hor*=aspect;
|
|
|
|
|
|
|
|
|
|
|
|
btVector3 rayToCenter = rayFrom + rayForward;
|
|
|
|
btVector3 dHor = hor * 1.f/width;
|
|
|
|
btVector3 dVert = vertical * 1.f/height;
|
|
|
|
|
|
|
|
|
|
|
|
btVector3 rayTo = rayToCenter - 0.5f * hor + 0.5f * vertical;
|
|
|
|
rayTo += btScalar(x) * dHor;
|
|
|
|
rayTo -= btScalar(y) * dVert;
|
|
|
|
return rayTo;
|
|
|
|
}
|
2015-07-14 22:30:17 +00:00
|
|
|
|
2015-10-29 18:25:50 +00:00
|
|
|
|
|
|
|
extern int gSharedMemoryKey;
|
|
|
|
|
|
|
|
class CommonExampleInterface* PhysicsServerCreateFunc(struct CommonExampleOptions& options)
|
|
|
|
{
|
2016-03-10 22:36:46 +00:00
|
|
|
PhysicsServerExample* example = new PhysicsServerExample(options.m_guiHelper, options.m_sharedMem);
|
2015-10-29 18:25:50 +00:00
|
|
|
if (gSharedMemoryKey>=0)
|
|
|
|
{
|
|
|
|
example->setSharedMemoryKey(gSharedMemoryKey);
|
|
|
|
}
|
|
|
|
if (options.m_option & PHYSICS_SERVER_ENABLE_COMMAND_LOGGING)
|
|
|
|
{
|
|
|
|
example->enableCommandLogging();
|
|
|
|
}
|
2015-10-30 17:30:48 +00:00
|
|
|
if (options.m_option & PHYSICS_SERVER_REPLAY_FROM_COMMAND_LOG)
|
|
|
|
{
|
|
|
|
example->replayFromLogFile();
|
|
|
|
}
|
2015-10-29 18:25:50 +00:00
|
|
|
return example;
|
|
|
|
|
|
|
|
}
|