Exemplo n.º 1
0
void Quaternion::FromRotationTo(const Vector3& start, const Vector3& end)
{
    Vector3 normStart = start.Normalized();
    Vector3 normEnd = end.Normalized();
    float d = normStart.DotProduct(normEnd);
    
    if (d > -1.0f + M_EPSILON)
    {
        Vector3 c = normStart.CrossProduct(normEnd);
        float s = sqrtf((1.0f + d) * 2.0f);
        float invS = 1.0f / s;
        
        x_ = c.x_ * invS;
        y_ = c.y_ * invS;
        z_ = c.z_ * invS;
        w_ = 0.5f * s;
    }
    else
    {
        Vector3 axis = Vector3::RIGHT.CrossProduct(normStart);
        if (axis.Length() < M_EPSILON)
            axis = Vector3::UP.CrossProduct(normStart);
        
        FromAngleAxis(180.f, axis);
    }
}
Exemplo n.º 2
0
void SoundSource3D::CalculateAttenuation()
{
    if (!audio_)
        return;

    float interval = farDistance_ - nearDistance_;
    if (node_)
    {
        SoundListener* listener = audio_->GetListener();

        // Listener must either be sceneless or in the same scene, else attenuate sound to silence
        if (listener && listener->IsEnabledEffective() && (!listener->GetScene() || listener->GetScene() == GetScene()))
        {
            Node* listenerNode = listener->GetNode();
            Vector3 relativePos
                (listenerNode->GetWorldRotation().Inverse() * (node_->GetWorldPosition() - listenerNode->GetWorldPosition()));
            float distance = relativePos.Length();

            // Distance attenuation
            if (interval > 0.0f)
                attenuation_ = powf(1.0f - Clamp(distance - nearDistance_, 0.0f, interval) / interval, rolloffFactor_);
            else
                attenuation_ = distance <= nearDistance_ ? 1.0f : 0.0f;

            // Panning
            panning_ = relativePos.Normalized().x_;

            // Angle attenuation
            if (innerAngle_ < DEFAULT_ANGLE && outerAngle_ > 0.0f)
            {
                Vector3 listenerRelativePos
                    (node_->GetWorldRotation().Inverse() * (listenerNode->GetWorldPosition() - node_->GetWorldPosition()));
                float listenerDot = Vector3::FORWARD.DotProduct(listenerRelativePos.Normalized());
                float listenerAngle = acosf(listenerDot) * M_RADTODEG * 2.0f;
                float angleInterval = Max(outerAngle_ - innerAngle_, 0.0f);
                float angleAttenuation = 1.0f;

                if (angleInterval > 0.0f)
                {
                    if (listenerAngle > innerAngle_)
                    {
                        angleAttenuation = powf(1.0f - Clamp(listenerAngle - innerAngle_, 0.0f, angleInterval) / angleInterval,
                            rolloffFactor_);
                    }
                }
                else
                    angleAttenuation = listenerAngle <= innerAngle_ ? 1.0f : 0.0f;

                attenuation_ *= angleAttenuation;
            }
        }
        else
            attenuation_ = 0.0f;
    }
    else
        attenuation_ = 0.0f;
}
bool Quaternion::FromLookRotation(const Vector3& direction, const Vector3& upDirection)
{
    Quaternion ret;
    Vector3 forward = direction.Normalized();

    Vector3 v = forward.CrossProduct(upDirection);
    // If direction & upDirection are parallel and crossproduct becomes zero, use FromRotationTo() fallback
    if (v.LengthSquared() >= M_EPSILON)
    {
        v.Normalize();
        Vector3 up = v.CrossProduct(forward);
        Vector3 right = up.CrossProduct(forward);
        ret.FromAxes(right, up, forward);
    }
    else
        ret.FromRotationTo(Vector3::FORWARD, forward);

    if (!ret.IsNaN())
    {
        (*this) = ret;
        return true;
    }
    else
        return false;
}
Exemplo n.º 4
0
void IK::DefineM(const Vector3 p, const Vector3 d)
{
	Vector3 mX,mY,mZ;

	// Minv defines a coordinate system whose x axis contains P, so X = unit(P).
	mX = p.Normalized();

	// The y axis of Minv is perpendicular to P, so Y = unit( D - X(D·X) ).

    float  dDOTx = d.DotProduct(mX);
   	mY.x_ = d.x_ - dDOTx * mX.x_;
   	mY.y_ = d.y_ - dDOTx * mX.y_;
   	mY.z_ = d.z_ - dDOTx * mX.z_;
    mY.Normalize();

	// The z axis of Minv is perpendicular to both X and Y, so Z = X×Y.

    mZ=mX.CrossProduct(mY);

    Minv = Matrix3(mX.x_,mX.y_,mX.z_,mY.x_,mY.y_,mY.z_,mZ.x_,mZ.y_,mZ.z_);
    //Minv = Minv.Transpose();
    Mfwd = Minv.Transpose();
	// Mfwd = (Minv)T, since transposing inverts a rotation matrix.

}
Exemplo n.º 5
0
/** see: Player **/
void EnemyShip::setBearing(Vector3<float> headPosition, Vector3<float> headUp)
{
    if (headPosition.x != m_currentHeadPos.x || headPosition.y != m_currentHeadPos.y
            || headPosition.z != m_currentHeadPos.z) {
        m_previousHeadPos = m_currentHeadPos;
        m_currentHeadPos = headPosition;
        m_previousHeadUp = m_currentHeadUp;
    }

    m_currentHeadUp = headUp.Normalized();

    m_progressVelocity = (headPosition - m_progress).Normalized() * PATHVELOCITY;

    prevAngle = currentAngle;
    currentAngle = 180.0f / 3.14159265f * acos(m_previousHeadUp.Normalized().Dot(headUp.Normalized()));
}
Exemplo n.º 6
0
void Explosion::UpdateExplosion(StringHash eventType, VariantMap& eventData)
{
    float timeStep = eventData[Update::P_TIMESTEP].GetFloat();

    rigidBody_->SetMass(Max(initialMass_*((0.1f - age_)/0.1f),0.01f));
    light_->SetBrightness(Max(initialBrightness_*(0.32f - age_)/0.32f,0.0f));

    if (rootNode_->IsEnabled() && masterControl_->world.scene->IsUpdateEnabled()) {
        PODVector<RigidBody* > hitResults;
        float radius = 2.0f * initialMass_ + age_ * 7.0f;
        if (masterControl_->PhysicsSphereCast(hitResults,rootNode_->GetPosition(), radius, M_MAX_UNSIGNED)) {
            for (int i = 0; i < hitResults.Size(); i++) {
                Vector3 hitNodeWorldPos = hitResults[i]->GetNode()->GetWorldPosition();
                if (!hitResults[i]->IsTrigger() && hitResults[i]->GetPosition().y_ > -0.1f) {
                    //positionDelta is used for force calculation
                    Vector3 positionDelta = hitNodeWorldPos - rootNode_->GetWorldPosition();
                    float distance = positionDelta.Length();
                    Vector3 force = positionDelta.Normalized() * Max(radius-distance, 0.0f)
                                    * timeStep * 2342.0f * rigidBody_->GetMass();
                    hitResults[i]->ApplyForce(force);
                    //Deal damage
                    unsigned hitID = hitResults[i]->GetNode()->GetID();
                    float damage = rigidBody_->GetMass()*timeStep;
                    if(masterControl_->spawnMaster_->spires_.Keys().Contains(hitID)) {
                        masterControl_->spawnMaster_->spires_[hitID]->Hit(damage, 1);
                    }
                    else if(masterControl_->spawnMaster_->razors_.Keys().Contains(hitID)) {
                        masterControl_->spawnMaster_->razors_[hitID]->Hit(damage, 1);
                    }
                }
            }
        }
    }
}
Exemplo n.º 7
0
/********************************************************************************
Update the camera for Third Person View
Vector3 newPosition is the new position which the camera is to be based on
********************************************************************************/
void Camera3::UpdatePosition(Vector3 newPosition, Vector3 newDirection)
{
	position = newPosition - (newDirection.Normalized() * m_fTPVCameraOffset);
	if (position.y < 0.f + newPosition.y)
		position.y = 0.f + newPosition.y;
	else if (position.y > 30.f + newPosition.y)
		position.y = 30.f + newPosition.y;
	target = newPosition;// + newDirection.Normalized() * m_fTPVCameraOffset
}
Exemplo n.º 8
0
void Quaternion::AxisAngleRadians(const Vector3& axis, float radians, Quaternion& outQuat)
{
    float hr = radians * 0.5f;
    float sr = Sin(hr);

    Vector3 n = axis.Normalized();
    n *= sr;
    _XO_ASSIGN_QUAT_Q(outQuat, Cos(radians), n.x, n.y, n.z);
}
Exemplo n.º 9
0
void Quaternion::FromAngleAxis(float angle, const Vector3& axis)
{
    Vector3 normAxis = axis.Normalized();
    angle *= M_DEGTORAD_2;
    float sinAngle = sinf(angle);
    float cosAngle = cosf(angle);
    
    w_ = cosAngle;
    x_ = normAxis.x_ * sinAngle;
    y_ = normAxis.y_ * sinAngle;
    z_ = normAxis.z_ * sinAngle;
}
Exemplo n.º 10
0
void Quaternion::FromAngleAxis(float angle, const Vector3& axis)
{
	Vector3 normAxis = axis.Normalized();
	angle *= Math::kDEG2RAD_2;
	float sinAngle = sinf(angle);
	float cosAngle = cosf(angle);

	w = cosAngle;
	x = normAxis.x * sinAngle;
	y = normAxis.y * sinAngle;
	z = normAxis.z * sinAngle;
}
Exemplo n.º 11
0
//---------------------------------
//
//---------------------------------
void Matrix4::LookAt(const Vector3& pos, const Vector3& look, const Vector3& up)
{
    Vector3 zVector = pos - look;
    
    zVector.Normalized();

    Vector3 xVector = zVector.Cross( up		);
    Vector3 yVector = xVector.Cross( zVector );

    xVector.Normalized();
    yVector.Normalized();

    float values[ MATRIX_SIZE ] = { 
                                    xVector.X(), yVector.X(), zVector.X(), 0,
                                    xVector.Y(), yVector.Y(), zVector.Y(), 0,
                                    xVector.Z(), yVector.Z(), zVector.Z(), 0,
                                              0,           0,           0, 1
                                   };
    Matrix4 lookMatrix( values );

    lookMatrix.Translate( Vector3( -pos.X(), -pos.Y(), -pos.Z() ) );
}
Exemplo n.º 12
0
//Constructs a Quaternion that represents the given axis-angle rotation
Quaternion::Quaternion(const Vector3& axis, float angle){
	const float _Pi = 3.14159265358979323846264338f;

	float halfAngle = fmodf(angle/2, _Pi);
	w = cos(halfAngle);

	Vector3 normalizedAxis = axis.Normalized();
	float sinHalfAngle = sqrt(1 - w*w); //because cos(x)^2 + sin(x)^2 = 1

	x = normalizedAxis.x * sinHalfAngle;
	y = normalizedAxis.y * sinHalfAngle;
	z = normalizedAxis.z * sinHalfAngle;
}
Exemplo n.º 13
0
void Helicopter::Init(const Vector3& pos, const Vector3& frontDir)
{
    this->position = pos;
    this->frontDir = defaultFront = frontDir.Normalized();
    this->thrustDir = Vector3(0, 1, 0);
    this->right = this->defaultRight = (this->frontDir.Cross(Vector3(0, 1, 0))).Normalized();

    camera.Init(pos-frontDir*-10.f, pos, Vector3(0, 1, 0));
    rotateYaw = rotatePitch = rotateRoll = 0.f;
    vSpeed = xSpeed = zSpeed = 0.f;

    hitbox.SetSize(1.8f, 1.5f, 1.8f);
    hitbox.SetPosition(position);
}
Exemplo n.º 14
0
void enemy::Update(double dt, Camera3 camera)
{
	//positioning of AI
	Vector3 direction = camera.position - pos;
	direction = direction.Normalized();
	
	if (pos.x + (direction.x * dt * speed) < -330 && pos.x + (direction.x * dt *speed) > -470 && pos.z + (direction.z * dt *speed) > -10 && pos.z + (direction.z * dt *speed) < 80)
	{
		if (pos.x + (direction.x * dt * speed) < -330 && pos.x + (direction.x * dt *speed) > -470)
		{
			pos.z--;
		}
		if (pos.z + (direction.z * dt *speed) > -10 && pos.z + (direction.z * dt *speed) < 80)
		{
			pos.x++;
		}
	}
	else if (pos.x + (direction.x * dt * speed) < Camera3::location.x + 5 && pos.x + (direction.x * dt *speed) > Camera3::location.x - 5 && pos.z + (direction.z * dt *speed) > Camera3::location.z -5 && pos.z + (direction.z * dt *speed) < Camera3::location.z + 5)
	{

	}
	else
	{
		pos.x += (direction.x * dt * speed) * (1 + (enemyUpgrade/4));
		//std::cout << (direction.x * dt * speed) * ((enemyUpgrade / 4) + 1) << std::endl;
		//pos.y += (direction.y * dt * speed);
		pos.z += (direction.z * dt * speed) * (1 + (enemyUpgrade / 4));
	}

	//Rotation of AI
	Vector3 directionInit(0, 0, 1);
	Vector3 normal(0, 1, 0);
	Degree = Math::RadianToDegree(acos(directionInit.Dot(direction)));
	Vector3 Crossed = directionInit.Cross(direction);
	if (Crossed.Dot(normal) < 0)
	{
		Degree *= -1;
	}

	//std::vector<Vector3>::iterator count = bulletPos.begin();
	//std::vector<Vector3>::iterator count1 = bulletDir.begin();

	//while (count != bulletPos.end())
	//{
	//	*count += *count1;
	//	*count++;
	//	*count1++;
	//}
}
Exemplo n.º 15
0
void Polygon::RotateArbitrary(float angle, Vector3 pointa, Vector3 pointb)
{
	//Setup values
	Vector3 v;
	v = pointa - pointb;
	v = v.Normalized();
	float d = sqrt(pow(v.y, 2) + pow(v.z, 2));

	//Step 1
	Translate(-pointa);

	//Step 2
	Matrix<4, 4> a = {1, 0, 0, 0,
					  0, v.z/d, -v.y/d, 0,
					  0, v.y/d, v.z/d, 0,
					  0, 0, 0, 1};

	Matrix<4, 4> b = {d, 0, -v.x, 0,
					  0, 1, 0, 0,
					  v.x, 0, d, 0,
					  0, 0, 0, 1};
	for(int i=0; (unsigned) i<vertices.size(); i++)
	{
		vertices[i] = MultiplyByMatrix(vertices[i], a);
		vertices[i] = MultiplyByMatrix(vertices[i], b);
	}

	//Step 3
	RotateZAxis(angle);

	//Step 4
	Matrix<4, 4> c = {1, 0, 0, 0,
					  0, v.z/d, v.y/d, 0,
					  0, -v.y/d, v.z/d, 0,
					  0, 0, 0, 1};

	Matrix<4, 4> e = {d, 0, v.x, 0,
					  0, 1, 0, 0,
					  -v.x, 0, d, 0,
					  0, 0, 0, 1};
	for(int i=0; (unsigned) i<vertices.size(); i++)
	{
		vertices[i] = MultiplyByMatrix(vertices[i], e);
		vertices[i] = MultiplyByMatrix(vertices[i], c);
	}

	//Step 5
	Translate(pointa);
}
Exemplo n.º 16
0
void Quaternion::FromRotationTo(const Vector3& start, const Vector3& end)
{
	Vector3 normStart = start.Normalized();
	Vector3 normEnd = end.Normalized();
	float d = normStart.Dot(normEnd);

	if (d > -1.0f + Math::kLARGE_EPSILON) {
		Vector3 c = normStart.Cross(normEnd);
		float s = sqrtf((1.0f + d) * 2.0f);
		float invS = 1.0f / s;

		x = c.x * invS;
		y = c.y * invS;
		z = c.z * invS;
		w = 0.5f * s;
	}
	else {
		Vector3 axis = Vector3::RIGHT.Cross(normStart);
		if (axis.Length() < Math::kLARGE_EPSILON)
			axis = Vector3::UP.Cross(normStart);

		FromAngleAxis(180.f, axis);
	}
}
Exemplo n.º 17
0
void Quaternion::FromLookRotation(const Vector3& direction, const Vector3& upDirection)
{
    Vector3 forward = direction.Normalized();
    Vector3 v = forward.CrossProduct(upDirection).Normalized(); 
    Vector3 up = v.CrossProduct(forward);
    Vector3 right = up.CrossProduct(forward);

    Quaternion ret;
    ret.w_ = sqrtf(1.0f + right.x_ + up.y_ + forward.z_) * 0.5f;
    float w4Recip = 1.0f / (4.0f * ret.w_);
    ret.x_ = (up.z_ - forward.y_) * w4Recip;
    ret.y_ = (forward.x_ - right.z_) * w4Recip;
    ret.z_ = (right.y_ - up.x_) * w4Recip;

    (*this) = ret;
}
Exemplo n.º 18
0
    static Matrix4<T> LookAtWithVector(const Vector3<T>& eye,
            const Vector3<T>& lookVector, const Vector3<T>& up) {
        Vector3<T> z = lookVector.Normalized();
        Vector3<T> x = up.Cross(z).Normalized();
        Vector3<T> y = z.Cross(x).Normalized();

        Matrix4<T> m;
        m.x = Vector4<T>(x, 0);
        m.y = Vector4<T>(y, 0);
        m.z = Vector4<T>(z, 0);
        m.w = Vector4<T>(0, 0, 0, 1);

        Vector4<T> eyePrime = m * Vector4<T>(-eye, 1);
        m = m.Transposed();
        m.w = eyePrime;

        return m;
    }
