Moderators: Sascha Willems, Thomas
dgVector relStep (relativeVelocity.Scale(dgMax (proxy.m_timestep, dgFloat32 (1.0e-12f))));
#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <math.h>
#define _NEWTON_STATIC_LIB
#include "Newton\include\dgNewton\Newton.h"
#pragma comment(lib,"Newton\\lib\\dgCore.lib")
#pragma comment(lib,"Newton\\lib\\dgPhysics.lib")
#pragma comment(lib,"Newton\\lib\\newton.lib")
char* pFileData=NULL;
int iFilePosition=0;
void DeserialiseCollision(void* SerializeHandle,void* Buffer,int Size)
{
memcpy(Buffer,&pFileData[iFilePosition],Size);
iFilePosition+=Size;
}
static void ApplyGravity(const NewtonBody* const Body,dFloat timestep,int threadIndex)
{
float Gravity[]={0.66435254,-17.463728,8.8733788};
float fMass;float fIXX;float fIYY;float fIZZ;
NewtonBodyGetMass(Body,&fMass,&fIXX,&fIYY,&fIZZ);
Gravity[0]*=fMass;Gravity[1]*=fMass;Gravity[2]*=fMass;
NewtonBodyAddForce(Body,Gravity);
}
void CollisionCallback(const NewtonJoint* ContactJoint,dFloat Timestep,int ThreadIndex)
{
if(ContactJoint==NULL){return;}
NewtonBody* pBody1=NewtonJointGetBody0(ContactJoint);
NewtonBody* pBody2=NewtonJointGetBody1(ContactJoint);
void* pContact=NewtonContactJointGetFirstContact(ContactJoint);
while(pContact!=NULL)
{
NewtonMaterial* pMaterial=NewtonContactGetMaterial(pContact);
int iFaceAttribute=NewtonMaterialGetContactFaceAttribute(pMaterial);//doesn't work? always returns 0?
float fNormalSpeed=NewtonMaterialGetContactNormalSpeed(pMaterial);
if(fNormalSpeed>5.0f)
{
//make a noise
}
pContact=NewtonContactJointGetNextContact(ContactJoint,pContact);
}
}
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrev,LPSTR lpCmdLine,int nShowCmd)
{
float ZeroVector3[]={0.0f,0.0f,0.0f};
float IdentityMatrix[]={1.0000000,0.00000000,0.00000000,0.00000000,0.00000000,1.0000000,0.00000000,0.00000000,0.00000000,0.00000000,1.0000000,0.00000000,0.00000000,0.00000000,0.00000000,1.0000000};
//////////////////////////////////////////////////////////
//Newton object
//////////////////////////////////////////////////////////
NewtonWorld* pNewtonWorld=NewtonCreate();
//NewtonWorldSetUserData(pNewtonWorld,some pointer to something I use);
NewtonSetSolverIterations(pNewtonWorld,8);
NewtonSetNumberOfSubsteps(pNewtonWorld,5);
NewtonSetThreadsCount(pNewtonWorld,1);
//////////////////////////////////////////////////////////
//Materials
//////////////////////////////////////////////////////////
int iBallMaterialID=NewtonMaterialCreateGroupID(pNewtonWorld);
NewtonMaterialSetDefaultElasticity(pNewtonWorld,iBallMaterialID,iBallMaterialID,0.25f);
NewtonMaterialSetDefaultFriction(pNewtonWorld,iBallMaterialID,iBallMaterialID,2.1f,0.9f);
NewtonMaterialSetDefaultSoftness(pNewtonWorld,iBallMaterialID,iBallMaterialID,0.25f);
int iWorldMaterialID=NewtonMaterialCreateGroupID(pNewtonWorld);
NewtonMaterialSetDefaultElasticity(pNewtonWorld,iWorldMaterialID,iWorldMaterialID,0.25f);
NewtonMaterialSetDefaultFriction(pNewtonWorld,iWorldMaterialID,iWorldMaterialID,2.1f,0.9f);
NewtonMaterialSetDefaultSoftness(pNewtonWorld,iWorldMaterialID,iWorldMaterialID,0.25f);
NewtonMaterialSetCollisionCallback(pNewtonWorld,iBallMaterialID,iWorldMaterialID,NULL,CollisionCallback);
//////////////////////////////////////////////////////////
//create the ball
//////////////////////////////////////////////////////////
NewtonBody* pBall=NULL;
{
NewtonCollision* pCollision=NewtonCreateSphere(pNewtonWorld,0.5f,0,NULL);
pBall=NewtonCreateDynamicBody(pNewtonWorld,pCollision,IdentityMatrix);
NewtonBodySetForceAndTorqueCallback(pBall,ApplyGravity);
NewtonBodySetMassProperties(pBall,1.0f,pCollision);
NewtonBodySetLinearDamping(pBall,0.01f);
float Damping[]={0.02f,0.02f,0.02f};
NewtonBodySetAngularDamping(pBall,Damping);//this lets the ball spin like crazy though
NewtonDestroyCollision(pCollision);
NewtonBodySetMaterialGroupID(pBall,iBallMaterialID);
NewtonBodySetContinuousCollisionMode(pBall,1);
//NewtonBodySetUserData(pBall,some pointer to something I use);
}
//////////////////////////////////////////////////////////
//create the level - this is normally built as per commented lines, but I'm using serialise here
//////////////////////////////////////////////////////////
NewtonBody* pLevel=NULL;
{
HANDLE hFile=CreateFileA("Temp.dat",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
int iFileSize=GetFileSize(hFile,NULL);
pFileData=new char[iFileSize+1];
DWORD nRead=0;
ReadFile(hFile,pFileData,iFileSize,&nRead,NULL);
NewtonCollision* pCollision=NewtonCreateCollisionFromSerialization(pNewtonWorld,DeserialiseCollision,NULL);
/*pCollision=NewtonCreateTreeCollision(World,0);
NewtonTreeCollisionBeginBuild(pCollision);
for(int I=0;I<IndexCount;I+=3)
{
float Triangles[9]={0};
..gather triangle data per face
NewtonTreeCollisionAddFace(pCollision,3,Triangles,3*sizeof(float),1234); <- out of interest - why does 1234 never show up in collision callback, it's always 0?
}
NewtonTreeCollisionEndBuild(pCollision,0);*/
pLevel=NewtonCreateDynamicBody(pNewtonWorld,pCollision,IdentityMatrix);
NewtonDestroyCollision(pCollision);
NewtonBodySetMatrix(pLevel,IdentityMatrix);
NewtonBodySetMaterialGroupID(pLevel,iWorldMaterialID);
}
//////////////////////////////////////////////////////////
//game start logic.. player put into position, world reset etc
//////////////////////////////////////////////////////////
float StartPosition[16]={1.0000000,0.00000000,0.00000000,0.00000000,
0.00000000,1.0000000,0.00000000,0.00000000,
0.00000000,0.00000000,1.0000000,0.00000000,
1.4000000,4.9000001,7.6999998,1.0000000};
NewtonBodySetVelocity(pBall,ZeroVector3);
NewtonBodySetOmega(pBall,ZeroVector3);
NewtonBodySetMatrix(pBall,StartPosition);//(1.4f,4.9f,7.7f)
NewtonInvalidateCache(pNewtonWorld);
while(true)
{
NewtonUpdate(pNewtonWorld,1.0f/60.0f);
Sleep(10);
if(GetAsyncKeyState(0x1B)){break;}
}
if(pNewtonWorld!=NULL){NewtonDestroyAllBodies(pNewtonWorld);NewtonDestroy(pNewtonWorld);}
return(0);
}
I would like to know why the faceID I pass to NewtonTreeCollisionAddFace is never used/seen again? It's always 0 in the collision callback which means I can't ever use it for anything.
unsigned NewtonMaterialGetContactFaceAttribute(const NewtonMaterial* const materialHandle)
{
TRACE_FUNCTION(__FUNCTION__);
// dgContactMaterial* const material = (dgContactMaterial*) materialHandle;
dgAssert (0);
// return (unsigned) ((dgContactMaterial*) materialHandle->m_userId);
return 0;
}
The upload has failed
Please try again later, Gofile could be in maintenance.
Users browsing this forum: No registered users and 2 guests