Skip to content

Commit

Permalink
try the tippeTop again, but I can't get to work with convex hull shape.
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJerez committed Nov 9, 2024
1 parent b02b8ee commit 8c43e61
Show file tree
Hide file tree
Showing 6 changed files with 280 additions and 6 deletions.
67 changes: 66 additions & 1 deletion newton-4.00/applications/ndSandbox/demos/ndBasicGyroTorque.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "ndSandboxStdafx.h"
#include "ndSkyBox.h"
#include "ndDemoMesh.h"
#include "ndMeshLoader.h"
#include "ndDemoCamera.h"
#include "ndPhysicsUtils.h"
#include "ndPhysicsWorld.h"
Expand All @@ -20,6 +21,67 @@
#include "ndDemoEntityManager.h"
#include "ndDemoCameraManager.h"

static void TippeTop(ndDemoEntityManager* const scene, ndFloat32 mass, ndFloat32 angularSpeed, const ndVector& origin)
{
class TippeTopShape : public ndShapeUserDefinedImplicit
{
public:
TippeTopShape()
:ndShapeUserDefinedImplicit()
{
}
};


//ndMatrix matrix(ndGetIdentityMatrix());
ndMatrix matrix(ndPitchMatrix(15.0f * ndDegreeToRad));
matrix.m_posit = origin;
matrix.m_posit.m_w = 1.0f;
matrix.m_posit.m_y += 1.0f;

ndMeshLoader loader;
ndSharedPtr<ndDemoEntity> modelMesh(loader.LoadEntity("tippeTop.fbx", scene));

ndSharedPtr<ndDemoMeshInterface> mesh (modelMesh->GetFirstChild()->GetMesh());
ndArray<ndVector> meshPoints;
((ndDemoMesh*)(*mesh))->GetVertexArray(meshPoints);
//ndArray<ndBigVector> points;
//for (ndInt32 i = ndInt32 (meshPoints.GetCount() - 1); i >= 0; --i)
//{
// points.PushBack(meshPoints[i]);
//}
//ndConvexHull3d hull(&points[0].m_x, sizeof(ndBigVector), ndInt32 (points.GetCount()), 1.0e-3f);
//ndShapeInstance shape(new ndShapeSphere(0.64f));
//ndShapeInstance shape (new TippeTopShape());
ndShapeInstance shape(new ndShapeConvexHull(ndInt32 (meshPoints.GetCount()), sizeof (ndVector), 0.0f, &meshPoints[0].m_x));

ndVector omega(matrix.m_up.Scale (angularSpeed));
ndBodyKinematic* const body = new ndBodyDynamic();
ndDemoEntity* const entity = modelMesh->CreateClone();
body->SetNotifyCallback(new ndDemoEntityNotify(scene, entity));

body->SetOmega(omega);
body->SetMatrix(matrix);
body->SetCollisionShape(shape);
body->SetMassMatrix(mass, shape);

ndVector inertia (body->GetMassMatrix());
inertia.m_y *= .5f;
//inertia.m_x *= 1.75f;
//inertia.m_z *= 1.75f;
body->SetMassMatrix(inertia);

ndVector com(body->GetCentreOfMass());
com.m_y -= 0.2f;
body->SetCentreOfMass(com);

ndSharedPtr<ndBody> bodyPtr(body);

ndPhysicsWorld* const world = scene->GetWorld();
world->AddBody(bodyPtr);
scene->AddEntity(entity);
}