void CameraMaster::HandleSceneUpdate(StringHash /* eventType */, VariantMap &eventData)
{
    using namespace Update;

    //Take the frame time step, which is stored as a double
    double timeStep = eventData[P_TIMESTEP].GetFloat();
    //Movement speed as world units per second
    const double MOVE_SPEED = 2000.0;
    //Mouse sensitivity as degrees per pixel
    const double MOUSE_SENSITIVITY = 0.1;

    //Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees. Only move the camera when the cursor is hidden.
    Input* input = GetSubsystem<Input>();
    IntVector2 mouseMove = input->GetMouseMove();
    yawDelta_ = 0.5*(yawDelta_ + MOUSE_SENSITIVITY * mouseMove.x_);
    pitchDelta_ = 0.5*(pitchDelta_ + MOUSE_SENSITIVITY * mouseMove.y_);
    yaw_ += yawDelta_;
    pitch_ += pitchDelta_;
    pitch_ = Clamp(pitch_, -89.0, 89.0);
    //Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
    translationNode_->SetRotation(Quaternion(0.0f, 0.0f, 0.0f));
    rotationNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));

    //Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
    Vector3 camForce = Vector3::ZERO;
    if (input->GetKeyDown('W')) camForce += Scale(rotationNode_->GetDirection(), Vector3(1.0f,0.0f,1.0f) ).Normalized();
    if (input->GetKeyDown('S')) camForce += Scale(rotationNode_->GetDirection(), Vector3(-1.0f,0.0f,-1.0f) ).Normalized();
    if (input->GetKeyDown('D')) camForce += Scale(rotationNode_->GetRight(), Vector3(1.0f,0.0f,1.0f) ).Normalized();
    if (input->GetKeyDown('A')) camForce += Scale(rotationNode_->GetRight(), Vector3(-1.0f,0.0f,-1.0f) ).Normalized();
    if (input->GetKeyDown('E')) camForce += Vector3::UP;
    if (input->GetKeyDown('Q') && translationNode_->GetPosition().y_ > 1.0f) camForce += Vector3::DOWN;
    camForce = camForce.Normalized() * MOVE_SPEED * timeStep;

    if ( forceMultiplier < 8.0 && (input->GetKeyDown(KEY_LSHIFT)||input->GetKeyDown(KEY_RSHIFT)) ) {
        forceMultiplier += 0.23;
    } else forceMultiplier = pow(forceMultiplier, 0.75);
    rigidBody_->ApplyForce( (forceMultiplier * camForce) - (2.3f * rigidBody_->GetLinearVelocity()) );

    if (translationNode_->GetPosition().y_ < 1.0f)
    {
        translationNode_->SetPosition(Vector3(translationNode_->GetPosition().x_, 1.0f, translationNode_->GetPosition().z_));
        rigidBody_->SetLinearVelocity(Vector3(rigidBody_->GetLinearVelocity().x_, 0.0f, rigidBody_->GetLinearVelocity().z_));
    }
}
Exemplo n.º 20
0
// finds closest point on the specified triangle to the specified line
bool GetClosestPointOnTri(const Line& line, const Vector3& v0, const Vector3& v1, const Vector3& v2, Vector3& result)
{
	Vector3 normal ( (v1 - v0).Cross(v2 - v1) );
	if (normal.LengthSquared() > HELIUM_VALUE_NEAR_ZERO)//should remove this redundant work shared here and in normalized
	{
		normal.Normalized();
		Vector3 line_dir = line.m_Point - line.m_Origin;
		if (fabs(line_dir.Dot(normal)) < HELIUM_VALUE_NEAR_ZERO )
		{
			return false;
		}
		float32_t plane_w = normal.Dot(v0);
		//find the pt on tri
		float32_t origin_t = normal.Dot(line.m_Origin) - plane_w;
		float32_t end_t = normal.Dot(line.m_Point) - plane_w;
		float32_t plane_pt_t = origin_t/(origin_t-end_t);
		Vector3 pt_on_plane = line.m_Origin + line_dir*plane_pt_t;
		//now the pt is guaranteed to be not inside the lines so blindly i will find the closest pt on each edge and pick the one closest among the 3
		//better than doing a cross for each edge and directly narrow down which edge or vert it is closest to
		Vector3 closest_pt_on_edges[3];
		float32_t dist_sqr_to_closest_pt_on_edges[3];
		dist_sqr_to_closest_pt_on_edges[0] = GetClosestPointOnEdge(v0, v1, pt_on_plane, closest_pt_on_edges[0]);
		dist_sqr_to_closest_pt_on_edges[1] = GetClosestPointOnEdge(v1, v2, pt_on_plane, closest_pt_on_edges[1]);
		dist_sqr_to_closest_pt_on_edges[2] = GetClosestPointOnEdge(v2, v0, pt_on_plane, closest_pt_on_edges[2]);
		result = closest_pt_on_edges[0];
		float32_t closest_dist_sqr = dist_sqr_to_closest_pt_on_edges[0];
		if (closest_dist_sqr > dist_sqr_to_closest_pt_on_edges[1])
		{
			closest_dist_sqr = dist_sqr_to_closest_pt_on_edges[1];
			result = closest_pt_on_edges[1];
		}
		if (closest_dist_sqr > dist_sqr_to_closest_pt_on_edges[2])
		{
			closest_dist_sqr = dist_sqr_to_closest_pt_on_edges[2];
			result = closest_pt_on_edges[2];
		}

		return true;
	}
	else
	{
		return false;
	}
}
Exemplo n.º 21
0
Matrix Matrix::LookAtDirection(Vector3 position, Vector3 eyeDirection)
{
	Vector3 up = Vector3(0,1,0);
	
	Vector3 Z = eyeDirection.Normalized();
	Vector3 X = Vector3(up.Cross(Z)).Normalized();
	Vector3 Y = Vector3(Z.Cross(X)).Normalized();
	
	
	Matrix M = Matrix(
		X.X , Y.X , Z.X , 0 ,
		X.Y , Y.Y , Z.Y , 0 ,
		X.Z , Y.Z , Z.Z , 0 ,
		0 , 0 , 0 , 1) ;

	Matrix T = Matrix::Translation(position*-1) ;

	return M * T ;
}
Exemplo n.º 22
0
void DebugRenderer::AddCircle(const Vector3& center, const Vector3& normal, float radius, const Color& color, int steps, bool depthTest)
{
    Quaternion orientation;
    orientation.FromRotationTo(Vector3::UP, normal.Normalized());
    Vector3 p = orientation * Vector3(radius, 0, 0) + center;
    unsigned uintColor = color.ToUInt();

    for(int i = 1; i <= steps; ++i)
    {
        const float angle = (float)i / (float)steps * 360.0f;
        Vector3 v(radius * Cos(angle), 0, radius * Sin(angle));
        Vector3 c = orientation * v + center;
        AddLine(p, c, uintColor, depthTest);
        p = c;
    }

    p = center + normal * (radius / 4.0f);
    AddLine(center, p, uintColor, depthTest);
}
Exemplo n.º 23
0
		inline void Init(ParticleEmitter* emitter)
		{
			const float& et = emitter->m_emitterRate;
			life = maxLife = emitter->m_param_Lifetime->Init(et);
			pos = emitter->m_param_StartPosition->Init(et) * emitter->scale;

			// Velocity of startvelocity and spawn offset scale
			velocity = emitter->m_param_StartVelocity->Init(et) * emitter->scale;
			float spawnVelScale = emitter->m_param_SpawnVelocityScale->Init(et);
			if(spawnVelScale > 0)
				velocity += pos.Normalized() * spawnVelScale  * emitter->scale;

			// Add emitter offset to location
			pos += emitter->position;

			startColor = emitter->m_param_StartColor->Init(et);
			rotation = emitter->m_param_StartRotation->Init(et);
			startSize = emitter->m_param_StartSize->Init(et) * emitter->scale;
			drag = emitter->m_param_StartDrag->Init(et);
		}
