Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: migrate CombineObjectsAndCreatePhysics with structure view #122

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions HPL2/include/graphics/GraphicsBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ namespace hpl {
return std::span<T>(reinterpret_cast<T*>(reinterpret_cast<uint8_t*>(m_asset->m_mapped.m_mappedData) + m_byteOffset), (m_asset->m_mapped.m_size - m_byteOffset) / sizeof(T));
}
}
return std::span<T>();

}
inline std::span<uint8_t> rawByteSpan() {
switch(m_asset->m_type) {
Expand Down
4 changes: 1 addition & 3 deletions HPL2/include/impl/CollideShapeNewton.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ namespace hpl {

NewtonCollision* GetNewtonCollision(){ return mpNewtonCollision;}

private:
NewtonCollision* mpNewtonCollision;
NewtonCollision* mpNewtonCollision;
NewtonWorld *mpNewtonWorld;

tCollideShapeVec mvSubShapes;
};
};
Expand Down
77 changes: 41 additions & 36 deletions HPL2/include/impl/PhysicsWorldNewton.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,75 +34,80 @@ namespace hpl {
cPhysicsWorldNewton();
~cPhysicsWorldNewton();

void Simulate(float afTimeStep);
void Simulate(float afTimeStep) override;

void SetMaxTimeStep(float afTimeStep);
float GetMaxTimeStep();
void SetMaxTimeStep(float afTimeStep) override;
float GetMaxTimeStep() override;

void SetWorldSize(const cVector3f &avMin,const cVector3f &avMax);
cVector3f GetWorldSizeMin();
cVector3f GetWorldSizeMax();
void SetWorldSize(const cVector3f &avMin,const cVector3f &avMax) override;
cVector3f GetWorldSizeMin() override;
cVector3f GetWorldSizeMax() override;

void SetGravity(const cVector3f& avGravity);
cVector3f GetGravity();
void SetGravity(const cVector3f& avGravity) override;
cVector3f GetGravity() override;

void SetAccuracyLevel(ePhysicsAccuracy aAccuracy);
ePhysicsAccuracy GetAccuracyLevel();
void SetAccuracyLevel(ePhysicsAccuracy aAccuracy) override;
ePhysicsAccuracy GetAccuracyLevel() override;

void SetNumberOfThreads(int alThreads);
int GetNumberOfThreads();
void SetNumberOfThreads(int alThreads) override;
int GetNumberOfThreads() override;

iCollideShape* CreateNullShape();
iCollideShape* CreateBoxShape(const cVector3f &avSize, cMatrixf* apOffsetMtx);
iCollideShape* CreateSphereShape(const cVector3f &avRadii, cMatrixf* apOffsetMtx);
iCollideShape* CreateCylinderShape(float afRadius, float afHeight, cMatrixf* apOffsetMtx);
iCollideShape* CreateCapsuleShape(float afRadius, float afHeight, cMatrixf* apOffsetMtx);
iCollideShape* CreateNullShape() override;
iCollideShape* CreateBoxShape(const cVector3f &avSize, cMatrixf* apOffsetMtx) override;
iCollideShape* CreateSphereShape(const cVector3f &avRadii, cMatrixf* apOffsetMtx) override;
iCollideShape* CreateCylinderShape(float afRadius, float afHeight, cMatrixf* apOffsetMtx) override;
iCollideShape* CreateCapsuleShape(float afRadius, float afHeight, cMatrixf* apOffsetMtx) override;

iCollideShape* CreateMeshShape(iVertexBuffer *apVtxBuffer);
iCollideShape* LoadMeshShapeFromBuffer(cBinaryBuffer *apBuffer);
void SaveMeshShapeToBuffer(iCollideShape* apMeshShape, cBinaryBuffer *apBuffer);
iCollideShape* CreateMeshShape(iVertexBuffer *apVtxBuffer) override;
iCollideShape* CreateMeshShape(uint32_t numberIndecies,
uint32_t vertexOffset, uint32_t indexOffset,
GraphicsBuffer::BufferIndexView indexView,
GraphicsBuffer::BufferStructuredView<float3> position) override;

iCollideShape* CreateCompundShape(tCollideShapeVec &avShapes);
iCollideShape* CreateStaticSceneShape(tCollideShapeVec &avShapes, tMatrixfVec *apMatrices);
iCollideShape* LoadMeshShapeFromBuffer(cBinaryBuffer *apBuffer) override;
void SaveMeshShapeToBuffer(iCollideShape* apMeshShape, cBinaryBuffer *apBuffer) override;

iCollideShape* CreateCompundShape(tCollideShapeVec &avShapes) override;
iCollideShape* CreateStaticSceneShape(tCollideShapeVec &avShapes, tMatrixfVec *apMatrices) override;

iPhysicsJointBall* CreateJointBall(const tString &asName,const cVector3f& avPivotPoint,
const cVector3f& avPinDir,
iPhysicsBody* apParentBody, iPhysicsBody *apChildBody);
iPhysicsBody* apParentBody, iPhysicsBody *apChildBody) override;
iPhysicsJointHinge* CreateJointHinge(const tString &asName,const cVector3f& avPivotPoint,
const cVector3f& avPinDir,
iPhysicsBody* apParentBody, iPhysicsBody *apChildBody);
iPhysicsBody* apParentBody, iPhysicsBody *apChildBody) override;
iPhysicsJointSlider* CreateJointSlider(const tString &asName,const cVector3f& avPivotPoint,
const cVector3f& avPinDir,
iPhysicsBody* apParentBody, iPhysicsBody *apChildBody);
iPhysicsBody* apParentBody, iPhysicsBody *apChildBody) override;
iPhysicsJointScrew* CreateJointScrew(const tString &asName,const cVector3f& avPivotPoint,
const cVector3f& avPinDir,
iPhysicsBody* apParentBody, iPhysicsBody *apChildBody);
iPhysicsBody* apParentBody, iPhysicsBody *apChildBody) override;

