QBodyDef* WorldModel::createBody() { return createBody(static_cast<QFixtureDef*>(nullptr)); }
void phyMgr::parseUserText(const wcs& node_name, const SPtr<z3D::SG::SNode>& node, const wcs& user_text) { Config* cfg = Config::fromWCS(user_text); if(cfg->exists(L"collision_shape")) { wcs shape_name = cfg->getWString(L"collision_shape"); REAL mass = cfg->getFloat(L"mass"); int32_t compound = cfg->getInt32(L"compound"); if(shape_name == L"box") { if(compound) { SPtr<phyShape> shape = createCompoundWrappedBoxShape(node->local_bound().extent(), node->local_bound().center()); SPtr<phyBody> body = createBody(node, shape, Mat4::identity, mass, false); addBody(body); node->setPhyBody(body); } else { SPtr<phyShape> shape = createBoxShape(node->local_bound().extent()); SPtr<phyBody> body = createBody(node, shape, Mat4::translation(Vec3(node->local_bound().center())), mass, false); addBody(body); node->setPhyBody(body); } } else if(shape_name == L"sphere") { Vec3 ext = node->local_bound().extent(); SPtr<phyShape> shape = createSphereShape((ext[0] + ext[1] + ext[2]) / 3); SPtr<phyBody> body = createBody(node, shape, Mat4::translation(Vec3(node->local_bound().center())), mass, false); addBody(body); node->setPhyBody(body); } else if(shape_name == L"mesh") { if(node->type_info()->kind_of(z3D::SG::SMeshNode::type_info_static())) { SPtr<phyShape> shape = createMeshShape(node.cast<z3D::SG::SMeshNode>()->mesh()); SPtr<phyBody> body = createBody(node, shape, Mat4::identity, mass, true); addBody(body); node->setPhyBody(body); } } else if(shape_name == L"convex_hull") { if(node->type_info()->kind_of(z3D::SG::SMeshNode::type_info_static())) { Vec3 offset; REAL computed_mass; Mat3 inertia_tensor; SPtr<phyShape> shape = createConvexHullShape(node.cast<z3D::SG::SMeshNode>()->mesh(), offset, computed_mass, inertia_tensor); SPtr<phyBody> body = createBody(node, shape, Mat4::translation(offset), mass, mass / computed_mass * Vec3(inertia_tensor[0][0], inertia_tensor[1][1], inertia_tensor[2][2])); addBody(body); node->setPhyBody(body); } } else if(shape_name == L"convex_decomp") { if(node->type_info()->kind_of(z3D::SG::SMeshNode::type_info_static())) { Vec3 offset; REAL computed_mass; Mat3 inertia_tensor; SPtr<phyShape> shape = createDecompConvexHullShape(node.cast<z3D::SG::SMeshNode>()->mesh(), offset, computed_mass, inertia_tensor); SPtr<phyBody> body = createBody(node, shape, Mat4::translation(offset), mass, mass / computed_mass * Vec3(inertia_tensor[0][0], inertia_tensor[1][1], inertia_tensor[2][2])); addBody(body); node->setPhyBody(body); } } } delete cfg; }
Car::Car(dWorldID world, dSpaceID space, CarDesignID n_car_design, AppDesignID n_app_design) :MyObject(world, space), car_design(n_car_design), app_design(n_app_design) { int i, j, ind; body_obj = 0; for (i = 0; i < 2; ++i) chain_obj[i] = 0; for (i = 0; i < 6; ++i) wheel_obj[i] = 0; max_f = 0; // must be set with setMaxF TrackDesignID track_design = car_design->left_track_design; LinkDesignID link_design = track_design->link_design; WheelDesignID wheel_design = car_design->wheel_design; body_mass = car_design->getBodyMass(); // create body createBody(car_design->getChassisPosition()); /* for ( int i = 0 ; i < 6 ; ++i ) { CreateToothedWheel(world, space, wheel_obj[i], sprocket_teeth, R, t_sides, teeth_h_fuzz); } */ // create sprocket wheels in back wheels position for (j = 0, ind = FIRST_SPROCKET; j < 2; ++j, ++ind) { wheel_obj[ind] = wheel_design->create(world, space); sprocket[j] = wheel_obj[ind]->body[0]; const dReal *sprocket_pos = track_design->getBackWheelPos(); PVEC(sprocket_pos); dReal y = sprocket_pos[YY]; if (j == 1) y += car_design->getTrackToTrack(); PEXP(y); dBodySetPosition(sprocket[j], sprocket_pos[XX], y, sprocket_pos[ZZ]); } // create sprockets and set their position // create front wheels and set their position for (j = 0, ind = FIRST_FRONT; j < 2; ++j, ++ind) { wheel_obj[ind] = wheel_design->create(world, space); front[j] = wheel_obj[ind]->body[0]; const dReal *front_pos = track_design->getFrontWheelPos(); PVEC(front_pos); dReal y = front_pos[YY]; if (j == 1) y += car_design->getTrackToTrack(); PEXP(y); dBodySetPosition(front[j], front_pos[XX], y, front_pos[ZZ]); } // create chains chain_obj[0] = new Chain(world, space, track_design, link_design); track_design->moveDesign(0, car_design->getTrackToTrack(), 0); // instead of recreating design chain_obj[1] = new Chain(world, space, track_design, link_design); // create axle joints for sprockets for (j = 0, ind = FIRST_SPROCKET; j < 2; ++j, ++ind) { axle[ind] = dJointCreateHinge(world, 0); dBodyID second = (body_obj ? body_obj->body[0] : 0); dJointAttach(axle[ind], sprocket[j], second); const dReal *sp_v = dBodyGetPosition(sprocket[j]); const dReal *rot = dBodyGetRotation(sprocket[j]); dJointSetHingeAnchor(axle[ind], sp_v[XX], sp_v[YY], sp_v[ZZ]); dJointSetHingeAxis(axle[ind], rot[1], rot[5], rot[9]); } /* for ( j = 0, ind = FIRST_BACK ; j < 2 ; ++j, ++ind ) { axle[ind] = dJointCreateHinge(world, 0); // no joint group (0==NULL) dJointAttach(axle[ind], back[j], 0); // attach to world const dReal* v = dBodyGetPosition(back[j]); dJointSetHingeAnchor(axle[ind], v[XX], v[YY], v[ZZ]); dJointSetHingeAxis(axle[ind], y_axis[XX], y_axis[YY], y_axis[ZZ]); // axis is up (positive z) } */ // create front wheels joints for (j = 0, ind = FIRST_FRONT; j < 2; ++j, ++ind) { axle[ind] = dJointCreateHinge(world, 0); dBodyID second = (body_obj ? body_obj->body[0] : 0); dJointAttach(axle[ind], front[j], second); const dReal *v = dBodyGetPosition(front[j]); const dReal *rot = dBodyGetRotation(sprocket[j]); dJointSetHingeAnchor(axle[ind], v[XX], v[YY], v[ZZ]); dJointSetHingeAxis(axle[ind], rot[1], rot[5], rot[9]); } if (body_obj) body_obj->show_forces = 1; // create appendage if (body_obj && app_design) { //app_obj = new App(world, space, app_design, body_obj->body[0]); app_obj = 0; }; // lets see what we have, height wise if (chain_obj[0] != 0) { const dReal *pos = dBodyGetPosition(chain_obj[0]->body[0]); std::cout << "debug: first link position, z wise\n"; PEXP(pos[ZZ]); } #ifdef ARE_YOU_NUTS_ABOUT_SPRINGS // now create the suspension - it is between the wheels and the body. Init(0, 2, 0); // initially lets make some springs between the wheels and the body. for (int i = 0 ; i < 2 ; ++i) { dBody a_wheel = wheel_obj[i + FIRST_SPROCKET]->body[0]; }; #endif }
Gears::Gears(const b2Vec2 &gravity, QObject *parent) : QBox2DTest(gravity, parent) { //modified codes taken from Gears example b2RevoluteJoint* m_joint1; b2RevoluteJoint* m_joint2; b2PrismaticJoint* m_joint3; b2GearJoint* m_joint4; b2GearJoint* m_joint5; QBox2DBody* ground = NULL; { b2BodyDef bd; ground = createBody(&bd); b2EdgeShape shape; shape.Set(b2Vec2(50.0f, 0.0f), b2Vec2(-50.0f, 0.0f)); ground->createFixture(&shape, 0.0f); } // Gears co { b2CircleShape circle1; circle1.m_radius = 1.0f; b2PolygonShape box; box.SetAsBox(0.5f, 5.0f); b2CircleShape circle2; circle2.m_radius = 2.0f; b2BodyDef bd1; bd1.type = b2_staticBody; bd1.position.Set(10.0f, 9.0f); QBox2DBody* body1 = createBody(&bd1); body1->createFixture(&circle1, 0.0f); b2BodyDef bd2; bd2.type = b2_dynamicBody; bd2.position.Set(10.0f, 8.0f); QBox2DBody* body2 = createBody(&bd2); body2->createFixture(&box, 5.0f); b2BodyDef bd3; bd3.type = b2_dynamicBody; bd3.position.Set(10.0f, 6.0f); QBox2DBody* body3 = createBody(&bd3); body3->createFixture(&circle2, 5.0f); b2RevoluteJointDef jd1; jd1.Initialize(body2->body(), body1->body(), bd1.position); QBox2DJoint* joint1 = createJoint(&jd1); b2RevoluteJointDef jd2; jd2.Initialize(body2->body(), body3->body(), bd3.position); QBox2DJoint* joint2 = createJoint(&jd2); b2GearJointDef jd4; jd4.bodyA = body1->body(); jd4.bodyB = body3->body(); jd4.joint1 = joint1->joint(); jd4.joint2 = joint2->joint(); jd4.ratio = circle2.m_radius / circle1.m_radius; createJoint(&jd4); } { b2CircleShape circle1; circle1.m_radius = 1.0f; b2CircleShape circle2; circle2.m_radius = 2.0f; b2PolygonShape box; box.SetAsBox(0.5f, 5.0f); b2BodyDef bd1; bd1.type = b2_dynamicBody; bd1.position.Set(-3.0f, 12.0f); QBox2DBody* body1 = createBody(&bd1); body1->createFixture(&circle1, 5.0f); b2RevoluteJointDef jd1; jd1.bodyA = ground->body(); jd1.bodyB = body1->body(); jd1.localAnchorA = ground->getLocalPoint(bd1.position); jd1.localAnchorB = body1->getLocalPoint(bd1.position); jd1.referenceAngle = body1->getAngle() - ground->getAngle(); m_joint1 = (b2RevoluteJoint*)createJoint(&jd1)->joint(); b2BodyDef bd2; bd2.type = b2_dynamicBody; bd2.position.Set(0.0f, 12.0f); QBox2DBody* body2 = createBody(&bd2); body2->createFixture(&circle2, 5.0f); b2RevoluteJointDef jd2; jd2.Initialize(ground->body(), body2->body(), bd2.position); m_joint2 = (b2RevoluteJoint*)createJoint(&jd2)->joint(); b2BodyDef bd3; bd3.type = b2_dynamicBody; bd3.position.Set(2.5f, 12.0f); QBox2DBody* body3 = createBody(&bd3); body3->createFixture(&box, 5.0f); b2PrismaticJointDef jd3; jd3.Initialize(ground->body(), body3->body(), bd3.position, b2Vec2(0.0f, 1.0f)); jd3.lowerTranslation = -5.0f; jd3.upperTranslation = 5.0f; jd3.enableLimit = true; m_joint3 = (b2PrismaticJoint*)createJoint(&jd3)->joint(); b2GearJointDef jd4; jd4.bodyA = body1->body(); jd4.bodyB = body2->body(); jd4.joint1 = m_joint1; jd4.joint2 = m_joint2; jd4.ratio = circle2.m_radius / circle1.m_radius; m_joint4 = (b2GearJoint*)createJoint(&jd4)->joint(); b2GearJointDef jd5; jd5.bodyA = body2->body(); jd5.bodyB = body3->body(); jd5.joint1 = m_joint2; jd5.joint2 = m_joint3; jd5.ratio = -1.0f / circle2.m_radius; m_joint5 = (b2GearJoint*)createJoint(&jd5)->joint(); } }
/** Creates a bullet physics body for the flyable item. * \param forw_offset How far ahead of the kart the flyable should be * positioned. Necessary to avoid exploding a rocket inside of the * firing kart. * \param velocity Initial velocity of the flyable. * \param shape Collision shape of the flyable. * \param gravity Gravity to use for this flyable. * \param rotates True if the item should rotate, otherwise the angular factor * is set to 0 preventing rotations from happening. * \param turn_around True if the item is fired backwards. * \param custom_direction If defined the initial heading for this item, * otherwise the kart's heading will be used. */ void Flyable::createPhysics(float forw_offset, const Vec3 &velocity, btCollisionShape *shape, float restitution, const btVector3& gravity, const bool rotates, const bool turn_around, const btTransform* custom_direction) { // Get Kart heading direction btTransform trans = ( !custom_direction ? m_owner->getAlignedTransform() : *custom_direction ); // Apply offset btTransform offset_transform; offset_transform.setIdentity(); assert(!std::isnan(m_average_height)); assert(!std::isnan(forw_offset)); offset_transform.setOrigin(Vec3(0,m_average_height,forw_offset)); // turn around if(turn_around) { btTransform turn_around_trans; //turn_around_trans.setOrigin(trans.getOrigin()); turn_around_trans.setIdentity(); turn_around_trans.setRotation(btQuaternion(btVector3(0, 1, 0), M_PI)); trans *= turn_around_trans; } trans *= offset_transform; m_shape = shape; createBody(m_mass, trans, m_shape, restitution); m_user_pointer.set(this); Physics::getInstance()->addBody(getBody()); m_body->setGravity(gravity); // Rotate velocity to point in the right direction btVector3 v=trans.getBasis()*velocity; if(m_mass!=0.0f) // Don't set velocity for kinematic or static objects { #ifdef DEBUG // Just to get some additional information if the assert is triggered if(std::isnan(v.getX()) || std::isnan(v.getY()) || std::isnan(v.getZ())) { Log::debug("[Flyable]", "vel %f %f %f v %f %f %f", velocity.getX(),velocity.getY(),velocity.getZ(), v.getX(),v.getY(),v.getZ()); } #endif assert(!std::isnan(v.getX())); assert(!std::isnan(v.getY())); assert(!std::isnan(v.getZ())); m_body->setLinearVelocity(v); if(!rotates) m_body->setAngularFactor(0.0f); // prevent rotations } m_body->setCollisionFlags(m_body->getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE); m_saved_transform = getTrans(); m_saved_lv = m_body->getLinearVelocity(); m_saved_av = m_body->getAngularVelocity(); m_saved_gravity = gravity; } // createPhysics
void YGEBodyAsset::setSize(const YGEMath::Vector3 &size){ bodySize = size; if(hasBody) { createBody(); } }
Acad::ErrorStatus PDEcone::dxfInFields(AcDbDxfFiler* filer) { assertWriteEnabled(); Acad::ErrorStatus es = Acad::eOk; struct resbuf rb; if ((PDPrimary3D::dxfInFields(filer) != Acad::eOk) || !filer->atSubclassData(_T("PDEcone"))) { return filer->filerStatus(); } // Read version number. Adesk::UInt16 version; filer->readItem(&rb); if (rb.restype != AcDb::kDxfInt16) { filer->pushBackItem(); filer->setError(Acad::eInvalidDxfCode, _T("nError: expected object version group code %d"), AcDb::kDxfInt16); return filer->filerStatus(); } else { version = rb.resval.rint; if (version > VERSION_PDECONE) return Acad::eMakeMeProxy; } // Read the data members. switch (version){ case (1): while ((es == Acad::eOk) && ((es = filer->readResBuf(&rb)) == Acad::eOk)){ switch (rb.restype){ case AcDb::kDxfXCoord: m_ptStart = asPnt3d(rb.resval.rpoint); break; case AcDb::kDxfXCoord+1: m_ptEnd = asPnt3d(rb.resval.rpoint); break; case AcDb::kDxfNormalX: m_vect = asVec3d(rb.resval.rpoint); break; case AcDb::kDxfReal: m_dDiameter1=rb.resval.rreal; break; case AcDb::kDxfReal+1: m_dDiameter2=rb.resval.rreal; break; default: // An unrecognized group. Push it back so that // the subclass can read it again. filer->pushBackItem(); es = Acad::eEndOfFile; break; } } break; } #ifdef _USEAMODELER_ if(m_3dGeom.isNull()) createBody(); #endif // At this point the es variable must contain eEndOfFile // - either from readResBuf() or from pushback. If not, // it indicates that an error happened and we should // return immediately. // if (es != Acad::eEndOfFile) return Acad::eInvalidResBuf; return filer->filerStatus(); }
GameBodyObject::GameBodyObject(const Ogre::String& name, const Ogre::String& meshName, Scene* scene) throw (Exception) : GameActorObject(name, scene, NULL) { createBody(meshName); }
Web::Web(const b2Vec2 &gravity, QObject *parent) : QBox2DTest(gravity, parent) { QBox2DBody* ground = NULL; { b2BodyDef bd; ground = createBody(&bd); b2EdgeShape shape; shape.Set(b2Vec2(-40.0f, 0.0f), b2Vec2(40.0f, 0.0f)); ground->createFixture(&shape, 0.0f); } { b2PolygonShape shape; shape.SetAsBox(0.5f, 0.5f); b2BodyDef bd; bd.type = b2_dynamicBody; bd.position.Set(-5.0f, 5.0f); m_bodies[0] = createBody(&bd); m_bodies[0]->createFixture(&shape, 5.0f); bd.position.Set(5.0f, 5.0f); m_bodies[1] = createBody(&bd); m_bodies[1]->createFixture(&shape, 5.0f); bd.position.Set(5.0f, 15.0f); m_bodies[2] = createBody(&bd); m_bodies[2]->createFixture(&shape, 5.0f); bd.position.Set(-5.0f, 15.0f); m_bodies[3] = createBody(&bd); m_bodies[3]->createFixture(&shape, 5.0f); b2DistanceJointDef jd; b2Vec2 p1, p2, d; jd.frequencyHz = 2.0f; jd.dampingRatio = 0.0f; jd.bodyA = ground->body(); jd.bodyB = m_bodies[0]->body(); jd.localAnchorA.Set(-10.0f, 0.0f); jd.localAnchorB.Set(-0.5f, -0.5f); p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); m_joints[0] = createJoint(&jd); jd.bodyA = ground->body(); jd.bodyB = m_bodies[1]->body(); jd.localAnchorA.Set(10.0f, 0.0f); jd.localAnchorB.Set(0.5f, -0.5f); p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); m_joints[1] = createJoint(&jd); jd.bodyA = ground->body(); jd.bodyB = m_bodies[2]->body(); jd.localAnchorA.Set(10.0f, 20.0f); jd.localAnchorB.Set(0.5f, 0.5f); p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); m_joints[2] = createJoint(&jd); jd.bodyA = ground->body(); jd.bodyB = m_bodies[3]->body(); jd.localAnchorA.Set(-10.0f, 20.0f); jd.localAnchorB.Set(-0.5f, 0.5f); p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); m_joints[3] = createJoint(&jd); jd.bodyA = m_bodies[0]->body(); jd.bodyB = m_bodies[1]->body(); jd.localAnchorA.Set(0.5f, 0.0f); jd.localAnchorB.Set(-0.5f, 0.0f);; p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); m_joints[4] = createJoint(&jd); jd.bodyA = m_bodies[1]->body(); jd.bodyB = m_bodies[2]->body(); jd.localAnchorA.Set(0.0f, 0.5f); jd.localAnchorB.Set(0.0f, -0.5f); p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); m_joints[5] = createJoint(&jd); jd.bodyA = m_bodies[2]->body(); jd.bodyB = m_bodies[3]->body(); jd.localAnchorA.Set(-0.5f, 0.0f); jd.localAnchorB.Set(0.5f, 0.0f); p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); m_joints[6] = createJoint(&jd); jd.bodyA = m_bodies[3]->body(); jd.bodyB = m_bodies[0]->body(); jd.localAnchorA.Set(0.0f, -0.5f); jd.localAnchorB.Set(0.0f, 0.5f); p1 = jd.bodyA->GetWorldPoint(jd.localAnchorA); p2 = jd.bodyB->GetWorldPoint(jd.localAnchorB); d = p2 - p1; jd.length = d.Length(); m_joints[7] = createJoint(&jd); } }
b2Body* Box2DSystem::createSpawnComponentBody(float x, float y, SpawnComponent::TYPE type, b2BodyType btype) { float width = (type == SpawnComponent::BOX) ? conf::box_halfwidth : conf::circle_radius; return createBody(x, y, width, width, type, btype); }
b2Body* Box2DSystem::createDynamicBox(float x, float y, float halfwidth, float halfheight) { return createBody(x, y, halfwidth, halfheight, SpawnComponent::BOX, b2_dynamicBody); }
void *NclStructureParser::parseBody( DOMElement *parentElement, void *objGrandParent) { void *parentObject; DOMNodeList *elementNodeList; int i, size; DOMNode *node; DOMElement *element; string elementTagName; void *elementObject; parentObject = createBody(parentElement, objGrandParent); if (parentObject == NULL) { return NULL; } elementNodeList = parentElement->getChildNodes(); size = elementNodeList->getLength(); for (i = 0; i < size; i++) { node = elementNodeList->item(i); if(node->getNodeType()==DOMNode::ELEMENT_NODE){ element = (DOMElement*)node; elementTagName = XMLString::transcode(element->getTagName()); if (XMLString::compareIString( elementTagName.c_str(), "media")==0) { elementObject = getComponentsParser()->parseMedia( element, parentObject); if (elementObject != NULL) { addMediaToBody(parentObject, elementObject); } } else if (XMLString::compareIString( elementTagName.c_str(), "context")==0) { elementObject = getComponentsParser()->parseContext( element, parentObject); if (elementObject != NULL) { addContextToBody(parentObject, elementObject); } } else if (XMLString::compareIString( elementTagName.c_str(), "switch")==0) { elementObject = getPresentationControlParser()-> parseSwitch(element, parentObject); if (elementObject != NULL) { addSwitchToBody(parentObject, elementObject); } } } } for (i = 0; i < size; i++) { node = elementNodeList->item(i); if (node->getNodeType()==DOMNode::ELEMENT_NODE && XMLString::compareIString(((DOMElement*)node)-> getTagName(), XMLString::transcode("property"))==0) { elementObject = getInterfacesParser()->parseProperty( (DOMElement*)node, parentObject); if (elementObject != NULL) { addPropertyToBody(parentObject, elementObject); } } } return parentObject; }