static duk_ret_t RigidBody_GetLinearVelocity(duk_context* ctx) { RigidBody* thisObj = GetThisWeakObject<RigidBody>(ctx); float3 ret = thisObj->GetLinearVelocity(); PushValueObjectCopy<float3>(ctx, ret, float3_ID, float3_Finalizer); return 1; }
void SnowBall::ObjectCollision(GameObject* otherObject, VariantMap& eventData) { if (hitDamage > 0) { RigidBody* body = node_->GetComponent<RigidBody>(); float l = body->GetLinearVelocity().Length(); if ((body->GetLinearVelocity().Length() >= snowballMinHitSpeed)) { if (side != otherObject->GetSide()) { otherObject->Damage(this, hitDamage); // Create a temporary node for the hit sound SpawnSound(node_, node_->GetPosition(), "Sounds/PlayerFistHit.wav", 0.2); } hitDamage = 0; } } if (duration > snowballObjectHitDuration) duration = snowballObjectHitDuration; }
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 Character::FixedUpdate(float timeStep) { /// \todo Could cache the components for faster access instead of finding them each frame RigidBody* body = GetComponent<RigidBody>(); AnimationController* animCtrl = GetComponent<AnimationController>(); // Update the in air timer. Reset if grounded if (!onGround_) inAirTimer_ += timeStep; else inAirTimer_ = 0.0f; // When character has been in air less than 1/10 second, it's still interpreted as being on ground bool softGrounded = inAirTimer_ < INAIR_THRESHOLD_TIME; // Update movement & animation const Quaternion& rot = node_->GetRotation(); Vector3 moveDir = Vector3::ZERO; const Vector3& velocity = body->GetLinearVelocity(); // Velocity on the XZ plane Vector3 planeVelocity(velocity.x_, 0.0f, velocity.z_); if (controls_.IsDown(CTRL_FORWARD)) moveDir += Vector3::FORWARD; if (controls_.IsDown(CTRL_BACK)) moveDir += Vector3::BACK; if (controls_.IsDown(CTRL_LEFT)) moveDir += Vector3::LEFT; if (controls_.IsDown(CTRL_RIGHT)) moveDir += Vector3::RIGHT; // Normalize move vector so that diagonal strafing is not faster if (moveDir.LengthSquared() > 0.0f) moveDir.Normalize(); // If in air, allow control, but slower than when on ground body->ApplyImpulse(rot * moveDir * (softGrounded ? MOVE_FORCE : INAIR_MOVE_FORCE)); if (softGrounded) { // When on ground, apply a braking force to limit maximum ground velocity Vector3 brakeForce = -planeVelocity * BRAKE_FORCE; body->ApplyImpulse(brakeForce); // Jump. Must release jump control inbetween jumps if (controls_.IsDown(CTRL_JUMP)) { if (okToJump_) { body->ApplyImpulse(Vector3::UP * JUMP_FORCE); okToJump_ = false; } } else okToJump_ = true; } // Play walk animation if moving on ground, otherwise fade it out if (softGrounded && !moveDir.Equals(Vector3::ZERO)) animCtrl->PlayExclusive("Models/Jack_Walk.ani", 0, true, 0.2f); else animCtrl->Stop("Models/Jack_Walk.ani", 0.2f); // Set walk animation speed proportional to velocity animCtrl->SetSpeed("Models/Jack_Walk.ani", planeVelocity.Length() * 0.3f); // Reset grounded flag for next frame onGround_ = false; }