CPhysic.cpp
资源名称:brm2.rar [点击查看]
上传用户:liujun12jf
上传日期:2022-07-12
资源大小:638k
文件大小:2k
源码类别:
OpenGL
开发平台:
Visual C++
- #include "CPhysic.h"
- int GCBegin(const NewtonMaterial* material, const NewtonBody* body0, const NewtonBody* body1)
- {
- return 1;
- }
- int GCProcess(const NewtonMaterial* material, const NewtonContact* contact)
- {
- return 1;
- }
- void GCEnd(const NewtonMaterial* material)
- {
- }
- void CPhysic::AddObject(CPhysicObject *o)
- {
- NewtonCollision *c;
- NewtonBody *b;
- if (o->isBox) c = NewtonCreateBox(nWorld, o->size[0]*2, o->size[1]*2, o->size[2]*2, NULL);
- else c = NewtonCreateSphere(nWorld, o->size[0]*2, o->size[1]*2, o->size[2]*2, NULL);
- b = NewtonCreateBody(nWorld, c);
- NewtonReleaseCollision(nWorld, c);
- NewtonBodySetUserData(b, o);
- NewtonBodySetMassMatrix(b, o->weight, 1, 1, 1);
- NewtonBodySetMatrix(b, &o->mMatrix[0][0]);
- NewtonBodySetTransformCallback(b, TransformCallback);
- NewtonBodySetForceAndTorqueCallback(b, ForceAndTorqueCallback);
- NewtonBodySetAutoactiveCallback(b, AutoactiveCallback);
- o->pBody = b;
- }
- void CPhysic::Frame(float fTime)
- {
- // update world with timestep
- NewtonUpdate(nWorld, fTime*fSpeed);
- }
- void AutoactiveCallback(const NewtonBody *b, unsigned state)
- {
- static int iActiveBodies = 0;
- if (state == -1)
- {
- int *p;
- p = (int*)b;
- *p = iActiveBodies;
- }
- else
- {
- // 0*2-1 = -1 && 1*2-1 = 1 && so it will inc or dec count..
- iActiveBodies += (state*2)-1;
- }
- }
- void ForceAndTorqueCallback(const NewtonBody* body)
- {
- float gravity = 9.8f;
- float Ixx;
- float Iyy;
- float Izz;
- float mass;
- NewtonBodyGetMassMatrix(body, &mass, &Ixx, &Iyy, &Izz);
- CVector force(0.0f, -mass * gravity, 0.0f);
- NewtonBodySetForce(body, &force[0]);
- }
- void TransformCallback(const NewtonBody* b, const dFloat* m)
- {
- CPhysicObject *o;
- // get pointer to object, which we saved before
- o = (CPhysicObject*) NewtonBodyGetUserData(b);
- if (!o)
- {
- err("NewtonBodyGetUserData returned zero...");
- return;
- }
- // CMatrix& mat = *((dMatrix*)matrix);
- CMatrix mat;
- // set current matrix
- mat = *((CMatrix*)m);
- o->SetMatrix(mat);
- }
- void NewtonBodyUnfreeze(NewtonBody *b)
- {
- if (!b) return;
- NewtonWorld *pNW;
- pNW = NewtonBodyGetWorld(b);
- if (!pNW) return;
- NewtonWorldUnfreezeBody(pNW, b);
- }
- void NewtonBodyDestroy(NewtonBody *b)
- {
- if (!b) return;
- NewtonWorld *pNW;
- pNW = NewtonBodyGetWorld(b);
- if (!pNW) return;
- NewtonDestroyBody(pNW, b);
- }
English
