Extrusion::Extrusion(Vector3 pos, Vector3 size, Vector3 axis, float extrusionTravel, hkpWorld * world, SceneManager * sceneMgr): mWorld(world), mSceneMgr(sceneMgr) { hkVector4 halfSize(size.x * 0.5f, size.y * 0.5, size.z * 0.5); hkpBoxShape * shape = new hkpBoxShape(halfSize, 0); hkpRigidBodyCinfo info; info.m_shape = shape; info.m_mass = 800.0f; hkpInertiaTensorComputer::setShapeVolumeMassProperties(shape, info.m_mass, info); info.m_motionType = hkpMotion::MOTION_BOX_INERTIA; info.m_position.set(pos.x, pos.y, pos.z); mBody = new hkpRigidBody(info); mWorld->addEntity(mBody); shape->removeReference(); hkpRigidBodyCinfo anchorInfo; anchorInfo.m_motionType = hkpMotion::MOTION_FIXED; anchorInfo.m_position = hkVector4(pos.x + (20.0f * axis.x), pos.y, pos.z + (20.0f * axis.z)); anchorInfo.m_shape = new hkpSphereShape(0.1f); hkpRigidBody * anchor = new hkpRigidBody(anchorInfo); mWorld->addEntity(anchor); // Setup prismatic constraint hkVector4 a(axis.x, axis.y, axis.z); hkpPrismaticConstraintData * prismatic = new hkpPrismaticConstraintData(); prismatic->setMaxLinearLimit(extrusionTravel); prismatic->setMinLinearLimit(0.0f); prismatic->setInWorldSpace(mBody->getTransform(), anchor->getTransform(), hkVector4(pos.x, pos.y, pos.z), a); hkpConstraintInstance * prismaticConstraint = new hkpConstraintInstance(mBody, anchor, prismatic); mWorld->addConstraint(prismaticConstraint); prismaticConstraint->removeReference(); prismatic->removeReference(); // Ogre char entityName[] = "000ExtrusionEntity"; entityName[0] += numExtrusions; mMesh = mSceneMgr->createEntity(entityName, "cube.mesh"); AxisAlignedBox aab = mMesh->getBoundingBox(); mMesh->setMaterialName("Examples/CubeDefault"); Vector3 meshSize = aab.getSize(); Vector3 scaling = size / meshSize; char nodeName[] = "000ExtrusionNode"; nodeName[0] += numExtrusions; mNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(nodeName); mNode->setPosition(pos.x, pos.y, pos.z); mNode->setScale(scaling); mNode->attachObject(mMesh); numExtrusions++; }
static void HK_CALL createHinge(hkpWorld* world, hkpCollidableQualityType bodyQualityType, hkpConstraintInstance::ConstraintPriority constraintPriority, hkVector4& position, hkVector4& velocity) { // create to boards connected by a hinge hkpRigidBody* bodyA; hkpRigidBody* bodyB; hkReal pivotY = 0.40f; { hkVector4 halfSize(.1f, 1.0f, 0.1f); hkpConvexShape* shape = new hkpBoxShape(halfSize, 0.01f); hkpRigidBodyCinfo ci; ci.m_motionType = hkpMotion::MOTION_SPHERE_INERTIA; ci.m_shape = shape; ci.m_angularDamping = 0.0f; ci.m_linearDamping = 0.0f; ci.m_restitution = 0.0f; ci.m_friction = 0.2f; ci.m_linearVelocity = velocity; hkpInertiaTensorComputer::setShapeVolumeMassProperties( shape, 1.0f, ci ); ci.m_qualityType = bodyQualityType; ci.m_collisionFilterInfo = hkpGroupFilter::calcFilterInfo(30); ci.m_position.set( -2.0f, -1.0f - pivotY, 0.0f ); bodyA = new hkpRigidBody(ci); bodyA->setName("Body0"); world->addEntity( bodyA ); ci.m_position.set( -2.0f, 1.0f - pivotY, 0.0f ); bodyB = new hkpRigidBody(ci); bodyB->setName("Body1"); world->addEntity( bodyB ); shape->removeReference(); } // create a hing connecting the two bodies { hkVector4 axis( 0,0,1.f); hkVector4 pivot( -2.0f, -pivotY, 0.0f ); hkpHingeConstraintData* constraintData = new hkpHingeConstraintData(); constraintData->setInWorldSpace( bodyA->getTransform(), bodyB->getTransform(), pivot, axis ); world->addConstraint( new hkpConstraintInstance( bodyA, bodyB, constraintData, constraintPriority ))->removeReference(); constraintData->removeReference(); } bodyA->removeReference(); bodyB->removeReference(); }
bool CCmpPathfinder::CheckBuildingPlacement(const IObstructionTestFilter& filter, entity_pos_t x, entity_pos_t z, entity_pos_t a, entity_pos_t w, entity_pos_t h, entity_id_t id, pass_class_t passClass) { // Check unit obstruction CmpPtr<ICmpObstructionManager> cmpObstructionManager(GetSimContext(), SYSTEM_ENTITY); if (cmpObstructionManager.null()) return false; if (cmpObstructionManager->TestStaticShape(filter, x, z, a, w, h, NULL)) return false; // Test against terrain: UpdateGrid(); CmpPtr<ICmpObstruction> cmpObstruction(GetSimContext(), id); if (cmpObstruction.null()) return false; ICmpObstructionManager::ObstructionSquare square; if (!cmpObstruction->GetObstructionSquare(square)) return false; // Expand bounds by 1/sqrt(2) tile (multiply by TERRAIN_TILE_SIZE since we want world coordinates) entity_pos_t expand = entity_pos_t::FromInt(2).Sqrt().Multiply(entity_pos_t::FromInt(TERRAIN_TILE_SIZE / 2)); CFixedVector2D halfSize(square.hw + expand, square.hh + expand); CFixedVector2D halfBound = Geometry::GetHalfBoundingBox(square.u, square.v, halfSize); u16 i0, j0, i1, j1; NearestTile(square.x - halfBound.X, square.z - halfBound.Y, i0, j0); NearestTile(square.x + halfBound.X, square.z + halfBound.Y, i1, j1); for (u16 j = j0; j <= j1; ++j) { for (u16 i = i0; i <= i1; ++i) { entity_pos_t x, z; TileCenter(i, j, x, z); if (Geometry::PointIsInSquare(CFixedVector2D(x - square.x, z - square.z), square.u, square.v, halfSize) && !IS_TERRAIN_PASSABLE(m_Grid->get(i,j), passClass)) { return false; } } } return true; }
void Path::addRoundedRect(const FloatRect& rect, const FloatSize& roundingRadii) { if (rect.isEmpty()) return; FloatSize radius(roundingRadii); FloatSize halfSize(rect.width() / 2, rect.height() / 2); // If rx is greater than half of the width of the rectangle // then set rx to half of the width (required in SVG spec) if (radius.width() > halfSize.width()) radius.setWidth(halfSize.width()); // If ry is greater than half of the height of the rectangle // then set ry to half of the height (required in SVG spec) if (radius.height() > halfSize.height()) radius.setHeight(halfSize.height()); moveTo(FloatPoint(rect.x() + radius.width(), rect.y())); if (radius.width() < halfSize.width()) addLineTo(FloatPoint(rect.x() + rect.width() - roundingRadii.width(), rect.y())); addBezierCurveTo(FloatPoint(rect.x() + rect.width() - radius.width() * gCircleControlPoint, rect.y()), FloatPoint(rect.x() + rect.width(), rect.y() + radius.height() * gCircleControlPoint), FloatPoint(rect.x() + rect.width(), rect.y() + radius.height())); if (radius.height() < halfSize.height()) addLineTo(FloatPoint(rect.x() + rect.width(), rect.y() + rect.height() - radius.height())); addBezierCurveTo(FloatPoint(rect.x() + rect.width(), rect.y() + rect.height() - radius.height() * gCircleControlPoint), FloatPoint(rect.x() + rect.width() - radius.width() * gCircleControlPoint, rect.y() + rect.height()), FloatPoint(rect.x() + rect.width() - radius.width(), rect.y() + rect.height())); if (radius.width() < halfSize.width()) addLineTo(FloatPoint(rect.x() + radius.width(), rect.y() + rect.height())); addBezierCurveTo(FloatPoint(rect.x() + radius.width() * gCircleControlPoint, rect.y() + rect.height()), FloatPoint(rect.x(), rect.y() + rect.height() - radius.height() * gCircleControlPoint), FloatPoint(rect.x(), rect.y() + rect.height() - radius.height())); if (radius.height() < halfSize.height()) addLineTo(FloatPoint(rect.x(), rect.y() + radius.height())); addBezierCurveTo(FloatPoint(rect.x(), rect.y() + radius.height() * gCircleControlPoint), FloatPoint(rect.x() + radius.width() * gCircleControlPoint, rect.y()), FloatPoint(rect.x() + radius.width(), rect.y())); closeSubpath(); }
void Path::addRoundedRect(const FloatRect& rect, const FloatSize& roundingRadii) { if (rect.isEmpty()) return; FloatSize radius(roundingRadii); FloatSize halfSize(rect.width() / 2, rect.height() / 2); // If rx is greater than half of the width of the rectangle // then set rx to half of the width (required in SVG spec) if (radius.width() > halfSize.width()) radius.setWidth(halfSize.width()); // If ry is greater than half of the height of the rectangle // then set ry to half of the height (required in SVG spec) if (radius.height() > halfSize.height()) radius.setHeight(halfSize.height()); addBeziersForRoundedRect(rect, radius, radius, radius, radius); }
fixed CCmpPathfinder::DistanceToGoal(CFixedVector2D pos, const CCmpPathfinder::Goal& goal) { switch (goal.type) { case CCmpPathfinder::Goal::POINT: return (pos - CFixedVector2D(goal.x, goal.z)).Length(); case CCmpPathfinder::Goal::CIRCLE: return ((pos - CFixedVector2D(goal.x, goal.z)).Length() - goal.hw).Absolute(); case CCmpPathfinder::Goal::SQUARE: { CFixedVector2D halfSize(goal.hw, goal.hh); CFixedVector2D d(pos.X - goal.x, pos.Y - goal.z); return Geometry::DistanceToSquare(d, goal.u, goal.v, halfSize); } default: debug_warn(L"invalid type"); return fixed::Zero(); } }
std::shared_ptr<CSG> Ring(float halfWidth, float halfHeight, float centerX, float centerY, float thickness, const Color * inputColorPointer) { Vector center(centerX, centerY); Vector halfSize(halfWidth, halfHeight); Vector low = center - halfSize; Vector high = center + halfSize; std::shared_ptr<CSG> u(new Union(inputColorPointer, true)); std::shared_ptr<CSG> in(new Intersection(inputColorPointer, true)); std::shared_ptr<Shape> outer(new Ellipse(low, high, inputColorPointer)); Vector innerWay(thickness, thickness); low = low + innerWay; high = high - innerWay; std::shared_ptr<Shape> inner(new Ellipse(low, high, inputColorPointer, false)); in->AddElement(outer); in->AddElement(inner); u->AddElement(in); return (std::shared_ptr<CSG>) u; }
void VideoDecoderThread::sendFrame(AVFrame* pFrame) { VideoMsgPtr pMsg(new VideoMsg()); vector<BitmapPtr> pBmps; if (pixelFormatIsPlanar(m_PF)) { ScopeTimer timer(CopyImageProfilingZone); IntPoint halfSize(m_Size.x/2, m_Size.y/2); pBmps.push_back(getBmp(m_pBmpQ, m_Size, I8)); pBmps.push_back(getBmp(m_pHalfBmpQ, halfSize, I8)); pBmps.push_back(getBmp(m_pHalfBmpQ, halfSize, I8)); if (m_PF == YCbCrA420p) { pBmps.push_back(getBmp(m_pBmpQ, m_Size, I8)); } for (unsigned i = 0; i < pBmps.size(); ++i) { m_pFrameDecoder->copyPlaneToBmp(pBmps[i], pFrame->data[i], pFrame->linesize[i]); } } else { pBmps.push_back(getBmp(m_pBmpQ, m_Size, m_PF)); m_pFrameDecoder->convertFrameToBmp(pFrame, pBmps[0]); } pMsg->setFrame(pBmps, m_pFrameDecoder->getCurTime()); pushMsg(pMsg); }
AARect Area::MakeAARect(const Vector2 ¢re) const { Vector2 halfSize(Vector2(m_width, m_height) * 0.5f); return AARect(-halfSize + centre, halfSize + centre); }
osg::ref_ptr<osg::Geometry> ReaderWriterVRML2::convertVRML97Box(openvrml::node* vrml_box) const { std::auto_ptr<openvrml::field_value> fv = vrml_box->field("size"); const openvrml::vec3f &size = static_cast<const openvrml::sfvec3f *> (fv.get())->value(); osg::Vec3 halfSize(size[0] * 0.5f, size[1] * 0.5f, size[2] * 0.5f); BoxLibrary::const_iterator it = m_boxLibrary.find(halfSize); if (it != m_boxLibrary.end()) { return (*it).second.get(); } osg::ref_ptr<osg::Geometry> osg_geom = new osg::Geometry(); osg::ref_ptr<osg::Vec3Array> osg_vertices = new osg::Vec3Array(); osg::ref_ptr<osg::Vec2Array> osg_texcoords = new osg::Vec2Array(); osg::ref_ptr<osg::Vec3Array> osg_normals = new osg::Vec3Array(); osg::ref_ptr<osg::DrawArrays> box = new osg::DrawArrays(osg::PrimitiveSet::QUADS); osg_vertices->push_back(osg::Vec3(-halfSize[0], halfSize[1], halfSize[2])); osg_vertices->push_back(osg::Vec3(-halfSize[0], -halfSize[1], halfSize[2])); osg_vertices->push_back(osg::Vec3(halfSize[0], -halfSize[1], halfSize[2])); osg_vertices->push_back(osg::Vec3(halfSize[0], halfSize[1], halfSize[2])); osg_vertices->push_back(osg::Vec3(halfSize[0], halfSize[1], -halfSize[2])); osg_vertices->push_back(osg::Vec3(halfSize[0], -halfSize[1], -halfSize[2])); osg_vertices->push_back(osg::Vec3(-halfSize[0], -halfSize[1], -halfSize[2])); osg_vertices->push_back(osg::Vec3(-halfSize[0], halfSize[1], -halfSize[2])); osg_vertices->push_back(osg::Vec3(halfSize[0], halfSize[1], halfSize[2])); osg_vertices->push_back(osg::Vec3(halfSize[0], -halfSize[1], halfSize[2])); osg_vertices->push_back(osg::Vec3(halfSize[0], -halfSize[1], -halfSize[2])); osg_vertices->push_back(osg::Vec3(halfSize[0], halfSize[1], -halfSize[2])); osg_vertices->push_back(osg::Vec3(-halfSize[0], halfSize[1], -halfSize[2])); osg_vertices->push_back(osg::Vec3(-halfSize[0], -halfSize[1], -halfSize[2])); osg_vertices->push_back(osg::Vec3(-halfSize[0], -halfSize[1], halfSize[2])); osg_vertices->push_back(osg::Vec3(-halfSize[0], halfSize[1], halfSize[2])); osg_vertices->push_back(osg::Vec3(-halfSize[0], halfSize[1], -halfSize[2])); osg_vertices->push_back(osg::Vec3(-halfSize[0], halfSize[1], halfSize[2])); osg_vertices->push_back(osg::Vec3(halfSize[0], halfSize[1], halfSize[2])); osg_vertices->push_back(osg::Vec3(halfSize[0], halfSize[1], -halfSize[2])); osg_vertices->push_back(osg::Vec3(-halfSize[0], -halfSize[1], halfSize[2])); osg_vertices->push_back(osg::Vec3(-halfSize[0], -halfSize[1], -halfSize[2])); osg_vertices->push_back(osg::Vec3(halfSize[0], -halfSize[1], -halfSize[2])); osg_vertices->push_back(osg::Vec3(halfSize[0], -halfSize[1], halfSize[2])); for (int i = 0; i != 6; ++i) { osg_texcoords->push_back(osg::Vec2(0.0f, 1.0f)); osg_texcoords->push_back(osg::Vec2(0.0f, 0.0f)); osg_texcoords->push_back(osg::Vec2(1.0f, 0.0f)); osg_texcoords->push_back(osg::Vec2(1.0f, 1.0f)); } osg_normals->push_back(osg::Vec3(0.0f, 0.0f, 1.0f)); osg_normals->push_back(osg::Vec3(0.0f, 0.0f, -1.0f)); osg_normals->push_back(osg::Vec3(1.0f, 0.0f, 0.0f)); osg_normals->push_back(osg::Vec3(-1.0f, 0.0f, 0.0f)); osg_normals->push_back(osg::Vec3(0.0f, 1.0f, 0.0f)); osg_normals->push_back(osg::Vec3(0.0f, -1.0f, 0.0f)); box->setCount(osg_vertices->size()); osg_geom->addPrimitiveSet(box.get()); osg_geom->setVertexArray(osg_vertices.get()); osg_geom->setTexCoordArray(0, osg_texcoords.get()); osg_geom->setNormalArray(osg_normals.get()); osg_geom->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE); osg_geom->getOrCreateStateSet()->setAttributeAndModes(new osg::CullFace(osg::CullFace::BACK)); m_boxLibrary[halfSize] = osg_geom; return osg_geom.get(); }
DisablingConstraintsDemo::DisablingConstraintsDemo(hkDemoEnvironment* env) : hkDefaultPhysicsDemo(env) { m_time = 0.0f; m_constraintsEnabled = true; // // Setup the camera // { hkVector4 from(0.0f, 0.0f, 10.0f); hkVector4 to (0.0f, 0.0f, 0.0f); hkVector4 up (0.0f, 1.0f, 0.0f); setupDefaultCameras( env, from, to, up ); } // // Create the world // { hkpWorldCinfo info; info.setupSolverInfo(hkpWorldCinfo::SOLVER_TYPE_8ITERS_HARD); info.setBroadPhaseWorldSize( 100.0f ); info.m_enableDeactivation = false; m_world = new hkpWorld( info ); m_world->lock(); // // Create constraint viewer // m_debugViewerNames.pushBack( hkpConstraintViewer::getName() ); hkpConstraintViewer::m_scale = 1.0f; setupGraphics(); // Register the single agent required (a hkpBoxBoxAgent) hkpAgentRegisterUtil::registerAllAgents( m_world->getCollisionDispatcher() ); } // // Create vectors to be used for setup // hkVector4 halfSize(0.5f, 0.5f, 0.5f); hkVector4 size; size.setMul4(2.0f, halfSize); hkVector4 position(size(0), -size(1) -0.1f, 0.0f); // // Create Box Shape // hkpBoxShape* boxShape; { boxShape = new hkpBoxShape( halfSize , 0 ); } // // Get fixed rigid body // hkpRigidBody* fixedBody; { fixedBody = m_world->getFixedRigidBody(); } // // Create movable rigid body // hkpRigidBody* moveableBody; { hkpRigidBodyCinfo info; info.m_position = position; info.m_shape = boxShape; // Compute the box inertia tensor hkpMassProperties massProperties; info.m_mass = 10.0f; hkpInertiaTensorComputer::computeBoxVolumeMassProperties(halfSize, info.m_mass, massProperties); info.m_inertiaTensor = massProperties.m_inertiaTensor; info.m_centerOfMass = massProperties.m_centerOfMass; info.m_motionType = hkpMotion::MOTION_BOX_INERTIA; moveableBody = new hkpRigidBody(info); m_world->addEntity(moveableBody); moveableBody->removeReference(); } // // Cleanup references to shared shapes // boxShape->removeReference(); boxShape = HK_NULL; // // CREATE LIMITED HINGE CONSTRAINT // { hkpLimitedHingeConstraintData* lhc = new hkpLimitedHingeConstraintData(); // Set the pivot hkVector4 pivot; pivot.setAdd4(position, halfSize); // Move pivot to center of side on cube pivot(0) -= size(0); pivot(2) -= halfSize(2); hkVector4 axis(0.0f, 0.0f, 1.0f); // Create constraint lhc->setInWorldSpace(moveableBody->getTransform(), fixedBody->getTransform(), pivot, axis); lhc->setMinAngularLimit(-HK_REAL_PI/3.0f); lhc->setMaxAngularLimit(HK_REAL_PI/4.0f); { hkpConstraintInstance* constraint = new hkpConstraintInstance( moveableBody, fixedBody, lhc ); m_world->addConstraint(constraint); constraint->removeReference(); } lhc->removeReference(); } // // Create ground box // hkVector4 groundSize; groundSize.set(10.0f, 0.01f, 10.0f); hkpRigidBody* groundBody = GameUtils::createBox( groundSize, 0, hkVector4(0.0f, -3.0f, 0.0f) ); m_world->addEntity(groundBody); groundBody->removeReference(); m_world->unlock(); }
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> cPoint cRect::centerPoint() const { return position_ + halfSize(); }
PointToPlaneDemo::PointToPlaneDemo(hkDemoEnvironment* env) : hkDefaultPhysicsDemo(env) { // // Setup the camera // { hkVector4 from(5.0f, 2.0f, 10.0f); hkVector4 to (2.0f, 0.0f, 0.0f); hkVector4 up (0.0f, 1.0f, 0.0f); setupDefaultCameras( env, from, to, up ); } // // Create the world // { hkpWorldCinfo info; info.setupSolverInfo(hkpWorldCinfo::SOLVER_TYPE_4ITERS_MEDIUM); info.setBroadPhaseWorldSize( 100.0f ); info.m_enableDeactivation = false; m_world = new hkpWorld( info ); m_world->lock(); // Register the single agent required (a hkpBoxBoxAgent) hkpAgentRegisterUtil::registerAllAgents( m_world->getCollisionDispatcher() ); // // Create constraint viewer // m_debugViewerNames.pushBack( hkpConstraintViewer::getName() ); hkpConstraintViewer::m_scale = 1.0f; setupGraphics(); } // // Create vectors to be used for setup // hkVector4 halfSize (0.5f, 0.5f, 0.5f); hkVector4 position1(0.f,0.f,0.f); hkVector4 position2(3.0f,-0.5f,0.f); // // Create Box Shape // hkpBoxShape* boxShape; { boxShape = new hkpBoxShape( halfSize , 0 ); } // // Create fixed rigid body // hkpRigidBody* fixedBody; { hkpRigidBodyCinfo info; info.m_position = position1; info.m_shape = boxShape; info.m_motionType = hkpMotion::MOTION_FIXED; fixedBody = new hkpRigidBody(info); m_world->addEntity(fixedBody); fixedBody->removeReference(); } // // Create movable rigid body // hkpRigidBody* moveableBody; { hkpRigidBodyCinfo info; info.m_position = position2; info.m_shape = boxShape; // Compute the box inertia tensor hkpMassProperties massProperties; info.m_mass = 10.0f; hkpInertiaTensorComputer::computeBoxVolumeMassProperties(halfSize, info.m_mass, massProperties); info.m_inertiaTensor = massProperties.m_inertiaTensor; info.m_centerOfMass = massProperties.m_centerOfMass; info.m_motionType = hkpMotion::MOTION_BOX_INERTIA; moveableBody = new hkpRigidBody(info); m_world->addEntity(moveableBody); moveableBody->removeReference(); } // // Cleanup shared shape references // boxShape->removeReference(); boxShape = HK_NULL; // // Create point to plane constraint // { hkpPointToPlaneConstraintData* plane = new hkpPointToPlaneConstraintData(); hkVector4 up(0.0f, 1.0f, 0.0f); // Create constraint hkVector4 pivotW; pivotW.setAdd4(position2, hkVector4(-0.5f, 0.5f, 0.5f)); plane->setInWorldSpace(moveableBody->getTransform(), fixedBody->getTransform(), pivotW, up); // // Create and add the constraint // { hkpConstraintInstance* constraint = new hkpConstraintInstance( moveableBody, fixedBody, plane ); m_world->addConstraint(constraint); constraint->removeReference(); } plane->removeReference(); } m_world->unlock(); }
BallAndSocketDemo::BallAndSocketDemo(hkDemoEnvironment* env) : hkDefaultPhysicsDemo(env) { // // Setup the camera // { hkVector4 from(0.0f, 0.0f, 10.0f); hkVector4 to (0.0f, 0.0f, 0.0f); hkVector4 up (0.0f, 1.0f, 0.0f); setupDefaultCameras( env, from, to, up ); } // // Create the world // { hkpWorldCinfo info; { info.setupSolverInfo(hkpWorldCinfo::SOLVER_TYPE_4ITERS_MEDIUM); info.setBroadPhaseWorldSize( 100.0f ); info.m_enableDeactivation = false; } m_world = new hkpWorld(info); m_world->lock(); // Register the single agent required (a hkBoxBoxAgent) hkpBoxBoxAgent::registerAgent(m_world->getCollisionDispatcher()); // // Create constraint viewer // m_debugViewerNames.pushBack( hkpConstraintViewer::getName() ); hkpConstraintViewer::m_scale = 1.0f; setupGraphics(); } // // Some vectors to be used for setup // hkVector4 halfSize(0.5f, 0.5f, 0.5f); hkVector4 size; size.setMul4(2.0f, halfSize); hkVector4 position(size(0), -size(1) -0.1f, 0); // // Create Box Shape // hkpBoxShape* boxShape = new hkpBoxShape(halfSize , 0); // // Create fixed rigid body // hkpRigidBody* fixedBody; { hkpRigidBodyCinfo info; { info.m_position . set(0.0f, 0.0f, 0.0f); info.m_shape = boxShape; info.m_motionType = hkpMotion::MOTION_FIXED; } fixedBody = new hkpRigidBody(info); m_world->addEntity(fixedBody); fixedBody->removeReference(); } // // Create movable rigid body // hkpRigidBody* moveableBody; { hkpRigidBodyCinfo info; { hkReal mass = 10.0f; // Compute the box inertia tensor hkpMassProperties massProperties; hkpInertiaTensorComputer::computeBoxVolumeMassProperties(halfSize, mass, massProperties); info.m_position = position; info.m_shape = boxShape; info.m_mass = mass; info.m_inertiaTensor = massProperties.m_inertiaTensor; info.m_centerOfMass = massProperties.m_centerOfMass; info.m_motionType = hkpMotion::MOTION_BOX_INERTIA; } moveableBody = new hkpRigidBody(info); m_world->addEntity(moveableBody); moveableBody->removeReference(); } // Remove reference from shape boxShape->removeReference(); // // Create ball and socket constraint. // { // Set the pivot hkVector4 pivot; pivot.setAdd4(position, halfSize); pivot(0) -= size(0); // Move pivot to corner of cube // Create the constraint hkpBallAndSocketConstraintData* data = new hkpBallAndSocketConstraintData(); data->setInWorldSpace(moveableBody->getTransform(), fixedBody->getTransform(), pivot); hkpConstraintInstance* constraint = new hkpConstraintInstance(moveableBody, fixedBody, data); m_world->addConstraint( constraint); data->removeReference(); constraint->removeReference(); } m_world->unlock(); }
ConstraintKitDemo::ConstraintKitDemo(hkDemoEnvironment* env) : hkDefaultPhysicsDemo(env) { // // Setup the camera // { hkVector4 from(0.0f, 5.0f, 15.0f); hkVector4 to (0.0f, -2.0f, 0.0f); hkVector4 up (0.0f, 1.0f, 0.0f); setupDefaultCameras( env, from, to, up ); } // // Create the world // { hkpWorldCinfo info; info.setupSolverInfo(hkpWorldCinfo::SOLVER_TYPE_4ITERS_MEDIUM); info.setBroadPhaseWorldSize( 100.0f ); info.m_enableDeactivation = false; m_world = new hkpWorld( info ); m_world->lock(); setupGraphics(); } // // Register the box-box collision agent // { hkpAgentRegisterUtil::registerAllAgents( m_world->getCollisionDispatcher() ); } // // Create vectors to be used for setup // hkVector4 halfSize(0.5f, 0.5f, 0.5f); // // Create Box Shape // hkpBoxShape* boxShape; { boxShape = new hkpBoxShape( halfSize , 0 ); } // // Create fixed rigid body // hkpRigidBody* fixedBody; { hkpRigidBodyCinfo info; info.m_position.set(0.0f, 0.0f, 0.0f); info.m_shape = boxShape; info.m_motionType = hkpMotion::MOTION_FIXED; fixedBody = new hkpRigidBody(info); m_world->addEntity(fixedBody); fixedBody->removeReference(); } // // Create movable rigid body // hkpRigidBody* moveableBody; { hkpRigidBodyCinfo info; info.m_position.set(2.0f, 2.0f, 0.0f); info.m_shape = boxShape; // Compute the box inertia tensor hkpMassProperties massProperties; info.m_mass = 10.0f; hkpInertiaTensorComputer::computeBoxVolumeMassProperties(halfSize, info.m_mass, massProperties); info.m_inertiaTensor = massProperties.m_inertiaTensor; info.m_centerOfMass = massProperties.m_centerOfMass; info.m_motionType = hkpMotion::MOTION_BOX_INERTIA; moveableBody = new hkpRigidBody(info); // Add some damping to this body to allow it to come to rest. moveableBody->setAngularDamping(0.1f); moveableBody->setLinearDamping(0.1f); m_world->addEntity(moveableBody); moveableBody->removeReference(); } // Done with shape - remove reference since bodies have ownership boxShape->removeReference(); // // CREATE GENERIC CONSTRAINT // { hkpGenericConstraintData* constraintData = new hkpGenericConstraintData(); hkpConstraintConstructionKit kit; // Must always start with this command (the constraintData works like a program, every // command being executed sequentially). kit.begin(constraintData); { // Set the pivots. These will be the points which are constrained by any linear constraintData // specified later. The fixed body has its pivot in the middle of its base. The movable // body has its pivot on one corner. hkVector4 pivotB(0.0f, -0.5f, 0.0f); m_pivotBIndex = kit.setPivotB(pivotB); hkVector4 pivotA(-0.5f, 0.5f, 0.5f); m_pivotAIndex = kit.setPivotA(pivotA); // Set the axis in World space. This means we can easily update it // to pass through the pivots of the two bodies in our step code. Initially // we calculate this using the current location of the bodies and their pivots // <programlisting id="setInWorldSpace"> const int axisId = 0; hkVector4 axis0; { hkVector4 pivotAW, pivotBW; pivotAW.setTransformedPos(moveableBody->getTransform(), pivotA); pivotBW.setTransformedPos(fixedBody->getTransform(), pivotB); axis0.setSub4(pivotBW, pivotAW); axis0.normalize3(); } // Record the index we get back when we set this axis so we can query the // constraintData later using it as a "parameter ID". m_axisIndex = kit.setLinearDofWorld(axis0, axisId); // Set the limits of this axis ([0, 5]) kit.setLinearLimit(axisId, 0.0f, 5.0f); } // Must always end with this command. kit.end(); hkpConstraintInstance* constraint = new hkpConstraintInstance( moveableBody, fixedBody, constraintData ); m_world->addConstraint(constraint); m_constraintData = constraintData; m_constraint = constraint; } m_world->unlock(); }
void TSLastDetail::_update() { // We're gonna render... make sure we can. bool sceneBegun = GFX->canCurrentlyRender(); if ( !sceneBegun ) GFX->beginScene(); _validateDim(); Vector<GBitmap*> bitmaps; Vector<GBitmap*> normalmaps; // We need to create our own instance to render with. TSShapeInstance *shape = new TSShapeInstance( mShape, true ); // Animate the shape once. shape->animate( mDl ); // So we don't have to change it everywhere. const GFXFormat format = GFXFormatR8G8B8A8; S32 imposterCount = ( ((2*mNumPolarSteps) + 1 ) * mNumEquatorSteps ) + ( mIncludePoles ? 2 : 0 ); // Figure out the optimal texture size. Point2I texSize( smMaxTexSize, smMaxTexSize ); while ( true ) { Point2I halfSize( texSize.x / 2, texSize.y / 2 ); U32 count = ( halfSize.x / mDim ) * ( halfSize.y / mDim ); if ( count < imposterCount ) { // Try half of the height. count = ( texSize.x / mDim ) * ( halfSize.y / mDim ); if ( count >= imposterCount ) texSize.y = halfSize.y; break; } texSize = halfSize; } GBitmap *imposter = NULL; GBitmap *normalmap = NULL; GBitmap destBmp( texSize.x, texSize.y, true, format ); GBitmap destNormal( texSize.x, texSize.y, true, format ); U32 mipLevels = destBmp.getNumMipLevels(); ImposterCapture *imposterCap = new ImposterCapture(); F32 equatorStepSize = M_2PI_F / (F32)mNumEquatorSteps; static const MatrixF topXfm( EulerF( -M_PI_F / 2.0f, 0, 0 ) ); static const MatrixF bottomXfm( EulerF( M_PI_F / 2.0f, 0, 0 ) ); MatrixF angMat; F32 polarStepSize = 0.0f; if ( mNumPolarSteps > 0 ) polarStepSize = -( 0.5f * M_PI_F - mDegToRad( mPolarAngle ) ) / (F32)mNumPolarSteps; PROFILE_START(TSLastDetail_snapshots); S32 currDim = mDim; for ( S32 mip = 0; mip < mipLevels; mip++ ) { if ( currDim < 1 ) currDim = 1; dMemset( destBmp.getWritableBits(mip), 0, destBmp.getWidth(mip) * destBmp.getHeight(mip) * GFXFormat_getByteSize( format ) ); dMemset( destNormal.getWritableBits(mip), 0, destNormal.getWidth(mip) * destNormal.getHeight(mip) * GFXFormat_getByteSize( format ) ); bitmaps.clear(); normalmaps.clear(); F32 rotX = 0.0f; if ( mNumPolarSteps > 0 ) rotX = -( mDegToRad( mPolarAngle ) - 0.5f * M_PI_F ); // We capture the images in a particular order which must // match the order expected by the imposter renderer. imposterCap->begin( shape, mDl, currDim, mRadius, mCenter ); for ( U32 j=0; j < (2 * mNumPolarSteps + 1); j++ ) { F32 rotZ = -M_PI_F / 2.0f; for ( U32 k=0; k < mNumEquatorSteps; k++ ) { angMat.mul( MatrixF( EulerF( rotX, 0, 0 ) ), MatrixF( EulerF( 0, 0, rotZ ) ) ); imposterCap->capture( angMat, &imposter, &normalmap ); bitmaps.push_back( imposter ); normalmaps.push_back( normalmap ); rotZ += equatorStepSize; } rotX += polarStepSize; if ( mIncludePoles ) { imposterCap->capture( topXfm, &imposter, &normalmap ); bitmaps.push_back(imposter); normalmaps.push_back( normalmap ); imposterCap->capture( bottomXfm, &imposter, &normalmap ); bitmaps.push_back( imposter ); normalmaps.push_back( normalmap ); } } imposterCap->end(); Point2I texSize( destBmp.getWidth(mip), destBmp.getHeight(mip) ); // Ok... pack in bitmaps till we run out. for ( S32 y=0; y+currDim <= texSize.y; ) { for ( S32 x=0; x+currDim <= texSize.x; ) { // Copy the next bitmap to the dest texture. GBitmap* bmp = bitmaps.first(); bitmaps.pop_front(); destBmp.copyRect( bmp, RectI( 0, 0, currDim, currDim ), Point2I( x, y ), 0, mip ); delete bmp; // Copy the next normal to the dest texture. GBitmap* normalmap = normalmaps.first(); normalmaps.pop_front(); destNormal.copyRect( normalmap, RectI( 0, 0, currDim, currDim ), Point2I( x, y ), 0, mip ); delete normalmap; // Did we finish? if ( bitmaps.empty() ) break; x += currDim; } // Did we finish? if ( bitmaps.empty() ) break; y += currDim; } // Next mip... currDim /= 2; } PROFILE_END(); // TSLastDetail_snapshots delete imposterCap; delete shape; // Should we dump the images? if ( Con::getBoolVariable( "$TSLastDetail::dumpImposters", false ) ) { String imposterPath = mCachePath + ".imposter.png"; String normalsPath = mCachePath + ".imposter_normals.png"; FileStream stream; if ( stream.open( imposterPath, Torque::FS::File::Write ) ) destBmp.writeBitmap( "png", stream ); stream.close(); if ( stream.open( normalsPath, Torque::FS::File::Write ) ) destNormal.writeBitmap( "png", stream ); stream.close(); } // DEBUG: Some code to force usage of a test image. //GBitmap* tempMap = GBitmap::load( "./forest/data/test1234.png" ); //tempMap->extrudeMipLevels(); //mTexture.set( tempMap, &GFXDefaultStaticDiffuseProfile, false ); //delete tempMap; DDSFile *ddsDest = DDSFile::createDDSFileFromGBitmap( &destBmp ); DDSUtil::squishDDS( ddsDest, GFXFormatDXT3 ); DDSFile *ddsNormals = DDSFile::createDDSFileFromGBitmap( &destNormal ); DDSUtil::squishDDS( ddsNormals, GFXFormatDXT5 ); // Finally save the imposters to disk. FileStream fs; if ( fs.open( _getDiffuseMapPath(), Torque::FS::File::Write ) ) { ddsDest->write( fs ); fs.close(); } if ( fs.open( _getNormalMapPath(), Torque::FS::File::Write ) ) { ddsNormals->write( fs ); fs.close(); } delete ddsDest; delete ddsNormals; // If we did a begin then end it now. if ( !sceneBegun ) GFX->endScene(); }
BallAndSocketChainsDemo::BallAndSocketChainsDemo(hkDemoEnvironment* env) : hkDefaultPhysicsDemo(env) { // Disable warnings: hkError::getInstance().setEnabled(0x2a1db936, false); //'Constraint added between two *colliding* dynamic rigid bodies. Check your collision filter logic...' // // Setup the camera // { hkVector4 from(0.0f, 0.0f, 10.0f); hkVector4 to(0.0f, 0.0f, 0.0f); hkVector4 up(0.0f, 1.0f, 0.0f); // Change the camera for displaying 10 constraints from.set(-9.0f, 0.0f, 15.0f); to.set( 8.0f, -6.0f, 0.0f); setupDefaultCameras( env, from, to, up ); } // // Create the world // { hkpWorldCinfo info; info.setupSolverInfo(hkpWorldCinfo::SOLVER_TYPE_4ITERS_MEDIUM); info.setBroadPhaseWorldSize( 100.0f ); info.m_enableDeactivation = false; info.m_collisionTolerance = 0.01f; m_world = new hkpWorld( info ); m_world->lock(); // Register the single agent required (a hkpBoxBoxAgent) hkpAgentRegisterUtil::registerAllAgents( m_world->getCollisionDispatcher() ); // // Create constraint viewer // m_debugViewerNames.pushBack( hkpConstraintViewer::getName() ); setupGraphics(); } // // Some vectors to be used for setup // hkVector4 halfSize(0.5f, 0.5f, 0.5f); hkVector4 size; size.setMul4(2.0f, halfSize); hkVector4 position(0.0f, 0.0f, 0.0f); hkReal xSpacing = 3; hkReal ySpacing = size(1) + 0.1f; // // Create Box Shape // hkpBoxShape* boxShape; { boxShape = new hkpBoxShape( halfSize , 0 ); } const int numPairs = 10; for (int i = 0; i < numPairs; ++i) { position(1) = 0; // // Create fixed rigid body // hkpRigidBody* rb0; { hkpRigidBodyCinfo info; info.m_position.set(i * xSpacing, 0.0f, 0.0f); info.m_shape = boxShape; info.m_motionType = hkpMotion::MOTION_FIXED; rb0 = new hkpRigidBody(info); m_world->addEntity(rb0); rb0->removeReference(); } for (int k=0; k<10; k++) { hkVector4 positionRb0 = position; position(1) -= ySpacing; // // Create movable rigid body // hkpRigidBody* rb1; { hkpRigidBodyCinfo info; info.m_position = position; info.m_shape = boxShape; // Compute the box inertia tensor hkpMassProperties massProperties; info.m_mass = 10.0f; hkpInertiaTensorComputer::computeBoxVolumeMassProperties(halfSize, info.m_mass, massProperties); info.m_inertiaTensor = massProperties.m_inertiaTensor; info.m_centerOfMass = massProperties.m_centerOfMass; info.m_motionType = hkpMotion::MOTION_BOX_INERTIA; rb1 = new hkpRigidBody(info); m_world->addEntity(rb1); rb1->removeReference(); } // // CREATE BALL AND SOCKET CONSTRAINT // hkpBallAndSocketConstraintData* bs; { // Create the constraint bs = new hkpBallAndSocketConstraintData(); // Set the pivot hkVector4 pivot = positionRb0; bs->setInWorldSpace(rb1->getTransform(), rb0->getTransform(), pivot); hkpConstraintInstance* constraint = new hkpConstraintInstance(rb1, rb0, bs); m_world->addConstraint( constraint); constraint->removeReference(); } bs->removeReference(); rb0 = rb1; } position(0) += xSpacing; } // Remove reference from shape boxShape->removeReference(); // // As our demo does not use any references to the ball socket constraint, we can remove our reference // m_world->unlock(); }
SqueezedBallDemo::SqueezedBallDemo(hkDemoEnvironment* env) : hkDefaultPhysicsDemo(env) { enableDisplayingToiInformation( true ); // Disable warnings hkError::getInstance().setEnabled(0xf0de4356, false); // 'Your m_contactRestingVelocity seems to be too small' hkError::getInstance().setEnabled(0xad45d441, false); // 'SCR generated invalid velocities' hkError::getInstance().setEnabled(0xafe97523, false); //'This utility is intended primarily for Havok demo use. If you wish to step the world asynchronously,...' // // Setup the camera // { hkVector4 from( 6, 0, 50); hkVector4 to ( 6, 0, 0); hkVector4 up ( 0, 1, 0); setupDefaultCameras(env, from, to, up); } // // Setup and create the hkpWorld // { hkpWorldCinfo info; info.setBroadPhaseWorldSize(350.0f); info.m_collisionTolerance = .03f; info.m_gravity.set(0.0f, -100.0f, 0.0f); info.m_enableDeactivation = false; info.m_simulationType = info.SIMULATION_TYPE_CONTINUOUS; m_world = new hkpWorld( info ); m_world->lock(); m_debugViewerNames.pushBack( hkpBroadphaseViewer::getName() ); hkRegisterAlternateShapeTypes( m_world->getCollisionDispatcher() ); hkpPredGskfAgent::registerAgent( m_world->getCollisionDispatcher() ); setupGraphics(); } // Create a row of boxes int numBodies = 0; for (int r = 0; r < 1; r++) { for (int i = 0; i < 1; i++) { //hkVector4 boxSize( 0.5f, 0.5f, 0.5f); // the end pos //hkpConvexShape* shape = new hkpBoxShape( boxSize, 0.05f ); hkpConvexShape* shape = new hkpSphereShape( 0.5f ); hkpRigidBodyCinfo ci; ci.m_motionType = hkpMotion::MOTION_DYNAMIC; ci.m_shape = shape; ci.m_mass = 1.0f; ci.m_angularDamping = 0.0f; ci.m_linearDamping = 0.0f; hkReal d = 1.0f; ci.m_inertiaTensor.setDiagonal( d,d,d ); ci.m_position.set( i * -5.0f, i * -5.0f, 0); ci.m_restitution = 0.9f; ci.m_qualityType = HK_COLLIDABLE_QUALITY_CRITICAL; ci.m_maxLinearVelocity = 1000.0f; hkpRigidBody* body = new hkpRigidBody(ci); char buff[10]; hkString::sprintf(buff, "body%d", numBodies++); body->setName(buff); hkVector4 vel(1500.0f, 500.0f, 0.0f); body->setLinearVelocity(vel); m_world->addEntity( body )->removeReference(); shape->removeReference(); } } hkVector4 halfSize(40.0f, 0.5f, 10.0f); // Create bottom fixed body { hkpConvexShape* shape = new hkpBoxShape(halfSize, 0.0f); hkpRigidBodyCinfo ci; ci.m_motionType = hkpMotion::MOTION_FIXED; ci.m_shape = shape; ci.m_mass = 1.0f; ci.m_angularDamping = 0.0f; ci.m_linearDamping = 0.0f; hkReal d = 1.0f; ci.m_inertiaTensor.setDiagonal( d,d,d ); ci.m_position.set( halfSize(0), -2.0f, 0); ci.m_restitution = 0.9f; hkpRigidBody* body = new hkpRigidBody(ci); char buff[10]; hkString::sprintf(buff, "wall%d", 0); body->setName(buff); //bodies.pushBack( body ); m_world->addEntity( body ); body->removeReference(); shape->removeReference(); } // Create top body { hkpConvexShape* shape = new hkpBoxShape(halfSize, 0.0f); hkpRigidBodyCinfo ci; ci.m_motionType = hkpMotion::MOTION_DYNAMIC; ci.m_shape = shape; ci.m_mass = 1000.0f; ci.m_angularDamping = 0.0f; ci.m_linearDamping = 0.0f; ci.m_inertiaTensor.setDiagonal( 10e7,10e7,10e5 ); ci.m_position.set( halfSize(0), 2.1f, 0); ci.m_restitution = 0.9f; ci.m_rotation = hkQuaternion(hkVector4(0,0,-1), 4.0f * HK_REAL_PI / 180.0f); ci.m_qualityType = HK_COLLIDABLE_QUALITY_MOVING; hkpMassProperties mp; hkpInertiaTensorComputer::computeBoxVolumeMassProperties(halfSize, ci.m_mass, mp); hkpRigidBody* body = new hkpRigidBody(ci); char buff[10]; hkString::sprintf(buff, "wall%d", 1); body->setName(buff); //bodies.pushBack( body ); m_world->addEntity( body ); body->removeReference(); shape->removeReference(); } m_world->unlock(); }
int main() { //speed coefficient float levelSpeed = 200.0f; // Create the main rendering window sf::RenderWindow rendy(sf::VideoMode::GetMode(0), "SFML Graphics", sf::Style::Fullscreen); // Load the sprite images from files sf::Image back; back.LoadFromFile("background.jpg"); sf::Image belt; belt.LoadFromFile("belt.png"); // Create the sprites sf::Sprite background; background.SetImage(back); sf::Sprite belt1; belt1.SetImage(belt); sf::Sprite belt2; belt2.SetImage(belt); sf::Sprite belt3; belt3.SetImage(belt); sf::Sprite belt4; belt4.SetImage(belt); // Sprites configuration background.SetPosition(0.0f, -750.0f); belt1.SetPosition(100.0f, 500.0f); belt2.SetPosition(500.0f, 100.0f); belt3.SetPosition(1300.0f, 700.0f); belt4.SetPosition(1800.0f, 0.0f); // Create a view sf::Vector2f center(720.0f, 450.0f); sf::Vector2f halfSize(720.0f, 450.0f); sf::View view(center, halfSize); rendy.SetView(view); // Start game loop while (rendy.IsOpened()) { float elapsedTime = rendy.GetFrameTime(); // Process events sf::Event event; while (rendy.GetEvent(event)) { // Close window : exit if (event.Type == sf::Event::Closed) rendy.Close(); if ((event.Type == sf::Event::KeyPressed) && (event.Key.Code == sf::Key::Escape)) rendy.Close(); } //Move the view and the background if (rendy.GetInput().IsKeyDown(sf::Key::Left)) { view.Move(-levelSpeed * elapsedTime, 0.0f); background.Move((-levelSpeed / 2) * elapsedTime, 0.0f); } if (rendy.GetInput().IsKeyDown(sf::Key::Right)) { view.Move( levelSpeed * elapsedTime, 0.0f); background.Move((levelSpeed / 2) * elapsedTime, 0.0f); } // Clear the screen (fill it with black color) rendy.Clear(sf::Color(100.0f, 100.0f, 200.0f)); // Draw the sprites rendy.Draw(background); rendy.Draw(belt1); rendy.Draw(belt2); rendy.Draw(belt3); rendy.Draw(belt4); // Display window contents on screen rendy.Display(); } return EXIT_SUCCESS; }
PulleyDemo::PulleyDemo(hkDemoEnvironment* env) : hkDefaultPhysicsDemo(env) { // // Setup the camera // { hkVector4 from(0.0f, -15.0f, 0.0f); hkVector4 to(0.0f, 0.0f, 0.0f); hkVector4 up(0.0f, 0.0f, 1.0f); setupDefaultCameras( env, from, to, up ); } // // Create the world // { hkpWorldCinfo info; info.setupSolverInfo(hkpWorldCinfo::SOLVER_TYPE_4ITERS_MEDIUM); info.setBroadPhaseWorldSize( 3000.0f ); info.m_enableDeactivation = false; info.m_gravity.set(0,0,-10); m_world = new hkpWorld( info ); m_world->lock(); hkpAgentRegisterUtil::registerAllAgents( m_world->getCollisionDispatcher() ); // // Create constraint viewer // m_debugViewerNames.pushBack( hkpConstraintViewer::getName() ); hkpConstraintViewer::m_scale = 1.0f; setupGraphics(); } { // // Create Box Shape // hkVector4 halfSize(0.5f, 0.5f, 0.5f); hkpBoxShape* boxShape = new hkpBoxShape( halfSize , 0 ); // // Create movable rigid bodies // hkpRigidBody* moveableBody0; hkpRigidBody* moveableBody1; { hkpRigidBodyCinfo info; info.m_shape = boxShape; // Compute the box inertia tensor hkpInertiaTensorComputer::setShapeVolumeMassProperties(boxShape, info.m_mass, info); info.m_motionType = hkpMotion::MOTION_BOX_INERTIA; hkVector4 from(1.0f, 1.0f, 1.0f); from.normalize3(); hkVector4 to(0.0f, 0.0f, 1.0f); info.m_rotation.setShortestRotation( from, to ); info.m_rotation.normalize(); info.m_position.set(-3.0f, 0.0f, -2.25f); moveableBody0 = new hkpRigidBody(info); info.m_position.set(3.0f, 0.0f, -1.25f); moveableBody1 = new hkpRigidBody(info); m_world->addEntity(moveableBody0); m_world->addEntity(moveableBody1); boxShape->removeReference(); } // // Create the pulley constraint // hkVector4 worldPivots[2]; worldPivots[0] = hkVector4(-3.0,0,1); worldPivots[1] = hkVector4(3.0,0,1); { hkpPulleyConstraintData* pulley = new hkpPulleyConstraintData(); hkVector4 bodyPivots[2]; bodyPivots[0] = halfSize; bodyPivots[1] = halfSize; hkReal leverageOnBodyB = 3.0f; pulley->setInBodySpace(moveableBody1->getTransform(), moveableBody0->getTransform(), bodyPivots[1], bodyPivots[0], worldPivots[1], worldPivots[0], leverageOnBodyB ); pulley->setRopeLength(9.0f); hkpConstraintInstance* constraint = new hkpConstraintInstance(moveableBody1, moveableBody0, pulley); m_world->addConstraint( constraint); constraint->removeReference(); pulley->removeReference(); } // // Remove references to local rigidBodies, we do not need them here anymore // moveableBody0->removeReference(); moveableBody1->removeReference(); // // Helper bodies preventing the constrained bodies from getting too close to the pulley pivot points. // hkpRigidBodyCinfo info; info.m_motionType = hkpMotion::MOTION_FIXED; info.m_position = worldPivots[0]; info.m_shape = new hkpSphereShape(0.3f); hkpRigidBody* body = new hkpRigidBody(info); m_world->addEntity(body); body->removeReference(); info.m_position = worldPivots[1]; body = new hkpRigidBody(info); m_world->addEntity(body); body->removeReference(); info.m_shape->removeReference(); } m_world->unlock(); }
RagdollDemo::RagdollDemo(hkDemoEnvironment* env) : hkDefaultPhysicsDemo(env) { // // Setup the camera // { hkVector4 from(5.0f, 2.0f, 5.0f); hkVector4 to(0.0f, 0.0f, 0.0f); hkVector4 up(0.0f, 1.0f, 0.0f); setupDefaultCameras( env, from, to, up ); } // // Create the world // { hkpWorldCinfo info; info.setupSolverInfo(hkpWorldCinfo::SOLVER_TYPE_4ITERS_MEDIUM); info.m_enableDeactivation = false; m_world = new hkpWorld( info ); m_world->lock(); // // Create constraint viewer // m_debugViewerNames.pushBack( hkpConstraintViewer::getName() ); hkpConstraintViewer::m_scale = 1.0f; setupGraphics(); // Register the single agent required (a hkpBoxBoxAgent) hkpAgentRegisterUtil::registerAllAgents( m_world->getCollisionDispatcher() ); } hkVector4 pivot(0.0f, 0.0f, 0.0f); hkVector4 halfSize(1.0f, 0.25f, 0.5f); // Create Box Shapes // hkpBoxShape* boxShape; { boxShape = new hkpBoxShape( halfSize , 0 ); } // // Create fixed rigid body // hkpRigidBody* fixedBody; { hkpRigidBodyCinfo info; info.m_position.set(-halfSize(0) - 1.0f, 0.0f, 0.0f); info.m_shape = boxShape; info.m_motionType = hkpMotion::MOTION_FIXED; fixedBody = new hkpRigidBody(info); m_world->addEntity(fixedBody); fixedBody->removeReference(); } // // Create movable rigid body // hkpRigidBody* moveableBody; { hkpRigidBodyCinfo info; info.m_position.set(halfSize(0) + 1.0f, 0.0f, 0.0f); info.m_shape = boxShape; // Compute the box inertia tensor hkpMassProperties massProperties; info.m_mass = 10.0f; hkpInertiaTensorComputer::computeBoxVolumeMassProperties(halfSize, info.m_mass, massProperties); info.m_inertiaTensor = massProperties.m_inertiaTensor; info.m_centerOfMass = massProperties.m_centerOfMass; info.m_motionType = hkpMotion::MOTION_BOX_INERTIA; moveableBody = new hkpRigidBody(info); m_world->addEntity(moveableBody); moveableBody->removeReference(); } // // Cleanup shape references // boxShape->removeReference(); boxShape = HK_NULL; // // CREATE RAGDOLL CONSTRAINT // { hkReal planeMin = HK_REAL_PI * -0.2f; hkReal planeMax = HK_REAL_PI * 0.1f; hkReal twistMin = HK_REAL_PI * -0.1f; hkReal twistMax = HK_REAL_PI * 0.4f; hkReal coneMin = HK_REAL_PI * 0.3f; hkpRagdollConstraintData* rdc = new hkpRagdollConstraintData(); rdc->setConeAngularLimit(coneMin); rdc->setPlaneMinAngularLimit(planeMin); rdc->setPlaneMaxAngularLimit(planeMax); rdc->setTwistMinAngularLimit(twistMin); rdc->setTwistMaxAngularLimit(twistMax); hkVector4 twistAxis(1.0f, 0.0f, 0.0f); hkVector4 planeAxis(0.0f, 1.0f, 0.0f); pivot.set(0.0f, 0.0f, 0.0f); rdc->setInWorldSpace(moveableBody->getTransform(), fixedBody->getTransform(), pivot, twistAxis, planeAxis); // // Create and add the constraint // { hkpConstraintInstance* constraint = new hkpConstraintInstance(moveableBody, fixedBody, rdc ); m_world->addConstraint(constraint); constraint->removeReference(); } rdc->removeReference(); } m_world->unlock(); }
HingeDemo::HingeDemo(hkDemoEnvironment* env) : hkDefaultPhysicsDemo(env) { // // Setup the camera // { hkVector4 from(0.0f, 0.0f, 10.0f); hkVector4 to(0.0f, 0.0f, 0.0f); hkVector4 up(0.0f, 1.0f, 0.0f); setupDefaultCameras( env, from, to, up ); } // // Create the world // { hkpWorldCinfo info; info.setupSolverInfo(hkpWorldCinfo::SOLVER_TYPE_4ITERS_MEDIUM); info.setBroadPhaseWorldSize( 100.0f ); m_world = new hkpWorld( info ); m_world->lock(); // Register the single agent required (a hkpBoxBoxAgent) hkpAgentRegisterUtil::registerAllAgents( m_world->getCollisionDispatcher() ); // // Create constraint viewer // m_debugViewerNames.pushBack( hkpConstraintViewer::getName() ); hkpConstraintViewer::m_scale = 1.0f; setupGraphics(); } // // Create vectors to be used for setup // hkVector4 halfSize(0.5f, 0.5f, 0.5f); hkVector4 size; size.setMul4(2.0f, halfSize); hkVector4 position(size(0), -size(1) -0.1f, 0.0f); // // Create Box Shape // hkReal radius; hkpBoxShape* boxShape; { boxShape = new hkpBoxShape( halfSize , 0 ); radius = boxShape->getRadius(); } // // Create fixed rigid body // hkpRigidBody* fixedBody; { hkpRigidBodyCinfo info; info.m_position.set(0.0f, 0.0f, 0.0f); info.m_shape = boxShape; info.m_motionType = hkpMotion::MOTION_FIXED; fixedBody = new hkpRigidBody(info); m_world->addEntity(fixedBody); fixedBody->removeReference(); } // // Create movable rigid body // hkpRigidBody* moveableBody; { hkpRigidBodyCinfo info; info.m_position = position; info.m_shape = boxShape; // Compute the box inertia tensor hkpMassProperties massProperties; info.m_mass = 10.0f; hkpInertiaTensorComputer::computeBoxVolumeMassProperties(halfSize, info.m_mass, massProperties); info.m_inertiaTensor = massProperties.m_inertiaTensor; info.m_centerOfMass = massProperties.m_centerOfMass; info.m_motionType = hkpMotion::MOTION_BOX_INERTIA; moveableBody = new hkpRigidBody(info); m_world->addEntity(moveableBody); moveableBody->removeReference(); } // Remove reference from box shape since rigid bodies now have ownership boxShape->removeReference(); // // CREATE HINGE CONSTRAINT // hkpHingeConstraintData* hc = new hkpHingeConstraintData(); { // Set the pivot hkVector4 pivot; pivot.setAdd4(position, halfSize); // Move pivot to center of side on cube pivot(0) -= size(0); pivot(2) -= halfSize(2); hkVector4 axis(0.0f, 0.0f, 1.0f); // Create constraint hc->setInWorldSpace(moveableBody->getTransform(), fixedBody->getTransform(), pivot, axis); hkpConstraintInstance* constraint = new hkpConstraintInstance(moveableBody, fixedBody, hc); m_world->addConstraint( constraint); constraint->removeReference(); hc->removeReference(); } m_world->unlock(); }
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Vector2 Rectangle::centerPoint() const { return position_ + halfSize(); }
PoweredRagdollDemo::PoweredRagdollDemo(hkDemoEnvironment* env) : hkDefaultPhysicsDemo(env) { m_time = 0.0f; // // Setup the camera // { hkVector4 from(5.0f, 2.0f, 5.0f); hkVector4 to(0.0f, 0.0f, 0.0f); hkVector4 up(0.0f, 1.0f, 0.0f); setupDefaultCameras( env, from, to, up ); } // // Create the world // { hkpWorldCinfo info; info.setupSolverInfo(hkpWorldCinfo::SOLVER_TYPE_8ITERS_MEDIUM); info.m_enableDeactivation = false; m_world = new hkpWorld( info ); m_world->lock(); // // Create constraint viewer // m_debugViewerNames.pushBack( hkpConstraintViewer::getName() ); hkpConstraintViewer::m_scale = 1.0f; setupGraphics(); // Register the single agent required (a hkpBoxBoxAgent) hkpAgentRegisterUtil::registerAllAgents( m_world->getCollisionDispatcher() ); } // // Create vectors to be used for setup // hkVector4 pivot(0.0f, 0.0f, 0.0f); hkVector4 halfSize(1.0f, 0.25f, 0.5f); // // Create Box Shape // hkpBoxShape* boxShape; { boxShape = new hkpBoxShape( halfSize , 0 ); } // // Create fixed rigid body // hkpRigidBody* fixedBody; { hkpRigidBodyCinfo info; info.m_position.set(-halfSize(0) - 1.0f, 0.0f, 0.0f); info.m_shape = boxShape; info.m_motionType = hkpMotion::MOTION_FIXED; fixedBody = new hkpRigidBody(info); m_world->addEntity(fixedBody); fixedBody->removeReference(); } // // Create movable rigid body // hkpRigidBody* moveableBody; { hkpRigidBodyCinfo info; info.m_position.set(halfSize(0) + 1.0f, 0.0f, 0.0f); info.m_shape = boxShape; // Compute the box inertia tensor hkpMassProperties massProperties; info.m_mass = 10.0f; hkpInertiaTensorComputer::computeBoxVolumeMassProperties(halfSize, info.m_mass, massProperties); info.m_inertiaTensor = massProperties.m_inertiaTensor; info.m_centerOfMass = massProperties.m_centerOfMass; info.m_motionType = hkpMotion::MOTION_BOX_INERTIA; moveableBody = new hkpRigidBody(info); m_world->addEntity(moveableBody); moveableBody->removeReference(); } // // Cleanup shape references // boxShape->removeReference(); boxShape = HK_NULL; // // CREATE POWERED RAGDOLL CONSTRAINT // { hkReal planeMin = HK_REAL_PI * -0.2f; hkReal planeMax = HK_REAL_PI * 0.1f; hkReal twistMin = HK_REAL_PI * -0.1f; hkReal twistMax = HK_REAL_PI * 0.4f; hkReal coneMin = HK_REAL_PI * 0.3f; hkpRagdollConstraintData* rdc = new hkpRagdollConstraintData(); rdc->setConeAngularLimit(coneMin); rdc->setPlaneMinAngularLimit(planeMin); rdc->setPlaneMaxAngularLimit(planeMax); rdc->setTwistMinAngularLimit(twistMin); rdc->setTwistMaxAngularLimit(twistMax); hkVector4 twistAxis(1.0f, 0.0f, 0.0f); hkVector4 planeAxis(0.0f, 1.0f, 0.0f); pivot.set(0.0f, 0.0f, 0.0f); rdc->setInWorldSpace(moveableBody->getTransform(), fixedBody->getTransform(), pivot, twistAxis, planeAxis); // // Create and add the constraint // hkpConstraintInstance* constraint = new hkpConstraintInstance(moveableBody, fixedBody, rdc ); m_world->addConstraint(constraint); hkpPositionConstraintMotor* motor = new hkpPositionConstraintMotor( 0 ); motor->m_tau = 0.6f; motor->m_maxForce = 1000.0f; motor->m_constantRecoveryVelocity = 0.1f; rdc->setTwistMotor( motor ); rdc->setConeMotor( motor ); rdc->setPlaneMotor( motor ); rdc->setMotorsActive(constraint, true); motor->removeReference(); hkRotation bRa; bRa.setTranspose( fixedBody->getTransform().getRotation() ); bRa.mul( moveableBody->getTransform().getRotation() ); rdc->setTargetRelativeOrientationOfBodies( bRa ); m_constraintData = rdc; constraint->removeReference(); } // // So that we can actually see the motor in action, rotate the constrained body slightly. // When simulation starts it will then by driven to the target position (frame). // { hkQuaternion rot; hkVector4 axis( 1.0f, 0.0f, 0.0f ); axis.normalize3(); rot.setAxisAngle( axis, 0.4f ); moveableBody->setRotation( rot ); } m_world->unlock(); }