iPhysicsBody* CreateBody(const tString &asName,iCollideShape *apShape);
iPhysicsBody* CreateBody(const tString &asName,iCollideShape *apShape) override;

void GetBodiesInBV(cBoundingVolume *apBV, std::vector<iPhysicsBody*> *apBodyVec);
void GetBodiesInBV(cBoundingVolume *apBV, std::vector<iPhysicsBody*> *apBodyVec) override;

iCharacterBody *CreateCharacterBody(const tString &asName, const cVector3f &avSize);
iCharacterBody *CreateCharacterBody(const tString &asName, const cVector3f &avSize) override;

iPhysicsMaterial* CreateMaterial(const tString &asName);
iPhysicsMaterial* CreateMaterial(const tString &asName) override;

iPhysicsController *CreateController(const tString &asName);
iPhysicsController *CreateController(const tString &asName) override;

iPhysicsRope* CreateRope(const tString &asName, const cVector3f &avStartPos, const cVector3f &avEndPos);
iPhysicsRope* CreateRope(const tString &asName, const cVector3f &avStartPos, const cVector3f &avEndPos) override;

void CastRay(iPhysicsRayCallback *apCallback,
const cVector3f &avOrigin, const cVector3f& avEnd,
bool abCalcDist, bool abCalcNormal, bool abCalcPoint,
bool abUsePrefilter = false);
bool abUsePrefilter = false) override;

bool CheckShapeCollision( iCollideShape* apShapeA, const cMatrixf& a_mtxA,
iCollideShape* apShapeB, const cMatrixf& a_mtxB,
cCollideData & aCollideData, int alMaxPoints,
bool abCorrectNormalDirection);
bool abCorrectNormalDirection) override;

void RenderShapeDebugGeometry( iCollideShape *apShape, const cMatrixf& a_mtxTransform,
DebugDraw *apLowLevel, const cColor& aColor);
void RenderDebugGeometry(DebugDraw *apLowLevel, const cColor& aColor);
DebugDraw *apLowLevel, const cColor& aColor) override;
void RenderDebugGeometry(DebugDraw *apLowLevel, const cColor& aColor) override;

