void MyGame::Update(float elapsedTime) { static float spawnTimer = 0; spawnTimer += elapsedTime; if(spawnTimer >= 0.15f) { Cube* c = new Cube(Vector3f(1.f, 1.f, 1.f)); c->SetTexture(Texture::Get("!debug.png")); GetScene().GetRoot()->AddChild(c); RigidBody* rb = c->AddToPhysic(1, Vector3f(0, 25, 7.5)); rb->ApplyForce(Vector3f(0, -10.f, 0)); spawnTimer = 0; m_cubeCount++; } std::ostringstream fps; fps << "FPS: " << GetFps(); m_txtFps->SetText(fps.str()); std::ostringstream CubeCount; CubeCount << "Cube count: " << m_cubeCount; m_txtCubeCount->SetText(CubeCount.str()); }
static duk_ret_t RigidBody_ApplyForce_float3_float3(duk_context* ctx) { int numArgs = duk_get_top(ctx); RigidBody* thisObj = GetThisWeakObject<RigidBody>(ctx); float3& force = *GetCheckedValueObject<float3>(ctx, 0, float3_ID); float3 position = numArgs > 1 ? *GetCheckedValueObject<float3>(ctx, 1, float3_ID) : float3::zero; thisObj->ApplyForce(force, position); return 0; }
void SnowBall::FixedUpdate(float timeStep) { // Apply damping when rolling on the ground, or near disappearing RigidBody* body = node_->GetComponent<RigidBody>(); if ((onGround) || (duration < snowballGroundHitDuration)) { Vector3 vel = body->GetLinearVelocity(); body->ApplyForce(Vector3(-snowballDampingForce * vel.x_, 0, -snowballDampingForce * vel.z_)); } // Disappear when duration expired if (duration >= 0) { duration -= timeStep; if (duration <= 0) { SpawnParticleEffect(node_, node_->GetPosition(), "Particle/SnowExplosion.xml", 1); node_->Remove(); } } }
void CollisionHandler::CollisionItem::ApplyCollision( RigidBody& i_oRigidBody1, RigidBody& i_oRigidBody2, double i_rMu, double /*i_rFrameTime*/ ) { // i_oRigidBody2 collides with i_oRigidBody1. // Total elastic force: double rTotalElasticConst; { double fK1 = i_oRigidBody1.ElasticConstant(); double fK2 = i_oRigidBody2.ElasticConstant(); if ( *(int*)&fK1 != 0 && *(int*)&fK2 != 0 ) // the two bodies' behavior is the same as two spring in series { rTotalElasticConst = fK1 * fK2 / (fK1 + fK2); } else if ( *(int*)&fK1 == 0 ) // only the second body produce elastic force { rTotalElasticConst = fK2; } else // only the first body produce elastic force or neither { rTotalElasticConst = fK1; } } PCEVector3D vElasticForce = m_vImpactNormal * ( rTotalElasticConst * m_rDeformation ); // Fluid Friction: PCEVector3D vTotalFluidFriction = ( i_oRigidBody1.Velocity() * i_oRigidBody1.Muv() ) + ( i_oRigidBody2.Velocity() * i_oRigidBody2.Muv() ); PCEVector3D vNormalFluidFriction = DotProduct(vTotalFluidFriction, m_vImpactNormal) * m_vImpactNormal; PCEVector3D vTangFluidFriction = vTotalFluidFriction - vNormalFluidFriction; // Force Normal Component: elastic force + fluid friction normal double rNormalForceModule = DotProduct( vElasticForce + vNormalFluidFriction, m_vImpactNormal ); if(rNormalForceModule < 0) { rNormalForceModule = 0; } PCEVector3D vNormForce = m_vImpactNormal * rNormalForceModule; // Force Tangent Component: dry friction and fluid friction tangent PCEVector3D vTangVelocity = m_vImpactVelocity - (DotProduct(m_vImpactVelocity,m_vImpactNormal) * m_vImpactNormal); double rFrictionForceModule = rNormalForceModule * i_rMu; PCEVector3D vTangForce = ( vTangVelocity * rFrictionForceModule ) + vTangFluidFriction; /* double modVtang = vTangVelocity.Module(); if(modVtang > 9.81f * i_rFrameTime) { vTangForce /= modVtang; } else { vTangForce /= ( 9.8f * i_rFrameTime ); } */ vNormForce += vTangForce; // now vNormForce is the total force i_oRigidBody1.ApplyForce(vNormForce, m_vImpactPoint); vNormForce[0] = -vNormForce[0]; vNormForce[1] = -vNormForce[1]; vNormForce[2] = -vNormForce[2]; i_oRigidBody2.ApplyForce(vNormForce, m_vImpactPoint); }