static void DzhanibekovEffect(ndDemoEntityManager* const scene, ndFloat32 mass, ndFloat32 angularSpeed, const ndVector& origin)
{
ndMatrix matrix(ndGetIdentityMatrix());
Expand Down Expand Up @@ -214,10 +276,13 @@ void ndBasicAngularMomentum (ndDemoEntityManager* const scene)
PrecessingTop(scene, ndVector(5.0f, 0.0f, -4.0f, 0.0f));
PrecessingTop(scene, ndVector(5.0f, 0.0f, 0.0f, 0.0f));
PrecessingTop(scene, ndVector(5.0f, 0.0f, 4.0f, 0.0f));

RattleBack(scene, 10.0f, -5.0f, ndVector(0.0f, 0.0f, -4.0f, 0.0f));
RattleBack(scene, 10.0f, 5.0f, ndVector(0.0f, 0.0f, 0.0f, 0.0f));
RattleBack(scene, 10.0f, -5.0f, ndVector(0.0f, 0.0f, 4.0f, 0.0f));

// I can't get this to work.
//TippeTop(scene, 10.0f, 50.0f, ndVector(0.0f, 0.0f, -4.0f, 0.0f));

scene->GetCameraManager()->SetPickMode(true);

Expand Down
6 changes: 3 additions & 3 deletions newton-4.00/applications/ndSandbox/ndDemoEntityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
//#define DEFAULT_SCENE 1 // gpu basic rigidbody
//#define DEFAULT_SCENE 2 // friction ramp
//#define DEFAULT_SCENE 3 // basic compound shapes
//#define DEFAULT_SCENE 4 // conservation of momentum
#define DEFAULT_SCENE 4 // conservation of momentum
//#define DEFAULT_SCENE 5 // basic Stacks
//#define DEFAULT_SCENE 6 // basic Trigger
//#define DEFAULT_SCENE 7 // object Placement
Expand All @@ -57,7 +57,7 @@
//#define DEFAULT_SCENE 17 // cart pole discrete controller
//#define DEFAULT_SCENE 18 // cart pole continue controller
//#define DEFAULT_SCENE 19 // unit cycle controller
#define DEFAULT_SCENE 20 // simple industrial robot
//#define DEFAULT_SCENE 20 // simple industrial robot
//#define DEFAULT_SCENE 21 // advanced industrial robot
//#define DEFAULT_SCENE 22 // quadruped test 1
//#define DEFAULT_SCENE 23 // quadruped test 2
Expand Down Expand Up @@ -579,7 +579,7 @@ ndDemoEntityManager::ndDemoEntityManager()
//m_showJointDebugInfo = true;
m_showModelsDebugInfo = true;
//m_collisionDisplayMode = 1;
//m_collisionDisplayMode = 2;
m_collisionDisplayMode = 2;
//m_collisionDisplayMode = 3; // solid wire frame
m_synchronousPhysicsUpdate = true;
m_synchronousParticlesUpdate = true;
Expand Down
5 changes: 3 additions & 2 deletions newton-4.00/sdk/dCollision/ndCollision.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#include <ndBodyListView.h>
#include <ndContactArray.h>
#include <ndBodySphFluid.h>
#include "ndBodySphFluid_New.h"
#include <ndBodySphFluid_New.h>
#include <ndShapeCapsule.h>
#include <ndShapeCylinder.h>
#include <ndBodyKinematic.h>
Expand All @@ -65,10 +65,11 @@
#include <ndConvexCastNotify.h>
#include <ndBodyPlayerCapsule.h>
#include <ndBodyTriggerVolume.h>
#include <ndBodyKinematicBase.h>
#include <ndBodiesInAabbNotify.h>
#include <ndShapeConvexPolygon.h>
#include <ndBodyKinematicBase.h>
#include <ndShapeChamferCylinder.h>
#include <ndShapeUserDefinedImplicit.h>
#include <ndJointBilateralConstraint.h>
#include <ndShapeStaticProceduralMesh.h>

Expand Down
3 changes: 3 additions & 0 deletions newton-4.00/sdk/dCollision/ndShape.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ndShapeHeightfield;
class ndShapeDebugNotify;
class ndShapeConvexPolygon;
class ndShapeChamferCylinder;
class ndShapeUserDefinedImplicit;
class ndShapeStaticProceduralMesh;

enum ndShapeID
Expand All @@ -57,6 +58,7 @@ enum ndShapeID
m_capsule,
m_cylinder,
m_chamferCylinder,
m_userDefinedImplicit,
m_convexHull, // this must be the last convex shape ID

// special and non convex collisions.
Expand Down Expand Up @@ -237,6 +239,7 @@ class ndShape: public ndContainersFreeListAlloc<ndShape>
virtual ndShapeHeightfield* GetAsShapeHeightfield() { return nullptr; }
virtual ndShapeConvexPolygon* GetAsShapeAsConvexPolygon() { return nullptr; }
virtual ndShapeChamferCylinder* GetAsShapeChamferCylinder() { return nullptr; }
virtual ndShapeUserDefinedImplicit* GetAsShapeUserDefinedImplicit() { return nullptr; }
virtual ndShapeStaticProceduralMesh* GetAsShapeStaticProceduralMesh() { return nullptr; }

D_COLLISION_API virtual ndInt32 GetConvexVertexCount() const;
Expand Down
148 changes: 148 additions & 0 deletions newton-4.00/sdk/dCollision/ndShapeUserDefinedImplicit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/* Copyright (c) <2003-2022> <Julio Jerez, Newton Game Dynamics>
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/

#include "ndCoreStdafx.h"
#include "ndCollisionStdafx.h"
#include "ndContact.h"
#include "ndContactSolver.h"
#include "ndShapeUserDefinedImplicit.h"

ndShapeUserDefinedImplicit::ndShapeUserDefinedImplicit()
:ndShapeConvex(m_userDefinedImplicit)
{
ndAssert(0);
}

ndShapeUserDefinedImplicit::~ndShapeUserDefinedImplicit()
{
}

void ndShapeUserDefinedImplicit::MassProperties()
{
ndAssert(0);
}

void ndShapeUserDefinedImplicit::CalculateAabb(const ndMatrix& matrix, ndVector &p0, ndVector &p1) const
{
ndAssert(0);
//ndVector size(m_radius);
//p0 = (matrix[3] - size) & ndVector::m_triplexMask;
//p1 = (matrix[3] + size) & ndVector::m_triplexMask;
}

ndVector ndShapeUserDefinedImplicit::SupportVertexSpecialProjectPoint(const ndVector&, const ndVector& dir) const
{
ndAssert(0);
return ndVector::m_zero;
// return dir.Scale(m_radius - D_PENETRATION_TOL);
}

ndVector ndShapeUserDefinedImplicit::SupportVertexSpecial(const ndVector&, ndFloat32) const
{
ndAssert(0);
return ndVector::m_zero;
}

ndVector ndShapeUserDefinedImplicit::SupportVertex(const ndVector& dir) const
{
ndAssert(0);
return ndVector::m_zero;
//ndAssert(dir.m_w == ndFloat32(0.0f));
//ndAssert(ndAbs(dir.DotProduct(dir).GetScalar() - ndFloat32(1.0f)) < ndFloat32(1.0e-3f));
//ndAssert(dir.m_w == 0.0f);
//return dir.Scale(m_radius);
}

ndInt32 ndShapeUserDefinedImplicit::CalculatePlaneIntersection(const ndVector& normal, const ndVector& point, ndVector* const contactsOut) const
{
ndAssert(0);
ndAssert(normal.m_w == 0.0f);
ndAssert(normal.DotProduct(normal).GetScalar() > ndFloat32(0.999f));
contactsOut[0] = normal * normal.DotProduct(point);
return 1;
}

ndFloat32 ndShapeUserDefinedImplicit::RayCast(ndRayCastNotify&, const ndVector& localP0, const ndVector& localP1, ndFloat32 maxT, const ndBody* const, ndContactPoint& contactOut) const
{
ndAssert(0);
return 0;
//ndFloat32 t = ndRayCastSphere(localP0, localP1, ndVector::m_zero, m_radius);
//if (t < maxT)
//{
// ndVector contact(localP0 + (localP1 - localP0).Scale(t));
// ndAssert(contact.m_w == ndFloat32(0.0f));
// //contactOut.m_normal = contact.Scale (dgRsqrt (contact.DotProduct(contact).GetScalar()));
// contactOut.m_normal = contact.Normalize();
// //contactOut.m_userId = SetUserDataID();
//}
//return t;
}

ndShapeInfo ndShapeUserDefinedImplicit::GetShapeInfo() const
{
ndAssert(0);
ndShapeInfo info(ndShapeConvex::GetShapeInfo());
//info.m_sphere.m_radius = m_radius;
return info;
}

void ndShapeUserDefinedImplicit::DebugShape(const ndMatrix& matrix, ndShapeDebugNotify& debugCallback) const
{
ndAssert(0);
//ndVector tmpVectex[1024 * 2];
//ndShapeDebugNotify::ndEdgeType edgeType[1024 * 2];
//
//ndVector p0(ndFloat32(1.0f), ndFloat32(0.0f), ndFloat32(0.0f), ndFloat32(0.0f));
//ndVector p1(-ndFloat32(1.0f), ndFloat32(0.0f), ndFloat32(0.0f), ndFloat32(0.0f));
//ndVector p2(ndFloat32(0.0f), ndFloat32(1.0f), ndFloat32(0.0f), ndFloat32(0.0f));
//ndVector p3(ndFloat32(0.0f), -ndFloat32(1.0f), ndFloat32(0.0f), ndFloat32(0.0f));
//ndVector p4(ndFloat32(0.0f), ndFloat32(0.0f), ndFloat32(1.0f), ndFloat32(0.0f));
//ndVector p5(ndFloat32(0.0f), ndFloat32(0.0f), -ndFloat32(1.0f), ndFloat32(0.0f));
//
//ndInt32 index = 3;
//ndInt32 count = 0;
//TesselateTriangle(index, p4, p0, p2, count, tmpVectex);
//TesselateTriangle(index, p4, p2, p1, count, tmpVectex);
//TesselateTriangle(index, p4, p1, p3, count, tmpVectex);
//TesselateTriangle(index, p4, p3, p0, count, tmpVectex);
//TesselateTriangle(index, p5, p2, p0, count, tmpVectex);
//TesselateTriangle(index, p5, p1, p2, count, tmpVectex);
//TesselateTriangle(index, p5, p3, p1, count, tmpVectex);
//TesselateTriangle(index, p5, p0, p3, count, tmpVectex);
//
//for (ndInt32 i = 0; i < count; ++i)
//{
// edgeType[i] = ndShapeDebugNotify::m_shared;
// tmpVectex[i] = matrix.TransformVector(tmpVectex[i].Scale(m_radius)) & ndVector::m_triplexMask;
//}
//
//for (ndInt32 i = 0; i < count; i += 3)
//{
// debugCallback.DrawPolygon(3, &tmpVectex[i], &edgeType[i]);
//}
}

ndUnsigned64 ndShapeUserDefinedImplicit::GetHash(ndUnsigned64 hash) const
{
ndAssert(0);
ndShapeInfo info(GetShapeInfo());
return info.GetHash(hash);
}
57 changes: 57 additions & 0 deletions newton-4.00/sdk/dCollision/ndShapeUserDefinedImplicit.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* Copyright (c) <2003-2022> <Julio Jerez, Newton Game Dynamics>
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/

#ifndef __ND_USER_DEFINED_IMPLICIT_H__
#define __ND_USER_DEFINED_IMPLICIT_H__

#include "ndShapeConvex.h"



D_MSV_NEWTON_ALIGN_32
class ndShapeUserDefinedImplicit: public ndShapeConvex
{
public:
D_CLASS_REFLECTION(ndShapeUserDefinedImplicit,ndShapeConvex)

D_COLLISION_API ndShapeUserDefinedImplicit();
D_COLLISION_API virtual ~ndShapeUserDefinedImplicit();

virtual ndShapeUserDefinedImplicit* GetAsShape() { return this; }

protected:
D_COLLISION_API virtual void MassProperties();

D_COLLISION_API virtual ndShapeInfo GetShapeInfo() const;
D_COLLISION_API virtual ndUnsigned64 GetHash(ndUnsigned64 hash) const;
D_COLLISION_API virtual void CalculateAabb(const ndMatrix& matrix, ndVector& p0, ndVector& p1) const;
D_COLLISION_API virtual void DebugShape(const ndMatrix& matrix, ndShapeDebugNotify& debugCallback) const;
D_COLLISION_API virtual ndVector SupportVertexSpecialProjectPoint(const ndVector& point, const ndVector& dir) const;
D_COLLISION_API virtual ndVector SupportVertex(const ndVector& dir) const;
D_COLLISION_API virtual ndVector SupportVertexSpecial(const ndVector& dir, ndFloat32 skinMargin) const;
D_COLLISION_API virtual ndFloat32 RayCast(ndRayCastNotify& callback, const ndVector& localP0, const ndVector& localP1, ndFloat32 maxT, const ndBody* const body, ndContactPoint& contactOut) const;
D_COLLISION_API virtual ndInt32 CalculatePlaneIntersection(const ndVector& normal, const ndVector& point, ndVector* const contactsOut) const;

} D_GCC_NEWTON_ALIGN_32;


#endif

0 comments on commit 8c43e61

Please sign in to comment.