NewtonWorld* GetNewtonWorld(){ return mpNewtonWorld;}
private:
Expand Down
7 changes: 4 additions & 3 deletions HPL2/include/physics/PhysicsWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ namespace hpl {
virtual iCollideShape* CreateCylinderShape(float afRadius, float afHeight, cMatrixf* apOffsetMtx)=0;
virtual iCollideShape* CreateCapsuleShape(float afRadius, float afHeight, cMatrixf* apOffsetMtx)=0;
virtual iCollideShape* CreateMeshShape(iVertexBuffer *apVtxBuffer)=0;
//virtual iCollideShape* CreateMeshShape(size_t numberVerticies,
// GraphicsBuffer::BufferIndexView indexView,
// GraphicsBuffer::BufferStructuredView<float3> position)=0;
virtual iCollideShape* CreateMeshShape(uint32_t numberIndecies,
uint32_t vertexOffset, uint32_t indexOffset,
GraphicsBuffer::BufferIndexView indexView,
GraphicsBuffer::BufferStructuredView<float3> position)=0;
/**
* The buffer position must be pointing to where the data is saved!
*/
Expand Down
52 changes: 0 additions & 52 deletions HPL2/sources/impl/CollideShapeNewton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,7 @@ namespace hpl {

int lCount=0;

//unsigned long lStartTime = cPlatform::GetApplicationTime();
//Log("%d triangles: ", alIndexNum/3);

mpNewtonCollision = NewtonCreateTreeCollision(mpNewtonWorld, 0);
//Log("-- Creating mesh collision.:\n");
NewtonTreeCollisionBeginBuild(mpNewtonCollision);
for(int tri = 0; tri < alIndexNum; tri+=3)
{
Expand All @@ -324,54 +320,6 @@ namespace hpl {
vTriVec[idx*3 + 1] = apVertexArray[lVtx + 1];
vTriVec[idx*3 + 2] = apVertexArray[lVtx + 2];
}

//if(alIndexNum<100)
//{
// for(int i=0;i<3; ++i) Log("%f, %f, %f, ", vTriVec[i*3 + 0],vTriVec[i*3 + 1],vTriVec[i*3 + 2]);
// Log("\n");
//}
//lCount+=9;

//cannot allow negative zeros! it is true!
/*for(int i=0;i<9; ++i)
{
if(fabs(vTriVec[i]-0.0f) < 0.000001f) vTriVec[i] = 0;
}*/

if(false)//bOptimize==false)
{
cPlanef tempPlane;
cVector3f vP1(vTriVec[0+0],vTriVec[0+1],vTriVec[0+2]);
cVector3f vP2(vTriVec[1*3+0],vTriVec[1*3+1],vTriVec[1*3+2]);
cVector3f vP3(vTriVec[2*3+0],vTriVec[2*3+1],vTriVec[2*3+2]);

tempPlane.FromPoints(vP1, vP2, vP3);

//Log("P1: %s P2: %s P3: %s\n",vP1.ToString().c_str(),vP2.ToString().c_str(),vP3.ToString().c_str());
//Log("Plane: a: %f b: %f c: %f d: %f\n",tempPlane.a,tempPlane.b,tempPlane.c,tempPlane.d);

/////////////////////////////////
//Check if the triangles are on the same plane. If so, do not optimize
// Do not check first triangle
if(bCreatedPlane==false)
{
plane = tempPlane;
bCreatedPlane = true;
}
// First plane created.
else
{
//Check the plane equation is different from the first one (allow a small difference thoug).
if( std::abs(plane.a - tempPlane.a) > 0.001f ||
std::abs(plane.b - tempPlane.b) > 0.001f ||
std::abs(plane.c - tempPlane.c) > 0.001f ||
std::abs(plane.d - tempPlane.d) > 0.001f )
{
bOptimize = true;
}
}
}

NewtonTreeCollisionAddFace(mpNewtonCollision,3,vTriVec,sizeof(float)*3,1);
}

Expand Down
40 changes: 40 additions & 0 deletions HPL2/sources/impl/PhysicsWorldNewton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,45 @@ namespace hpl {
return pShape;
}


iCollideShape* cPhysicsWorldNewton::CreateMeshShape(uint32_t numberIndecies,
uint32_t vertexOffset, uint32_t indexOffset,
GraphicsBuffer::BufferIndexView indexView,
GraphicsBuffer::BufferStructuredView<float3> position) {
cCollideShapeNewton* pShape = hplNew(cCollideShapeNewton, (eCollideShapeType_Mesh, 0, NULL, mpNewtonWorld, this));

Vector3 minLocal = Vector3(100000, 100000, 100000);
Vector3 maxLocal = Vector3(-100000, -100000, -100000);
pShape->mpNewtonCollision = NewtonCreateTreeCollision(mpNewtonWorld, 0);
NewtonTreeCollisionBeginBuild(pShape->mpNewtonCollision);
for (size_t i = 0; i < numberIndecies; i += 3) {
float3 p1 = position.Get(vertexOffset + indexView.Get(indexOffset + i));
float3 p2 = position.Get(vertexOffset + indexView.Get(indexOffset + i + 1));
float3 p3 = position.Get(vertexOffset + indexView.Get(indexOffset + i + 2));

maxLocal = maxPerElem(f3Tov3(p1), maxLocal);
minLocal = minPerElem(f3Tov3(p1), minLocal);

maxLocal = maxPerElem(f3Tov3(p2), maxLocal);
minLocal = minPerElem(f3Tov3(p2), minLocal);

maxLocal = maxPerElem(f3Tov3(p3), maxLocal);
minLocal = minPerElem(f3Tov3(p3), minLocal);

float face[3 * 3] = {
p1.x, p1.y, p1.z,
p2.x, p2.y, p2.z,
p3.x, p3.y, p3.z
};
NewtonTreeCollisionAddFace(pShape->mpNewtonCollision, 3, face, sizeof(float) * 3, 1);
}
NewtonTreeCollisionEndBuild(pShape->mpNewtonCollision, 0);

pShape->GetBoundingVolume().SetLocalMinMax(cMath::FromForgeVector3(minLocal), cMath::FromForgeVector3(maxLocal));
mlstShapes.push_back(pShape);
return pShape;
}

//-----------------------------------------------------------------------

iCollideShape* cPhysicsWorldNewton::LoadMeshShapeFromBuffer(cBinaryBuffer *apBuffer)
Expand All @@ -308,6 +347,7 @@ namespace hpl {
pNewtonMeshShape->SaveToSerializedData(apBuffer);
}


//-----------------------------------------------------------------------

iCollideShape* cPhysicsWorldNewton::CreateCompundShape(tCollideShapeVec &avShapes)
Expand Down
Loading
Loading