hkpRigidBody* PhysicsWrapper::SetupSphericalRigidBody(float radius, float mass, D3DXVECTOR3 position, D3DXVECTOR3 velocity, bool isStatic, ProjectStructs::PROJECTILE *projectile){ hkVector4 relPos( 0.0f,radius, 0.0f); hkpRigidBodyCinfo info; hkpMassProperties massProperties; hkpInertiaTensorComputer::computeSphereVolumeMassProperties(radius, mass, massProperties); info.m_mass = massProperties.m_mass; info.m_centerOfMass = massProperties.m_centerOfMass; info.m_friction = 0.75f; info.m_inertiaTensor = massProperties.m_inertiaTensor; info.m_shape = new hkpSphereShape( radius ); relPos.add4(hkVector4(position.x, position.y, position.z)); info.m_position = relPos; info.m_linearVelocity = hkVector4(velocity.x, velocity.y, velocity.z); if(isStatic) { info.m_motionType = hkpMotion::MOTION_FIXED; info.m_qualityType = HK_COLLIDABLE_QUALITY_FIXED; } else{ info.m_motionType = hkpMotion::MOTION_SPHERE_INERTIA; info.m_qualityType = HK_COLLIDABLE_QUALITY_MOVING; } hkpRigidBody* rb = new hkpRigidBody( info ); rb->addProperty(HK_OBJECT_IS_PROJECTILE, hkpPropertyValue(projectile)); physicsWorld->addEntity( rb ); rb->removeReference(); info.m_shape->removeReference(); return rb; }
void plApplyImpulse(plRigidBodyHandle object, const plVector3 impulse, const plVector3 relativePos) { btRigidBody* body = reinterpret_cast<btRigidBody*>(object); btAssert(body); btVector3 implse(impulse[0], impulse[1], impulse[2]); btVector3 relPos(relativePos[0], relativePos[1], relativePos[2]); body->applyImpulse(implse, relPos); }
void ofApp::newCircle() { ofVec2f relPos(ofRandom(radius), ofRandom(radius)); unsigned f = ofRandom(fieldPosPtrs.size()); ofVec2f *fPosPtr = fieldPosPtrs.at(f); ofVec2f pos = (*fPosPtr + relPos) - (radius/2); shared_ptr <ofxBox2dCircle> c = make_shared<ofxBox2dCircle>(); circles.push_back(c); circles.back().get()->setFixedRotation(true); circles.back().get()->setPhysics(3, 0.53, 0.1); circles.back().get()->setup(world.world.getWorld(), pos.x, pos.y, circRadius); circles.back().get()->enableGravity(true); circles.back().get()->setMassFromShape = true; }
char* CBroadcastData::receiveData(int receiverID,float simulationTime,int dataHeader,std::string& dataName,int antennaHandle,int& dataLength,int& senderID,int& dataHeaderR,std::string& dataNameR,bool removeMessageForThisReceiver) { C7Vector antennaConf1; antennaConf1.setIdentity(); if (_antennaHandle!=sim_handle_default) { C3DObject* it=App::ct->objCont->getObject(_antennaHandle); if (it==NULL) return(NULL); // the emission antenna was destroyed! antennaConf1=it->getCumulativeTransformationPart1(); } C3Vector antennaPos2; antennaPos2.clear(); if (antennaHandle!=sim_handle_default) { C3DObject* it=App::ct->objCont->getObject(antennaHandle); if (it==NULL) return(NULL); // that shouldn't happen! antennaPos2=it->getCumulativeTransformationPart1().X; } if ((_emitterID==receiverID)&&(_emitterID!=0)) // second part since 25/9/2012: from c/c++, emitter/receiver ID is always 0 return(NULL); // the emitter cannot receive its own message! if (simulationTime>_timeOutSimulationTime) return(NULL); // data timed out! if ( (dataHeader!=-1)&&(dataHeader!=_dataHeader) ) return(NULL); // wrong data header! if ( (dataName.length()!=0)&&(dataName.compare(_dataName)!=0) ) return(NULL); // wrong data name! if (_receiverID!=sim_handle_all) { // message not for everyone if (_receiverID==sim_handle_tree) { // we have to check if receiverID has a parent _emitterID: CLuaScriptObject* rec=App::ct->luaScriptContainer->getScriptFromID(receiverID); CLuaScriptObject* em=App::ct->luaScriptContainer->getScriptFromID(_emitterID); if ( (rec==NULL)||(em==NULL) ) return(NULL); if (em->getScriptType()!=sim_scripttype_mainscript) { if (rec->getScriptType()==sim_scripttype_mainscript) return(NULL); C3DObject* recObj=App::ct->objCont->getObject(rec->getObjectIDThatScriptIsAttachedTo()); C3DObject* emObj=App::ct->objCont->getObject(em->getObjectIDThatScriptIsAttachedTo()); bool found=false; while (recObj!=NULL) { if (recObj==emObj) { found=true; break; } recObj=recObj->getParent(); } if (!found) return(NULL); } } if (_receiverID==sim_handle_chain) { // we have to check if _emitterID has a parent receiverID: CLuaScriptObject* rec=App::ct->luaScriptContainer->getScriptFromID(receiverID); CLuaScriptObject* em=App::ct->luaScriptContainer->getScriptFromID(_emitterID); if ( (rec==NULL)||(em==NULL) ) return(NULL); if (rec->getScriptType()!=sim_scripttype_mainscript) { if (em->getScriptType()==sim_scripttype_mainscript) return(NULL); C3DObject* recObj=App::ct->objCont->getObject(rec->getObjectIDThatScriptIsAttachedTo()); C3DObject* emObj=App::ct->objCont->getObject(em->getObjectIDThatScriptIsAttachedTo()); bool found=false; while (emObj!=NULL) { if (recObj==emObj) { found=true; break; } emObj=emObj->getParent(); } if (!found) return(NULL); } } if (_receiverID>=0) { if (_receiverID!=receiverID) return(NULL); } } if ((antennaPos2-antennaConf1.X).getLength()>_actionRadius) return(NULL); // outside of action radius! if (_emissionAngle1<piValue*0.99f) { // Check if inside the vertical "hearing" area: C3Vector relPos(antennaConf1.getInverse()*antennaPos2); float h=fabs(relPos(2)); relPos(2)=0.0f; float l=relPos.getLength(); if (l==0.0f) return(NULL); // just outside of the vertical active area (border condition) float a=fabs(atan2(h,l)); if (a>=_emissionAngle1*0.5f) return(NULL); // just outside of the vertical active area (border condition) } if (_emissionAngle2<piValTimes2*0.99f) { // Check if inside the horizontal "hearing" area: C3Vector relPos(antennaConf1.getInverse()*antennaPos2); if (relPos(0)==0.0f) return(NULL); // just outside of the horizontal active area (border condition) float a=fabs(atan2(relPos(1),relPos(0))); if (a>=_emissionAngle2*0.5f) return(NULL); // just outside of the horizontal active area (border condition) } if (receiverPresent(receiverID)) return(NULL); // data was already read by that script! // We can receive the data, finally! if (removeMessageForThisReceiver) _receivedReceivers.push_back(receiverID); // remember that this receiverID already read the message! dataLength=_dataLength; senderID=_emitterID; dataHeaderR=_dataHeader; dataNameR=_dataName; return(_data); }
void setupPhysics(hkpWorld* physicsWorld) { // // Create the ground box // { hkVector4 groundRadii( 70.0f, 2.0f, 140.0f ); hkpConvexShape* shape = new hkpBoxShape( groundRadii , 0 ); hkpRigidBodyCinfo ci; ci.m_shape = shape; ci.m_motionType = hkpMotion::MOTION_FIXED; ci.m_position = hkVector4( 0.0f, -2.0f, 0.0f ); ci.m_qualityType = HK_COLLIDABLE_QUALITY_FIXED; physicsWorld->addEntity( new hkpRigidBody( ci ) )->removeReference(); shape->removeReference(); } hkVector4 groundPos( 0.0f, 0.0f, 0.0f ); hkVector4 posy = groundPos; // // Create the walls // int wallHeight = 8; int wallWidth = 8; int numWalls = 6; hkVector4 boxSize( 1.0f, 0.5f, 0.5f); hkpBoxShape* box = new hkpBoxShape( boxSize , 0 ); box->setRadius( 0.0f ); hkReal deltaZ = 25.0f; posy(2) = -deltaZ * numWalls * 0.5f; for ( int y = 0; y < numWalls; y ++ ) // first wall { createBrickWall( physicsWorld, wallHeight, wallWidth, posy, 0.2f, box, boxSize ); posy(2) += deltaZ; } box->removeReference(); // // Create a ball moving towards the walls // const hkReal radius = 1.5f; const hkReal sphereMass = 150.0f; hkVector4 relPos( 0.0f,radius + 0.0f, 50.0f ); hkpRigidBodyCinfo info; hkpMassProperties massProperties; hkpInertiaTensorComputer::computeSphereVolumeMassProperties(radius, sphereMass, massProperties); info.m_mass = massProperties.m_mass; info.m_centerOfMass = massProperties.m_centerOfMass; info.m_inertiaTensor = massProperties.m_inertiaTensor; info.m_shape = new hkpSphereShape( radius ); info.m_position.setAdd4(posy, relPos ); info.m_motionType = hkpMotion::MOTION_BOX_INERTIA; info.m_qualityType = HK_COLLIDABLE_QUALITY_BULLET; hkpRigidBody* sphereRigidBody = new hkpRigidBody( info ); g_ball = sphereRigidBody; physicsWorld->addEntity( sphereRigidBody ); sphereRigidBody->removeReference(); info.m_shape->removeReference(); hkVector4 vel( 0.0f,4.9f, -100.0f ); sphereRigidBody->setLinearVelocity( vel ); }
void clientMouseFunc(int button, int state, int x, int y) { //printf("button %i, state %i, x=%i,y=%i\n",button,state,x,y); //button 0, state 0 means left mouse down SimdVector3 rayTo = GetRayTo(x,y); switch (button) { case 2: { if (state==0) { shootBox(rayTo); } break; }; case 1: { if (state==0) { //apply an impulse if (physicsEnvironmentPtr) { float hit[3]; float normal[3]; PHY_IPhysicsController* hitObj = physicsEnvironmentPtr->rayTest(0,eye[0],eye[1],eye[2],rayTo.getX(),rayTo.getY(),rayTo.getZ(),hit[0],hit[1],hit[2],normal[0],normal[1],normal[2]); if (hitObj) { CcdPhysicsController* physCtrl = static_cast<CcdPhysicsController*>(hitObj); RigidBody* body = physCtrl->GetRigidBody(); if (body) { body->SetActivationState(ACTIVE_TAG); SimdVector3 impulse = rayTo; impulse.normalize(); float impulseStrength = 10.f; impulse *= impulseStrength; SimdVector3 relPos( hit[0] - body->getCenterOfMassPosition().getX(), hit[1] - body->getCenterOfMassPosition().getY(), hit[2] - body->getCenterOfMassPosition().getZ()); body->applyImpulse(impulse,relPos); } } } } else { } break; } case 0: { if (state==0) { //add a point to point constraint for picking if (physicsEnvironmentPtr) { float hit[3]; float normal[3]; PHY_IPhysicsController* hitObj = physicsEnvironmentPtr->rayTest(0,eye[0],eye[1],eye[2],rayTo.getX(),rayTo.getY(),rayTo.getZ(),hit[0],hit[1],hit[2],normal[0],normal[1],normal[2]); if (hitObj) { CcdPhysicsController* physCtrl = static_cast<CcdPhysicsController*>(hitObj); RigidBody* body = physCtrl->GetRigidBody(); if (body) { pickedBody = body; pickedBody->SetActivationState(DISABLE_DEACTIVATION); SimdVector3 pickPos(hit[0],hit[1],hit[2]); SimdVector3 localPivot = body->getCenterOfMassTransform().inverse() * pickPos; gPickingConstraintId = physicsEnvironmentPtr->createConstraint(physCtrl,0,PHY_POINT2POINT_CONSTRAINT, localPivot.getX(), localPivot.getY(), localPivot.getZ(), 0,0,0); //printf("created constraint %i",gPickingConstraintId); //save mouse position for dragging gOldPickingPos = rayTo; SimdVector3 eyePos(eye[0],eye[1],eye[2]); gOldPickingDist = (pickPos-eyePos).length(); Point2PointConstraint* p2p = static_cast<Point2PointConstraint*>(physicsEnvironmentPtr->getConstraintById(gPickingConstraintId)); if (p2p) { //very weak constraint for picking p2p->m_setting.m_tau = 0.1f; } } } } } else { if (gPickingConstraintId && physicsEnvironmentPtr) { physicsEnvironmentPtr->removeConstraint(gPickingConstraintId); //printf("removed constraint %i",gPickingConstraintId); gPickingConstraintId = 0; pickedBody->ForceActivationState(ACTIVE_TAG); pickedBody->m_deactivationTime = 0.f; pickedBody = 0; } } break; } default: { } } }
int32 CFont::m_DrawPhrase (FONTPHRASE *phrase, float4 relpos) { int32 i, l; float4 start, end; float4 animStartScale, animEndScale; // range del keyframer di scaling float4 animStartRot, animEndRot; // range del keyframer di rotazione uint32 alpha, r, color; float4 animScaleX, animScaleY, animPos; float4 end_animScaleX, end_animScaleY; AD_Vect3D scale3D; AD_Matrix rot, end_rot; mat_identity(&rot); mat_identity(&end_rot); l=strlen(phrase->text); end_animScaleX=phrase->scaleX; end_animScaleY=phrase->scaleY; if (p_ScaleTrack.p_NumKeys>0) { p_ScaleTrack.m_GetTimeRange(&animStartScale, &animEndScale); p_ScaleTrack.m_GetData(animEndScale, &scale3D); end_animScaleX*=scale3D.x; end_animScaleY*=scale3D.y; } animScaleX=phrase->scaleX; animScaleY=phrase->scaleY; if (p_RotationTrack.p_NumKeys>0) { p_RotationTrack.m_GetTimeRange(&animStartRot, &animEndRot); p_RotationTrack.m_GetData(animEndRot, &end_rot); } // in questo intervallo la frase è tutta visibile completa // di transizioni (colore, alpha, animazioni) terminate if ((relpos>=phrase->phraseFixedStart) && (relpos<phrase->phraseFixedEnd)) m_DrawAnimatedText(phrase->x, phrase->y, end_animScaleX, end_animScaleX, end_animScaleX, end_animScaleY, phrase->color, phrase->text, 0, l-1, &end_rot); else if (relpos<phrase->phraseFixedStart) { for (i=0; i<l; i++) { animScaleX=phrase->scaleX; animScaleY=phrase->scaleY; // calcolo l'inizio e fine della lettera i-esima start=phrase->relstart+i*phrase->phraseDelayPerChar; if (relpos<start) continue; end=start+phrase->phraseTimePerChar; // scrivo la lettera a colore e trasformazioni piene if (relpos>=end) { m_DrawAnimatedText(phrase->x, phrase->y, end_animScaleX, end_animScaleY, end_animScaleX, end_animScaleY, phrase->color, phrase->text, i, i, &end_rot); } else { r=(uint32)(255.0f*relPos(start, end, relpos)); alpha=(phrase->color >> 24); alpha=((alpha*r)>>8) & 255; color=(phrase->color & 0x00FFFFFF) | (alpha << 24); // estrazione dell'animazione if (p_ScaleTrack.p_NumKeys>0) { animPos=map_intervals(start, end, animStartScale, animEndScale, relpos); p_ScaleTrack.m_GetData(animPos, &scale3D); animScaleX*=scale3D.x; animScaleY*=scale3D.y; } if (p_RotationTrack.p_NumKeys>0) { animPos=map_intervals(start, end, animStartRot, animEndRot, relpos); p_RotationTrack.m_GetData(animPos, &rot); } m_DrawAnimatedText(phrase->x, phrase->y, animScaleX, animScaleY, end_animScaleX, end_animScaleY, color, phrase->text, i, i, &rot); } } } else // fase di scomparsa della frase if (relpos>phrase->phraseFixedEnd)
BrickWallDemo::BrickWallDemo(hkDemoEnvironment* env) : hkDefaultPhysicsDemo(env) { // Disable warnings: hkError::getInstance().setEnabled(0xaf55adde, false); //'No runtime block of size 637136 currently available. Allocating new block from unmanaged memory.' hkDefaultPhysicsDemo::enableDisplayingToiInformation( true ); m_frameToSimulationFrequencyRatio = 1.0f; m_timestep = 1.0f / 60.0f; const BrickWallVariant& variant = g_variants[m_variantId]; int wallHeight = variant.m_height; int wallWidth = variant.m_width; int numWalls = variant.m_numWalls; hkpWorldCinfo::SimulationType simulationType; simulationType = hkpWorldCinfo::SIMULATION_TYPE_CONTINUOUS; // // Setup the camera so that we can see the ball hit the (first) wall. // { hkVector4 from(40.0f, 20.0f, 40.0f); hkVector4 to ( 0.0f, 10.0f, 0.0f); hkVector4 up ( 0.0f, 1.0f, 0.0f); setupDefaultCameras( env, from, to, up ); } { hkpWorldCinfo info; info.setBroadPhaseWorldSize( 5000.0f ); info.setupSolverInfo(hkpWorldCinfo::SOLVER_TYPE_4ITERS_HARD); info.m_collisionTolerance = 0.01f; info.m_gravity = hkVector4( 0.0f,-9.8f,0.0f); info.m_simulationType = simulationType; info.m_broadPhaseBorderBehaviour = info.BROADPHASE_BORDER_FIX_ENTITY; //info.m_enableDeactivation = false; //info.m_enableSimulationIslands = false; info.m_enableDeactivation = false; m_world = new hkpWorld( info ); m_world->lock(); } { // Register ALL agents (though some may not be necessary) hkpAgentRegisterUtil::registerAllAgents(m_world->getCollisionDispatcher()); setupGraphics(); // enable instancing (if present on platform) hkpShapeDisplayViewer* shapeViewer = (hkpShapeDisplayViewer*)getLocalViewerByName( hkpShapeDisplayViewer::getName() ); if (shapeViewer) { shapeViewer->setInstancingEnabled( true ); } } // // Create the ground box // { hkVector4 groundRadii( 70.0f, 2.0f, 70.0f ); hkpConvexShape* shape = new hkpBoxShape( groundRadii , 0 ); hkpRigidBodyCinfo ci; ci.m_shape = shape; ci.m_motionType = hkpMotion::MOTION_FIXED; ci.m_position = hkVector4( 0.0f, -2.0f, 0.0f ); ci.m_qualityType = HK_COLLIDABLE_QUALITY_FIXED; m_world->addEntity( new hkpRigidBody( ci ) )->removeReference(); shape->removeReference(); } hkVector4 groundPos( 0.0f, 0.0f, 0.0f ); hkVector4 posy = groundPos; // // Create the walls // if(1) { hkVector4 boxSize( 1.0f, 0.5f, 0.5f); hkpBoxShape* box = new hkpBoxShape( boxSize , 0 ); box->setRadius( 0.0f ); hkReal deltaZ = 25.0f; posy(2) = -deltaZ * numWalls * 0.5f; for ( int y = 0; y < numWalls; y ++ ) // first wall { createBrickWall( m_world, wallHeight, wallWidth, posy, 0.2f, box, boxSize ); posy(2) += deltaZ; } box->removeReference(); } // // Create the ball // if (1) { const hkReal radius = 1.5f; const hkReal sphereMass = 150.0f; hkVector4 relPos( 0.0f,radius + 0.0f, 20.0f ); hkpRigidBodyCinfo info; hkpMassProperties massProperties; hkpInertiaTensorComputer::computeSphereVolumeMassProperties(radius, sphereMass, massProperties); info.m_mass = massProperties.m_mass; info.m_centerOfMass = massProperties.m_centerOfMass; info.m_inertiaTensor = massProperties.m_inertiaTensor; info.m_shape = new hkpSphereShape( radius ); info.m_position.setAdd4(posy, relPos ); info.m_motionType = hkpMotion::MOTION_BOX_INERTIA; if ( variant.m_usePredictive) { info.m_qualityType = HK_COLLIDABLE_QUALITY_BULLET; } else { info.m_qualityType = HK_COLLIDABLE_QUALITY_DEBRIS; } hkpRigidBody* sphereRigidBody = new hkpRigidBody( info ); m_world->addEntity( sphereRigidBody ); sphereRigidBody->removeReference(); info.m_shape->removeReference(); hkVector4 vel( 0.0f,4.9f, -200.0f ); sphereRigidBody->setLinearVelocity( vel ); } m_world->unlock(); }