Exemplo n.º 24
0
/********************************************************************************
Move the camera forward
********************************************************************************/
void TPCamera::MoveForward(const double dt)
{
	// Calculate the direction vector of the camera
	Vector3 view = (target - position).Normalized();

	// Constrain the movement to the ground if the camera type is land based
	if (sCameraType == LAND_CAM)
	{
		view.y = 0.0f;
		view = view.Normalized();
	}

	MoveVel_W = MoveVel_W + CAMERA_ACCEL * (float)dt;
	if (MoveVel_W > CAMERA_SPEED)
		MoveVel_W = CAMERA_SPEED;

	// Update the camera and target position
	position += view * MoveVel_W;
	target += view * MoveVel_W;
}
Exemplo n.º 25
0
/********************************************************************************
Move the camera backward
********************************************************************************/
void TPCamera::MoveBackward(const double dt)
{
	Vector3 view = (target - position).Normalized();

	// Constrain the movement to the ground if the camera type is land based
	if (sCameraType == LAND_CAM)
	{
		view.y = 0.0f;
		view = view.Normalized();
	}

	MoveVel_S = MoveVel_S + CAMERA_ACCEL * (float)dt;
	if (MoveVel_S < -CAMERA_SPEED)
		MoveVel_S = -CAMERA_SPEED;

	// Update the camera and target position
	position -= view * MoveVel_S;
	target -= view * MoveVel_S;

}
Exemplo n.º 26
0
void Bird::Fly(float timeStep)
{
    Vector3 targetDelta = target_ - GetPosition();
    Vector3 beforeVelocity = velocity_;
    bool limit = velocity_.Angle(targetDelta) < 90.0f && velocity_.Length() > maxVelocity_;
    if (!limit) velocity_ += 6.0f * timeStep * targetDelta.Normalized()*Clamp(targetDelta.Length(), 2.0f, 3.0f);
    velocity_ += dead_*timeStep*Vector3::DOWN*23.0f;
    if (targetDelta.Angle(rootNode_->GetDirection()) > 90.0f && seenTarget_){
        target_ = AirTarget();
        seenTarget_ = false;
    }
    else if (targetDelta.Angle(rootNode_->GetDirection()) < 90.0f){
        seenTarget_ = true;
    }
    velocity_ *= 0.99f;
    Vector3 acceleration = velocity_ - beforeVelocity;
    animCtrl_->SetSpeed("Resources/Models/Fly.ani", 3.0f - species_->At((int)Gene::Scale)+0.1f*acceleration.y_);
    if (acceleration.Length() / (0.01f+timeStep) < 7.0f) animCtrl_->Play("Resources/Models/Soar.ani", 1, true, 0.23f);
    else animCtrl_->Stop("Resources/Models/Soar.ani", 0.1f);
}
Exemplo n.º 27
0
void TurntableCamera<real>::Update()
{
	Transformation<real> transformation;
	Vector3<real> initRotationAxis = Vector3<real>( 0, 1, 0 ) ^ mThetaAxis;

	if ( initRotationAxis.SquaredLength() > 1e-5 )
		transformation.SetRotationAxis( initRotationAxis.Normalized(), asin( initRotationAxis.Length() ) );

	transformation.RotateAxis( mPhiAxis, mPhi );
	transformation.RotateAxis( mThetaAxis, mTheta );

	Transformation<real> transformationT;
	transformationT.RotateAxis( mPhiAxis, mPhi );
	transformationT.RotateAxis( mThetaAxis, mTheta );
	transformation.SetTranslation( transformationT.GetRotation() * ( mPhiAxis ^ mThetaAxis ).Normalized() * mDistance + mCenter );

	this->SetLocalTransformation( transformation );

	Camera<real>::Update();
}
Exemplo n.º 28
0
void Weapon::Recoil()
{
    //I think that before I make the projectile, I should move the gun, to straighted it, as well as create kick
    Quaternion rot = node_->GetWorldRotation();
    Vector3 pos = node_->GetWorldPosition();

    //initlal rotation of fire_direction
    Vector3 rotoff = rot*fire_off_;
    projectile_spawn_position_ = pos+rotoff;
    projectile_spawn_direction_ = rotoff.Normalized()*Vector3(1.0f,1.0f,0.0f);//make sure its stays on plane

    //get rotation axis
    //Vector3 rotaxis = dir.CrossProduct(Vector3(0.0f,1.0f,0.0f));//local to the gun
    Vector3 rotaxis = Vector3(0.0f,1.0f,0.0f);//local to the gun
    kick_rot_ = Quaternion(Random(4.0f),rotaxis);
    kick_off_ = Vector3(Random(0.1f),Random(0.1f),Random(0.1f));

    node_->SetTransform(kick_off_,kick_rot_);

    lefthand_target_=lefthand_grip_->GetWorldPosition();
}
Exemplo n.º 29
0
void boss::Update(double dt, Camera3 camera)
{
	//positioning of AI
	Vector3 direction = camera.position - pos;
	direction = direction.Normalized();

	if (pos.x + (direction.x * dt * speed) < -330 && pos.x + (direction.x * dt *speed) > -470 && pos.z + (direction.z * dt *speed) > -10 && pos.z + (direction.z * dt *speed) < 80)
	{
		if (pos.x + (direction.x * dt * speed) < -330 && pos.x + (direction.x * dt *speed) > -470)
		{
			pos.z--;
		}
		if (pos.z + (direction.z * dt *speed) > -10 && pos.z + (direction.z * dt *speed) < 80)
		{
			pos.x++;
		}
	}
	else if (pos.x + (direction.x * dt * speed) < Camera3::location.x + 5 && pos.x + (direction.x * dt *speed) > Camera3::location.x - 5 && pos.z + (direction.z * dt *speed) > Camera3::location.z - 5 && pos.z + (direction.z * dt *speed) < Camera3::location.z + 5)
	{

	}
	else
	{
		pos.x += (direction.x * dt * speed) * (0.5 + (bossUpgrade/2));
		//std::cout << (direction.x * dt * speed) * ((bossUpgrade / 4) + 1) << std::endl;
		//pos.y += (direction.y * dt * speed);
		pos.z += (direction.z * dt * speed) * (0.5 + (bossUpgrade / 2));
	}

	//Rotation of AI
	Vector3 directionInit(0, 0, 1);
	Vector3 normal(0, 1, 0);
	Degree = Math::RadianToDegree(acos(directionInit.Dot(direction)));
	Vector3 Crossed = directionInit.Cross(direction);
	if (Crossed.Dot(normal) < 0)
	{
		Degree *= -1;
	}

}
Exemplo n.º 30
0
void Bird::Land(float timeStep)
{
    Vector3 targetDelta = target_ - GetPosition();
    if (targetDelta.Length() < 0.5f){
        if (!touchDown_){
            touchDown_ = true;
            animCtrl_->StopAll(0.1f);
            animCtrl_->Play("Resources/Models/Land.ani", 0, false, 0.1f);
        }
        Quaternion rotation = rootNode_->GetWorldRotation();
        Vector3 eulerRotation = rotation.EulerAngles();
        Quaternion aimRotation = Quaternion(0.0f, eulerRotation.y_, 0.0f);
        if (first_) aimRotation = Quaternion(0.0f, rootNode_->GetDirection().x_ > 0.0f? 90.0f : -90.0f, 0.0f);
        rootNode_->SetRotation(rotation.Slerp(aimRotation, 5.0f*timeStep));

        if (targetDelta.Length() < 0.2f || targetDelta.y_ > 0.0f) {
            SetState(BirdState::Standing);
        }
    }
    velocity_ += 23.0f * timeStep * targetDelta.Normalized() * Clamp( targetDelta.Length()*0.5f, 2.0f, 4.0f ) - Finchy::Scale(velocity_, Vector3(0.023f, 0.23f, 0.023f));
    velocity_ *= Clamp( 0.2f + targetDelta.Length() * 0.666f, 0.2f, 0.95f );
    velocity_.x_ = Clamp(velocity_.x_, -targetDelta.Length(), targetDelta.Length());
    velocity_.y_ = Clamp(velocity_.y_, -targetDelta.Length(), targetDelta.Length());
}