void demography :: create_walls() { //body definition b2BodyDef myBodyDef; myBodyDef.type = b2_staticBody; //shape definition b2PolygonShape polygonShape; polygonShape.SetAsBox(1, 1); //a 2x2 rectangle //fixture definition b2FixtureDef myFixtureDef; myFixtureDef.shape = &polygonShape; myFixtureDef.density = 1; myFixtureDef.restitution = 1; //a static body myBodyDef.type = b2_staticBody; myBodyDef.position.Set(0, 0); b2Body* staticBody = World.CreateBody(&myBodyDef); // CircleShape * p = 0; staticBody->SetUserData(NULL); vector<b2Vec2> pos; vector<b2Vec2> size; // int coeff_x= 38; // int coeff_y= 29; // int coeff_x= 38; // int coeff_y= 29; b2Vec2 dims(wsize.x/SCALE,wsize.y/SCALE); int coeff_x = dims.x*.5, coeff_y = dims.y*.5; //top,bottom,left,right pos.push_back(b2Vec2(coeff_x,1)); pos.push_back(b2Vec2(coeff_x,coeff_y*2)); pos.push_back(b2Vec2(1,coeff_y)); pos.push_back(b2Vec2(coeff_x*2,coeff_y)); size.push_back(b2Vec2(coeff_x,1)); size.push_back(b2Vec2(coeff_x,1)); size.push_back(b2Vec2(1,coeff_y)); size.push_back(b2Vec2(1,coeff_y)); for(size_t a = 0; a < 4; ++a) { float w = size[a].x, h = size[a].y; Vector2f posit((pos[a].x)*SCALE,(pos[a].y)*SCALE); polygonShape.SetAsBox( w, h, pos[a], 0);//ground staticBody->CreateFixture(&myFixtureDef); walls.push_back(RectangleShape()); walls.back().setSize(Vector2f(w*2*SCALE,2*h*SCALE)); walls.back().setPosition(posit); walls.back().setFillColor(Color(50,50,50)); recenter(walls.back()); // staticBody->SetUserData(0); staticBody->SetUserData(NULL); } }
void CalculatePickForceAndTorque (const NewtonBody* const body, const dVector& pointOnBodyInGlobalSpace, const dVector& targetPositionInGlobalSpace, dFloat timestep) { dFloat mass; dFloat Ixx; dFloat Iyy; dFloat Izz; const dFloat stiffness = 0.33f; const dFloat damping = -0.05f; NewtonBodyGetMass(body, &mass, &Ixx, &Iyy, &Izz); // calculate the desired impulse dVector posit(targetPositionInGlobalSpace - pointOnBodyInGlobalSpace); dVector impulse(posit.Scale(stiffness * mass)); // apply linear impulse NewtonBodyApplyImpulseArray(body, 1, sizeof (dVector), &impulse[0], &pointOnBodyInGlobalSpace[0], timestep); // apply linear and angular damping dMatrix inertia; dVector linearMomentum(0.0f); dVector angularMomentum(0.0f); NewtonBodyGetOmega(body, &angularMomentum[0]); NewtonBodyGetVelocity(body, &linearMomentum[0]); NewtonBodyGetInertiaMatrix(body, &inertia[0][0]); angularMomentum = inertia.RotateVector(angularMomentum); angularMomentum = angularMomentum.Scale(damping); linearMomentum = linearMomentum.Scale(mass * damping); NewtonBodyApplyImpulsePair(body, &linearMomentum[0], &angularMomentum[0], timestep); }
void OptimizedMeshLevelCollision (NewtonFrame& system) { NewtonWorld* world; world = system.m_world; // create the sky box and the floor, //LoadLevelAndSceneRoot (system, "flatplane.dae", 1); //LoadLevelAndSceneRoot (system, "dungeon.dae", 1); //LoadLevelAndSceneRoot (system, "pitpool.dae", 1); LoadLevelAndSceneRoot (system, "pitpool.dae", 1); //LoadLevelAndSceneRoot (system, "dungeon.dae", 1); // create a material to collide with this object int defaultMaterialID; defaultMaterialID = NewtonMaterialGetDefaultGroupID (world); dVector posit (0.0f, 0.0f, 0.0f, 0.0f); posit.m_y = FindFloor (world, posit.m_x, posit.m_z) + 5.0f; InitEyePoint (dVector (1.0f, 0.0f, 0.0f), posit); dVector size (1.0f, 1.0f, 1.0f); dVector location (cameraEyepoint + cameraDir.Scale (10.0f)); AddBoxes(&system, 10.0f, location, size, 3, 3, 10.0f, _SPHERE_PRIMITIVE, defaultMaterialID); AddBoxes(&system, 10.0f, location, size, 3, 3, 10.0f, _BOX_PRIMITIVE, defaultMaterialID); AddBoxes(&system, 10.0f, location, size, 3, 3, 10.0f, _CONE_PRIMITIVE, defaultMaterialID); AddBoxes(&system, 10.0f, location, size, 3, 3, 10.0f, _CYLINDER_PRIMITIVE, defaultMaterialID); AddBoxes(&system, 10.0f, location, size, 3, 3, 10.0f, _CAPSULE_PRIMITIVE, defaultMaterialID); AddBoxes(&system, 10.0f, location, size, 3, 3, 10.0f, _CHAMFER_CYLINDER_PRIMITIVE, defaultMaterialID); AddBoxes(&system, 10.0f, location, size, 3, 3, 10.0f, _RANDOM_CONVEX_HULL_PRIMITIVE, defaultMaterialID); AddBoxes(&system, 10.0f, location, size, 3, 3, 10.0f, _REGULAR_CONVEX_HULL_PRIMITIVE, defaultMaterialID); }
void Motif::freq_matrix_extended(double *fm) const{ const vector<vector <int> >& seq = seqset.seq(); int i, col, j; int fm_size = (width + 2 * ncols()) * 4; for(i = 0; i < fm_size; i++) fm[i] = 0.0; if(number() == 0) return; for(i = 0; i < number(); i++) {//i = site number int c = chrom(i); int p = posit(i); bool s = strand(i); for(j = 0, col = -ncols(); col < width + ncols(); col++, j += 4) { if(s) { if((p + col <= seqset.len_seq(c) - 1) && (p + col >= 0)) { assert(j + seq[c][p + col] < fm_size); fm[j + seq[c][p + col]] += 1.0; } else { for(int k = 0; k < 4; k++) { fm[j + k] += 0.25; } } } else { if((p + width - 1 - col <= seqset.len_seq(c) - 1) && (p + width - 1 - col >= 0)) { assert(j + 3 - seq[c][p + width - 1 - col] < fm_size); fm[j + 3 - seq[c][p + width - 1 - col]] += 1.0; } else { for(int k = 0; k < 4; k++) { fm[j + k] += 0.25; } } } } } for(i = 0; i < fm_size; i++) fm[i] /= (double) number(); }
void spin(int x) { switch(x) { case 1: posit(8,10); printf("| \n"); break; case 2: posit(8,10); printf("/ \n"); break; case 3: posit(8,10); printf("\\ \n"); break; default: posit(8,10); printf("- \n"); break; } }
static void Magnets (NewtonFrame& system, dFloat strength) { NewtonWorld* world; LevelPrimitive* scene; world = system.m_world; // create the sky box and the floor, scene = LoadLevelAndSceneRoot (system, "playground.dae", 1); dVector posit (0.0f, 0.0f, 0.0f, 1.0f); posit.m_y = FindFloor (world, posit.m_x, posit.m_z) + 5.0f; InitEyePoint (dVector (1.0f, 0.0f, 0.0f), posit); // create a material carrier to colliding with this objects int defaultMaterialID; int magneticFieldID; int magneticPicesID; defaultMaterialID = NewtonMaterialGetDefaultGroupID (world); magneticFieldID = NewtonMaterialCreateGroupID (world); magneticPicesID = NewtonMaterialCreateGroupID (world); NewtonMaterialSetCollisionCallback (world, magneticPicesID, magneticFieldID, NULL, NULL, Magnet::MagneticField); // create a spherical object to serve are the magnet core dMatrix matrix (GetIdentityMatrix()); matrix.m_posit = posit; matrix.m_posit.m_x += 7.0f; new Magnet (system, matrix, 20.0f, defaultMaterialID, magneticFieldID, strength); // add a material to control the buoyancy dVector size (1.0f, 0.25f, 0.5f); dVector sphSize (1.0f, 1.0f, 1.0f); dVector capSize (1.25f, 0.4f, 1.0f); dVector location (cameraEyepoint + cameraDir.Scale (10.0f)); AddBoxes (&system, 1.0f, location, sphSize, 3, 3, 5.0f, _SPHERE_PRIMITIVE, magneticPicesID); AddBoxes (&system, 1.0f, location, size, 3, 3, 5.0f, _BOX_PRIMITIVE, magneticPicesID); AddBoxes (&system, 1.0f, location, size, 3, 3, 5.0f, _CONE_PRIMITIVE, magneticPicesID); AddBoxes (&system, 1.0f, location, size, 3, 3, 5.0f, _CYLINDER_PRIMITIVE, magneticPicesID); AddBoxes (&system, 1.0f, location, capSize, 3, 3, 5.0f, _CAPSULE_PRIMITIVE, magneticPicesID); AddBoxes (&system, 1.0f, location, size, 3, 3, 5.0f, _CHAMFER_CYLINDER_PRIMITIVE, magneticPicesID); AddBoxes (&system, 1.0f, location, size, 3, 3, 5.0f, _RANDOM_CONVEX_HULL_PRIMITIVE, magneticPicesID); AddBoxes (&system, 1.0f, location, size, 3, 3, 5.0f, _REGULAR_CONVEX_HULL_PRIMITIVE, magneticPicesID); posit.m_x -= 10.0f; InitEyePoint (dVector (1.0f, 0.0f, 0.0f), posit); }
void WindowingSystem::update(Time dt) { auto topLevelWidget = getTopLevelWidget(); topLevelWidget->updateInternal(); // if it's clean, mark it dirty drawContext.vbo->changeState(VBOs::Clean, VBOs::Dirty); // if it's dirty (explicitly, or because we just moved it from clean), start updating if (drawContext.vbo->changeState(VBOs::Dirty, VBOs::Updating)) { drawContext.vbo->clear(); drawContext.revision++; topLevelWidget->draw(drawContext); posit(drawContext.vbo->changeState(VBOs::Updating, VBOs::Updated), "Could not move windowing vbo to updated"); } }
void DemoEntity::InterpolateMatrix (DemoEntityManager& world, dFloat param) { // read the data in a critical section to prevent race condition from oteh thread world.Lock(m_lock); dVector p0(m_curPosition); dVector p1(m_nextPosition); dQuaternion r0 (m_curRotation); dQuaternion r1 (m_nextRotation); // release the critical section world.Unlock(m_lock); dVector posit (p0 + (p1 - p0).Scale (param)); dQuaternion rotation (r0.Slerp(r1, param)); m_matrix = dMatrix (rotation, posit); }
static void MagneticFieldNoForce (const NewtonBody* body, dFloat timestep, int threadIndex) { dMatrix matrix; dVector zero (0.0f, 0.0f, 0.0f, 0.0f); NewtonBodySetForce (body, &zero[0]); NewtonBodySetTorque (body, &zero[0]); NewtonBodySetOmega (body, &zero[0]); NewtonBodySetVelocity (body, &zero[0]); // telepor the magnetic field trigger to the center of the core Magnet* magnet; magnet = (Magnet*)NewtonBodyGetUserData(body); NewtonBodyGetMatrix (magnet->m_magneticCore, &matrix[0][0]); dMatrix posit (GetIdentityMatrix()); posit.m_posit = matrix.m_posit; NewtonBodySetMatrix (body, &posit[0][0]); }
void SimplePlaneCollision (NewtonFrame& system) { NewtonWorld* world; LevelPrimitive *level; NewtonCollision* planeColl; world = system.m_world; // create a material carrier to collision with this object int defaultMaterialID; defaultMaterialID = NewtonMaterialGetDefaultGroupID (world); // create the sky box and the floor, level = LoadLevelAndSceneRoot (system, "flatplane.dae", 1); // Find the world mesh and replace the collision for a custom plane collision mesh dMatrix matrix; NewtonBodyGetMatrix (level->m_level, &matrix[0][0]); dVector plane (matrix.m_up); plane.m_w = - (plane % matrix.m_posit); planeColl = CreatePlaneCollidion (world, plane); NewtonBodySetCollision(level->m_level, planeColl); NewtonReleaseCollision(world, planeColl); dVector posit (0.0f, 0.0f, 0.0f, 0.0f); posit.m_y = FindFloor (world, posit.m_x, posit.m_z) + 5.0f; InitEyePoint (dVector (1.0f, 0.0f, 0.0f), posit); dVector size (1.0f, 1.0f, 1.0f); dVector location (cameraEyepoint + cameraDir.Scale (10.0f)); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _SPHERE_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _BOX_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _CONE_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _CYLINDER_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _CAPSULE_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _CHAMFER_CYLINDER_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _RANDOM_CONVEX_HULL_PRIMITIVE, defaultMaterialID); AddBoxes (&system, 10.0f, location, size, 3, 3, 10.0f, _REGULAR_CONVEX_HULL_PRIMITIVE, defaultMaterialID); }
void message(char *str) { static int count=0; int len, loc; if(strlen(str)<79) { len=79-strlen(str); while(len) { strcat(str, " "); len -= 1; } } count++; if(count>10) count=-1; loc = count+13; posit(loc, 10); printf("%s", str); }
// Transform callback to set the matrix of a the visual entity void SetTransformCallback (const NewtonBody* body, const dFloat* matrix, int threadIndex) { Entity* ent; // Get the position from the matrix dVector posit (matrix[12], matrix[13], matrix[14], 1.0f); dQuaternion rotation; // we will ignore the Rotation part of matrix and use the quaternion rotation stored in the body NewtonBodyGetRotation(body, &rotation.m_q0); // get the entity associated with this rigid body ent = (Entity*) NewtonBodyGetUserData(body); // since this tutorial run the physics and a different fps than the Graphics // we need to save the entity current transformation state before updating the new state. ent->m_prevPosition = ent->m_curPosition; ent->m_prevRotation = ent->m_curRotation; // set the new position and orientation for this entity ent->m_curPosition = posit; ent->m_curRotation = rotation; }
void ApplyTracktionForce (dFloat timestep, const NewtonBody* track) { dVector veloc; dVector omega; dMatrix matrix; NewtonBodyGetOmega(m_body0, &omega[0]); NewtonBodyGetVelocity(m_body0, &veloc[0]); NewtonBodyGetMatrix (m_body0, &matrix[0][0]); // itetate over the contact list and condition each contact direction anc contact acclerations for (NewtonJoint* contactJoint = NewtonBodyGetFirstContactJoint (track); contactJoint; contactJoint = NewtonBodyGetNextContactJoint (track, contactJoint)) { _ASSERTE ((NewtonJointGetBody0 (contactJoint) == track) || (NewtonJointGetBody1 (contactJoint) == track)); #ifdef REMOVE_REDUNDAT_CONTACT int contactCount; contactCount = NewtonContactJointGetContactCount(contactJoint); if (contactCount > 2) { // project the contact to the bounday of the conve hull o fteh trhread foot ptint dFloat maxDist; dFloat minDist; void* minContact; void* maxContact; dMatrix matrix; minContact = NULL; maxContact = NULL; NewtonBodyGetMatrix (track, &matrix[0][0]); maxDist = -1.0e10f; minDist = -1.0e10f; //find the best two contacts and remove all others for (void* contact = NewtonContactJointGetFirstContact (contactJoint); contact; contact = NewtonContactJointGetNextContact (contactJoint, contact)) { dFloat dist; dVector point; dVector normal; NewtonMaterial* material; material = NewtonContactGetMaterial (contact); NewtonMaterialGetContactPositionAndNormal(material, &point[0], &normal[0]); dist = matrix.m_front % point; if (dist > maxDist) { maxDist = dist; maxContact = contact; } if (-dist > minDist) { minDist = -dist; minContact = contact; } } // now delete all reduntact contacts void* nextContact; NewtonWorld* world; world = NewtonBodyGetWorld (track); NewtonWorldCriticalSectionLock(world); for (void* contact = NewtonContactJointGetFirstContact (contactJoint); contact; contact = nextContact) { nextContact = NewtonContactJointGetNextContact (contactJoint, contact); if (!((contact == minContact) || (contact == maxContact))) { NewtonContactJointRemoveContact (contactJoint, contact); } } NewtonWorldCriticalSectionUnlock(world); } #endif for (void* contact = NewtonContactJointGetFirstContact (contactJoint); contact; contact = NewtonContactJointGetNextContact (contactJoint, contact)) { dFloat speed; dFloat accel; dVector point; dVector normal; dVector dir0; dVector dir1; NewtonMaterial* material; material = NewtonContactGetMaterial (contact); NewtonMaterialContactRotateTangentDirections (material, &matrix.m_front[0]); NewtonMaterialGetContactPositionAndNormal(material, &point[0], &normal[0]); NewtonMaterialGetContactTangentDirections (material, &dir0[0], &dir1[0]); dVector posit (point - matrix.m_posit); veloc += omega * posit; speed = veloc % dir0; // accel = m_accel - 0.1f * speed + (((posit % m_matrix.m_right) > 0.0f) ? m_turnAccel : - m_turnAccel); accel = m_veloc + (((posit % matrix.m_right) > 0.0f) ? m_turnVeloc : - m_turnVeloc); accel = (accel - speed) * 0.5f / timestep; // NewtonMaterialSetContactStaticFrictionCoef (material, 1.0f, 0); // NewtonMaterialSetContactKineticFrictionCoef (material, 1.0f, 0); NewtonMaterialSetContactFrictionCoef (material, 1.0f, 1.0f, 0); // NewtonMaterialSetContactStaticFrictionCoef (material, 0.5f, 1); // NewtonMaterialSetContactKineticFrictionCoef (material, 0.5f, 1); NewtonMaterialSetContactFrictionCoef (material, 0.5f, 0.5f, 1); NewtonMaterialSetContactTangentAcceleration (material, accel, 0); } // for debug purpose show the contact ShowJointContacts (contactJoint); } }
void CustomVehicleController::Finalize() { dWeightDistibutionSolver solver; dFloat64 unitAccel[VEHICLE_CONTROLLER_MAX_JOINTS]; dFloat64 sprungMass[VEHICLE_CONTROLLER_MAX_JOINTS]; // make sure tire are aligned /* memset (unitAccel, sizeof (unitAccel), 0); int index = 0; for (TireList::dListNode* node0 = m_tireList.GetFirst(); node0; node0 = node0->GetNext()) { if (unitAccel[index] == 0) { CustomVehicleControllerBodyStateTire* const tire0 = &node0->GetInfo(); for (TireList::dListNode* node1 = node0->GetNext(); node1; node1 = node1->GetNext()) { CustomVehicleControllerBodyStateTire* const tire1 = &node->GetInfo(); } } } */ int count = 0; m_chassisState.m_matrix = dGetIdentityMatrix(); m_chassisState.UpdateInertia(); dVector dir (m_chassisState.m_localFrame[1]); for (dTireList::dListNode* node = m_tireList.GetFirst(); node; node = node->GetNext()) { CustomVehicleControllerBodyStateTire* const tire = &node->GetInfo(); dVector posit (tire->m_localFrame.m_posit); dComplemtaritySolver::dJacobian &jacobian0 = solver.m_jacobians[count]; dComplemtaritySolver::dJacobian &invMassJacobian0 = solver.m_invMassJacobians[count]; jacobian0.m_linear = dir; jacobian0.m_angular = posit * dir; jacobian0.m_angular.m_w = 0.0f; invMassJacobian0.m_linear = jacobian0.m_linear.Scale(m_chassisState.m_invMass); invMassJacobian0.m_angular = jacobian0.m_angular.CompProduct(m_chassisState.m_localInvInertia); dFloat diagnal = jacobian0.m_linear % invMassJacobian0.m_linear + jacobian0.m_angular % invMassJacobian0.m_angular; solver.m_diagRegularizer[count] = diagnal * 0.005f; solver.m_invDiag[count] = 1.0f / (diagnal + solver.m_diagRegularizer[count]); unitAccel[count] = 1.0f; sprungMass[count] = 0.0f; count ++; } if (count) { solver.m_count = count; solver.Solve (count, 1.0e-6f, sprungMass, unitAccel); } int index = 0; for (dTireList::dListNode* node = m_tireList.GetFirst(); node; node = node->GetNext()) { CustomVehicleControllerBodyStateTire* const tire = &node->GetInfo(); tire->m_restSprunMass = dFloat (5.0f * dFloor (sprungMass[index] / 5.0f + 0.5f)); if (m_engine) { tire->CalculateRollingResistance (m_engine->GetTopSpeed()); } index ++; } m_sleepCounter = VEHICLE_SLEEP_COUNTER; m_finalized = true; }
void dPluginCamera::DrawGizmo(dSceneRender* const render, int font) const { render->DisableZbuffer(); //render->EnableZbuffer(); render->EnableBackFace(); render->DisableLighting (); render->DisableTexture(); render->DisableBlend(); // calculate gizmo size dFloat zbuffer = 0.5f; // calculate a point the lower left corner of the screen at the front plane in global space dFloat x0 = 40.0f; dFloat y0 = render->GetViewPortHeight() - 30.0f; dVector origin (render->ScreenToGlobal(dVector (x0, y0, zbuffer, 1.0f))); dFloat length = 30.0f; dVector p1 (render->ScreenToGlobal(dVector (x0 + length, y0, zbuffer, 1.0f))); dVector p1p0(p1 - origin); length = dSqrt (p1p0.DotProduct3(p1p0)); // display x axis { dMatrix matrix (dGetIdentityMatrix()); matrix.m_posit = origin; render->PushMatrix(matrix); render->BeginLine(); render->SetColor(dVector (1.0f, 0.0f, 0.0f, 0.0f)); render->DrawLine (dVector (0.0f, 0.0f, 0.0f, 0.0f), dVector (length, 0.0f, 0.0f, 0.0f)); render->End(); dVector posit (render->GlobalToScreen(dVector (length * 1.2f, 0.0f, 0.0f, 0.0f))); render->SetColor(dVector (1.0f, 1.0f, 1.0f, 0.0f)); render->Print(font, posit.m_x, posit.m_y, "x"); render->PopMatrix(); } // display y axis { dMatrix matrix (dRollMatrix((90.0f * 3.141592f / 180.0f))); matrix.m_posit = origin; render->PushMatrix(matrix); render->BeginLine(); render->SetColor(dVector (0.0f, 1.0f, 0.0f, 0.0f)); render->DrawLine (dVector (0.0f, 0.0f, 0.0f, 0.0f), dVector (length, 0.0f, 0.0f, 0.0f)); render->End(); dVector posit (render->GlobalToScreen(dVector (length * 1.2f, 0.0f, 0.0f, 0.0f))); render->SetColor(dVector (1.0f, 1.0f, 1.0f, 0.0f)); render->Print(font, posit.m_x, posit.m_y, "y"); render->PopMatrix(); } // display z axis { dMatrix matrix (dYawMatrix((-90.0f * 3.141592f / 180.0f))); matrix.m_posit = origin; render->PushMatrix(matrix); render->BeginLine(); render->SetColor(dVector (0.0f, 0.0f, 1.0f, 0.0f)); render->DrawLine (dVector (0.0f, 0.0f, 0.0f, 0.0f), dVector (length, 0.0f, 0.0f, 0.0f)); render->End(); dVector posit (render->GlobalToScreen(dVector (length * 1.2f, 0.0f, 0.0f, 0.0f))); render->SetColor(dVector (1.0f, 1.0f, 1.0f, 0.0f)); render->Print(font, posit.m_x, posit.m_y, "z"); render->PopMatrix(); } }
void SPoint::positChange() { emit positionChanged(posit()); }