Hello,
I'm trying to make a really simple collision in C# between two objects, a box and a bigger and flatter box, which represents ground.
However, no matter how hard I try, I just can't make them collide with each other. The box just goes throught the 'ground'.
I've looked at some of the C++ tutorials that come with NewtonSDK, and tried to convert it to C#, but I can't figure out what's wrong with my code.
Now I'm kinda running out of ideas so I'm hoping you could help me out.
public static void GameLoop()
{
// First we will instruct our camera to have a specific background colour
DarkGDK.Camera.DefaultCamera.ColorBackdrop(System.Drawing.Color.BlueViolet);
//Create the Newton world
nWorld = NewtonWrapper.Newton.Create(0, 0);
//Create a small box
DarkGDK.Basic3D.Box box = new DarkGDK.Basic3D.Box(5f, 5f, 5f);
float[] boxOffset = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 15.0f, 0.0f, 0.0f };
int boxCollision = NewtonWrapper.Newton.CreateBox(nWorld, 5f, 5f, 5f, boxOffset);
int boxBody = NewtonWrapper.Newton.CreateBody(nWorld, boxCollision);
NewtonWrapper.Newton.BodySetMassMatrix(boxBody, 1, 1f, 1f, 1f);
//Create the static ground.
DarkGDK.Basic3D.Box land = new DarkGDK.Basic3D.Box(70f, 2f, 70f, System.Drawing.Color.Blue);
float[] landOffset = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -15.0f, 0.0f, 0.0f };
int landCollision = NewtonWrapper.Newton.CreateBox(nWorld, 70f, 2f, 70f, landOffset);
int landBody = NewtonWrapper.Newton.CreateBody(nWorld, landCollision);
// Instruct DarkGDK.NET to stop automatically positioning and
// angling the camera every time a new object is created.
DarkGDK.Camera.DefaultCamera.AutoCamOff();
// Place the camera just infront of where our cube will first appear
DarkGDK.Camera.DefaultCamera.PositionCurrent(-20, 15, -70);
// Tell the camera to point towards the centre of our virtual universe
DarkGDK.Camera.DefaultCamera.PointCurrent(0, 0, 0);
//Setup the materials.
int MatID = NewtonWrapper.Newton.MaterialGetDefaultGroupID(nWorld);
NewtonWrapper.Newton.BodySetMaterialGroupID(boxBody, MatID);
NewtonWrapper.Newton.BodySetMaterialGroupID(landBody, MatID);
NewtonWrapper.Newton.MaterialSetDefaultElasticity(nWorld, MatID, MatID, 0.4f);
NewtonWrapper.Newton.MaterialSetDefaultFriction(nWorld, MatID, MatID, 1.0f, 0.5f);
NewtonWrapper.Newton.MaterialSetDefaultSoftness(nWorld, MatID, MatID, 0.05f);
NewtonWrapper.Newton.MaterialSetDefaultCollidable(nWorld, MatID, MatID, true);
NewtonWrapper.Newton.MaterialSetCollisionCallback(nWorld, MatID, MatID, landBody
, null, null, null);
NewtonWrapper.Newton.MaterialSetCollisionCallback(nWorld, MatID, MatID, boxBody
, null, null, null);
//End of materials
NewtonWrapper.Newton.BodySetForceAndTorqueCallback(boxBody, AddGravity);
NewtonWrapper.Newton.BodySetUserData(boxBody, box);
NewtonWrapper.Newton.BodySetUserData(landBody, land);
//Release collisions to prevent memory leaks.
NewtonWrapper.Newton.ReleaseCollision(nWorld, landCollision);
NewtonWrapper.Newton.ReleaseCollision(nWorld, boxCollision);
NewtonWrapper.Newton.BodySetTransformCallback(boxBody, TransformCallback);
//
// Loop continuously until StopGDKLoop is called, or the ESC key is pressed.
//
while (DarkGDK.Engine.LoopGDK)
{
// Rotate our cube along it's Y axis
DarkGDK.Camera.DefaultCamera.ControlDefaultUsingArrowKeys(1, 1);
NewtonWrapper.Newton.BodySetMatrix(landBody, landOffset);
float[] boxPosition = NewtonWrapper.Newton.BodyGetMatrix(boxBody);
float[] landPosition = NewtonWrapper.Newton.BodyGetMatrix(landBody);
land.Position(landPosition[12], landPosition[13], landPosition[14]);
for (int s = 0; s < landPosition.Length; s++)
{
DarkGDK.Text.ShowText(0, s * 10, s + " - " + landPosition[s]);
DarkGDK.Text.ShowText(200, s * 10, s + " - " + boxPosition[s]);
}
NewtonWrapper.Newton.Update(nWorld, 0.01f);
// Tell DarkGDK.NET to render our default camera display
DarkGDK.Core.Sync();
}
}
I'm quite certain im missing something really small yet really required.