static void AddCylindrical (DemoEntityManager* const scene, const dVector& origin) { // make a reel static NewtonBody* const box0 = CreateCylinder (scene, origin + dVector (0.0f, 4.0f, 0.0f, 0.0f), 0.25f, 8.0f); NewtonBody* const box1 = CreateWheel (scene, origin + dVector (0.0f, 4.0f, 0.0f, 0.0f), 1.0f, 0.5f); dMatrix matrix; //connect the box0 to the base by a fix joint (a hinge with zero limit) //NewtonBodyGetMatrix(box0, &matrix[0][0]); //CustomHinge* const fixJoint = new CustomHinge(matrix, box0, NULL); //fixJoint->EnableLimits(true); //fixJoint->SetLimis(0.0f, 0.0f); NewtonBodySetMassMatrix(box0, 0.0f, 0.0f, 0.0f, 0.0f); // connect the bodies by a CorkScrew joint NewtonBodyGetMatrix (box1, &matrix[0][0]); CustomCorkScrew* const cylinder = new CustomCorkScrew (matrix, box1, box0); // enable limit of first axis cylinder->EnableLinearLimits(true); // set limit on second axis cylinder->SetLinearLimis (-4.0f, 4.0f); #ifdef _USE_HARD_JOINTS NewtonSkeletonContainer* const skeleton = NewtonSkeletonContainerCreate(scene->GetNewton(), box0, NULL); NewtonSkeletonContainerAttachBone(skeleton, box1, box0); NewtonSkeletonContainerFinalize(skeleton); #endif }
static void AddSlidingContact(DemoEntityManager* const scene, const dVector& origin) { // make a reel static NewtonBody* const box0 = CreateBox(scene, origin + dVector(0.0f, 4.0f, 0.0f, 0.0f), dVector(8.0f, 0.25f, 0.25f, 0.0f)); NewtonBody* const box1 = CreateWheel(scene, origin + dVector(0.0f, 4.0f, 0.0f, 0.0f), 1.0f, 0.5f); dMatrix matrix; //connect the box0 to the base by a fix joint (a hinge with zero limit) NewtonBodyGetMatrix(box0, &matrix[0][0]); CustomHinge* const hinge = new CustomHinge(matrix, box0, NULL); hinge->EnableLimits(true); hinge->SetLimis(0.0f, 0.0f); // connect the bodies by a Slider joint NewtonBodyGetMatrix(box1, &matrix[0][0]); CustomSlidingContact* const slider = new CustomSlidingContact(matrix, box1, box0); slider->EnableLinearLimits (true); slider->SetLinearLimis (-4.0f, 4.0f); // enable limit of first axis slider->EnableAngularLimits(true); slider->SetAngularLimis (-60.0f * 3.1416f / 180.0f, 60.0f * 3.1416f / 180.0f); #ifdef _USE_HARD_JOINTS NewtonSkeletonContainer* const skeleton = NewtonSkeletonContainerCreate(scene->GetNewton(), NULL, NULL); NewtonSkeletonContainerAttachBone(skeleton, box0, NULL); NewtonSkeletonContainerAttachBone(skeleton, box1, box0); NewtonSkeletonContainerFinalize(skeleton); #endif }
static void AddSlider (DemoEntityManager* const scene, const dVector& origin) { // make a reel static NewtonBody* const box0 = CreateBox (scene, origin + dVector (0.0f, 4.0f, 0.0f, 0.0f), dVector (8.0f, 0.25f, 0.25f, 0.0f)); NewtonBody* const box1 = CreateWheel (scene, origin + dVector (0.0f, 4.0f, 0.0f, 0.0f), 1.0f, 0.5f); dMatrix matrix; NewtonBodySetMassMatrix(box0, 0.0f, 0.0f, 0.0f, 0.0f); // connect the bodies by a Slider joint NewtonBodyGetMatrix (box1, &matrix[0][0]); CustomSlider* const slider = new CustomSlider (matrix, box1, box0); // enable limit of first axis slider->EnableLimits(true); // set limit on second axis slider->SetLimits (-4.0f, 4.0f); #ifdef _USE_HARD_JOINTS NewtonSkeletonContainer* const skeleton = NewtonSkeletonContainerCreate(scene->GetNewton(), box0, NULL); NewtonSkeletonContainerAttachBone(skeleton, box1, box0); NewtonSkeletonContainerFinalize(skeleton); #endif }
static void AddUniversal(DemoEntityManager* const scene, const dVector& origin) { dVector size(1.0f, 1.0f, 1.0f); NewtonBody* const box0 = CreateBox(scene, origin + dVector(0.0f, 4.0f, 0.0f, 0.0f), dVector(0.25f, 0.25f, 4.0f, 0.0f)); NewtonBody* const box1 = CreateWheel(scene, origin + dVector(0.0f, 4.0f, 2.0f, 0.0f), 1.0f, 0.5f); NewtonBody* const box2 = CreateWheel(scene, origin + dVector(0.0f, 4.0f, -2.0f, 0.0f), 1.0f, 0.5f); NewtonBodySetMassMatrix(box0, 0.0f, 0.0f, 0.0f, 0.0f); // align the object so that is looks nice dMatrix matrix; NewtonBodyGetMatrix(box1, &matrix[0][0]); matrix = dYawMatrix (3.1416f * 0.5f) * matrix; NewtonBodySetMatrix(box1, &matrix[0][0]); ((DemoEntity*) NewtonBodyGetUserData(box1))->ResetMatrix (*scene, matrix); NewtonBodyGetMatrix(box2, &matrix[0][0]); matrix = dYawMatrix(3.1416f * 0.5f) * matrix; NewtonBodySetMatrix(box2, &matrix[0][0]); ((DemoEntity*) NewtonBodyGetUserData(box2))->ResetMatrix (*scene, matrix); // link the two boxes NewtonBodyGetMatrix(box1, &matrix[0][0]); CustomUniversal* const joint1 = new CustomUniversal(matrix, box1, box0); joint1->EnableLimit_0(true); joint1->SetLimis_0 (-5.0f * 3.141592f, 2.0f * 3.141592f); joint1->EnableLimit_1(true); joint1->SetLimis_1 (-3.0f * 3.141592f, 4.0f * 3.141592f); // link the two boxes NewtonBodyGetMatrix(box2, &matrix[0][0]); CustomUniversal* const joint2 = new CustomUniversal(matrix, box2, box0); joint2->EnableLimit_0(true); joint2->SetLimis_0 (-3.0f * 3.141592f, 5.0f * 3.141592f); joint2->EnableLimit_1(true); joint2->SetLimis_1(-4.0f * 3.141592f, 2.0f * 3.141592f); #ifdef _USE_HARD_JOINTS NewtonSkeletonContainer* const skeleton = NewtonSkeletonContainerCreate(scene->GetNewton(), box0, NULL); NewtonSkeletonContainerAttachBone(skeleton, box1, box0); NewtonSkeletonContainerAttachBone(skeleton, box2, box0); NewtonSkeletonContainerFinalize(skeleton); #endif }
static CustomCorkScrew* AddCylindricalWheel (DemoEntityManager* const scene, const dVector& origin, dFloat radius, dFloat height, NewtonBody* const parent) { NewtonBody* const wheel = CreateWheel (scene, origin, height, radius); // the joint pin is the first row of the matrix dMatrix matrix; NewtonBodyGetMatrix (wheel, &matrix[0][0]); return new CustomCorkScrew (matrix, wheel, parent); }
static CustomSlider* AddSliderWheel (DemoEntityManager* const scene, const dVector& origin, dFloat radius, dFloat height, NewtonBody* const parent) { NewtonBody* const wheel = CreateWheel (scene, origin, height, radius); // the joint pin is the first row of the matrix //dMatrix localPin (dRollMatrix(90.0f * 3.141592f / 180.0f)); dMatrix localPin (dGetIdentityMatrix()); dMatrix matrix; NewtonBodyGetMatrix (wheel, & matrix[0][0]); matrix = localPin * matrix; // connect first box to the world return new CustomSlider (matrix, wheel, parent); }
static void AddPathFollow (DemoEntityManager* const scene, const dVector& origin) { // create a Bezier Spline path for AI car to drive DemoEntity* const rollerCosterPath = new DemoEntity(dGetIdentityMatrix(), NULL); scene->Append(rollerCosterPath); dBezierSpline spline; dFloat64 knots[] = {0.0f, 1.0f / 3.0f, 2.0f / 3.0f, 1.0f}; dBigVector o (origin[0], origin[1], origin[2], 0.0f); dBigVector control[] = { dBigVector(100.0f - 100.0f, 20.0f, 200.0f - 250.0f, 1.0f) + o, dBigVector(150.0f - 100.0f, 10.0f, 150.0f - 250.0f, 1.0f) + o, dBigVector(200.0f - 100.0f, 70.0f, 250.0f - 250.0f, 1.0f) + o, dBigVector(150.0f - 100.0f, 10.0f, 350.0f - 250.0f, 1.0f) + o, dBigVector( 50.0f - 100.0f, 30.0f, 250.0f - 250.0f, 1.0f) + o, dBigVector(100.0f - 100.0f, 20.0f, 200.0f - 250.0f, 1.0f) + o, }; spline.CreateFromKnotVectorAndControlPoints(3, sizeof (knots) / sizeof (knots[0]), knots, control); DemoBezierCurve* const mesh = new DemoBezierCurve (spline); rollerCosterPath->SetMesh(mesh, dGetIdentityMatrix()); mesh->SetVisible(true); mesh->SetRenderResolution(500); mesh->Release(); NewtonBody* const box0 = CreateWheel(scene, origin + dVector(100.0f - 100.0f, 20.0f, 200.0f - 250.0f, 0.0f), 1.0f, 0.5f); dMatrix matrix; NewtonBodyGetMatrix (box0, &matrix[0][0]); matrix.m_posit.m_y += 0.5f; new MyPathFollow (matrix, box0, rollerCosterPath); }
static void AddPathFollow (DemoEntityManager* const scene, const dVector& origin) { // create a Bezier Spline path for AI car to drive DemoEntity* const rollerCosterPath = new DemoEntity(dGetIdentityMatrix(), NULL); scene->Append(rollerCosterPath); dBezierSpline spline; dFloat64 knots[] = {0.0f, 1.0f / 5.0f, 2.0f / 5.0f, 3.0f / 5.0f, 4.0f / 5.0f, 1.0f}; dBigVector o (origin[0], origin[1], origin[2], 0.0f); dBigVector control[] = { dBigVector(100.0f - 100.0f, 20.0f, 200.0f - 250.0f, 1.0f) + o, dBigVector(150.0f - 100.0f, 10.0f, 150.0f - 250.0f, 1.0f) + o, dBigVector(175.0f - 100.0f, 30.0f, 250.0f - 250.0f, 1.0f) + o, dBigVector(200.0f - 100.0f, 70.0f, 250.0f - 250.0f, 1.0f) + o, dBigVector(215.0f - 100.0f, 20.0f, 250.0f - 250.0f, 1.0f) + o, dBigVector(150.0f - 100.0f, 50.0f, 350.0f - 250.0f, 1.0f) + o, dBigVector( 50.0f - 100.0f, 30.0f, 250.0f - 250.0f, 1.0f) + o, dBigVector(100.0f - 100.0f, 20.0f, 200.0f - 250.0f, 1.0f) + o, }; spline.CreateFromKnotVectorAndControlPoints(3, sizeof (knots) / sizeof (knots[0]), knots, control); DemoBezierCurve* const mesh = new DemoBezierCurve (spline); rollerCosterPath->SetMesh(mesh, dGetIdentityMatrix()); mesh->SetVisible(true); mesh->SetRenderResolution(500); mesh->Release(); const int count = 32; NewtonBody* bodies[count]; dBigVector point0; dVector positions[count + 1]; dFloat64 knot = spline.FindClosestKnot(point0, origin + dVector(100.0f - 100.0f, 20.0f, 200.0f - 250.0f, 0.0f), 4); positions[0] = dVector (point0.m_x, point0.m_y, point0.m_z, 0.0); dFloat average = 0.0f; for (int i = 0; i < count; i ++) { dBigVector point1; average += positions[i].m_y; dBigVector tangent(spline.CurveDerivative(knot)); tangent = tangent.Scale (1.0 / sqrt (tangent % tangent)); knot = spline.FindClosestKnot(point1, dBigVector (point0 + tangent.Scale (2.0f)), 4); point0 = point1; positions[i + 1] = dVector (point0.m_x, point0.m_y, point0.m_z, 0.0); } average /= count; for (int i = 0; i < count + 1; i ++) { positions[i].m_y = average; } dFloat attachmentOffset = 0.8f; for (int i = 0; i < count; i ++) { dMatrix matrix; dVector location0 (positions[i].m_x, positions[i].m_y, positions[i].m_z, 0.0); bodies[i] = CreateWheel(scene, location0, 1.0f, 0.5f); NewtonBodySetLinearDamping(bodies[i], 0.0f); NewtonBody* const box = bodies[i]; NewtonBodyGetMatrix(box, &matrix[0][0]); dVector location1 (positions[i + 1].m_x, positions[i + 1].m_y, positions[i + 1].m_z, 0.0); dVector dir (location1 - location0); matrix.m_front = dir.Scale (1.0f / dSqrt (dir % dir)); matrix.m_right = matrix.m_front * matrix.m_up; dMatrix matrix1 (dYawMatrix(0.5f * 3.141692f) * matrix); NewtonBodySetMatrix(box, &matrix1[0][0]); matrix.m_posit.m_y += attachmentOffset; new MyPathFollow(matrix, box, rollerCosterPath); dVector veloc (dir.Scale (20.0f)); NewtonBodySetVelocity(box, &veloc[0]); } for (int i = 1; i < count; i ++) { NewtonBody* const box0 = bodies[i - 1]; NewtonBody* const box1 = bodies[i]; dMatrix matrix0; dMatrix matrix1; NewtonBodyGetMatrix(box0, &matrix0[0][0]); NewtonBodyGetMatrix(box1, &matrix1[0][0]); matrix0.m_posit.m_y += attachmentOffset; matrix1.m_posit.m_y += attachmentOffset; new CustomDistanceRope (matrix1.m_posit, matrix0.m_posit, box1, box0); } void* const aggregate = NewtonCollisionAggregateCreate (scene->GetNewton()); for (int i = 0; i < count; i ++) { NewtonCollisionAggregateAddBody(aggregate, bodies[i]); } NewtonCollisionAggregateSetSelfCollision (aggregate, false); #ifdef _USE_HARD_JOINTS NewtonSkeletonContainer* const skeleton = NewtonSkeletonContainerCreate(scene->GetNewton(), bodies[0], NULL); for (int i = 1; i < count; i++) { NewtonSkeletonContainerAttachBone(skeleton, bodies[i], bodies[i - 1]); } NewtonSkeletonContainerFinalize(skeleton); #endif }