Polyangle Polyangle::shrink(const double l) const
{
    int length = lesPoints.size();
    QVector<Vector2D> newPoints;
    newPoints.resize(length);

    for(int cpt = 0; cpt <length ; cpt++)
    {
        Vector2D a = lesPoints.at((cpt-1+length)%length);
        Vector2D b = lesPoints.at((cpt));
        Vector2D c = lesPoints.at((cpt+1)%length);

        Vector2D ab = b-a;
        Vector2D bc = c-b;
        Vector2D ca = c-a;

        //calcul des vecteurs orthogonaux
        Vector2D orthoAB = Vector2D(-ab.y(), ab.x());
        Vector2D orthoBC = Vector2D(-bc.y(), bc.x());

        //on l'inverse si il est dans le mauvais sens
        if(orthoAB*bc < 0)
        {
            orthoAB = -orthoAB;
        }
        if(orthoBC*ca > 0)
        {
            orthoBC = -orthoBC;
        }
        orthoAB.normalize();
        orthoBC.normalize();

        //création des droites afin de récupérer le point d'intersection
        Vector2D aPrime = a + orthoAB * l;
        Vector2D bPrime = b + orthoAB * l;
        Vector2D cPrime;
        Droite aPrimeBPrime(aPrime, bPrime - aPrime);
        bPrime = b + orthoBC * l;
        cPrime = c + orthoBC * l;
        Droite bPrimeCPrime(bPrime, cPrime - bPrime);

        aPrimeBPrime.getIntersection(bPrimeCPrime, aPrime);

        newPoints[cpt] = aPrime;
    }

    return Polyangle(newPoints);
}
Example #2
0
void Animal::handleCollisions(Environment *environment) {
    Vector2D boundsCorrection;
    if (getBounds().getMinX() < environment->getBounds().getMinX()) {
        boundsCorrection.setX(environment->getBounds().getMinX() - getBounds().getMinX());
    }
    if (getBounds().getMinY() < environment->getBounds().getMinY()) {
        boundsCorrection.setY(environment->getBounds().getMinY() - getBounds().getMinY());
    }
    if (getBounds().getMaxX() > environment->getBounds().getMaxX()) {
        boundsCorrection.setX(environment->getBounds().getMaxX() - getBounds().getMaxX());
    }
    if (getBounds().getMaxY() > environment->getBounds().getMaxY()) {
        boundsCorrection.setY(environment->getBounds().getMaxY() - getBounds().getMaxY());
    }
    translate(boundsCorrection);
    
    std::vector<Object *> nearestObjects = environment->getNearestObjects(this);
    for (int k = 0; k < nearestObjects.size(); k++) {
        Object *nearestObject = nearestObjects[k];
        double distance = getPosition().distanceTo(nearestObject->getPosition());
        double radius = (OBJECT_WIDTH / 2);
        if (distance < 2.0 * radius) {
            Vector2D overlapVector = Vector2D(nearestObject->getPosition(), getPosition());        
            overlapVector = overlapVector.normalize();
            
            double overlapLength = 2.0 * radius - distance;
            overlapVector = overlapVector.mult(overlapLength);
            
            translate(overlapVector);
            
            handleCollision(environment, nearestObject);
        }
    }
}
Polyangle Polyangle::getIncircle(int definition) const
{
    float pi = 3.14159265358979323846;
    Vector2D center = getCentre();
    QVector<Vector2D> newPoints;
    float radius = std::numeric_limits<float>::max();
    for (int i = 0; i < lesPoints.size(); i++)
    {
        Vector2D segment = (lesPoints[(i+1)%lesPoints.size()]-lesPoints[i]);
        segment.normalize();
        float scalar = (center-lesPoints[i])* segment;
        Vector2D nearestPoint = segment * scalar+lesPoints[i];
        float distance = center.distanceToPoint2D(nearestPoint);
        if (distance < radius)
            radius = distance;
    }

    for (int i=0; i < definition; i++)
    {
        float x = cos(2*pi*i/definition)*radius;
        float y = sin(2*pi*i/definition)*radius;
        newPoints << center + Vector2D(x, y);
    }

    return Polyangle(newPoints);

}
Example #4
0
inline Vector2D Vector2D::reflect(const Vector2D &other) const
{
  Vector2D n = other;
  n.normalize();
  Vector2D w = *this - n * dot(n) * 2;
  return w;
}
Example #5
0
void Boid::cohesion(deque<Boid*>& neighbours) {
	Vector2D vForce;
	float neighborCount = 0;
	Vector2D vCenterOfMass;
	deque<Boid*>::iterator itBoid;
	
	for(itBoid = neighbours.begin(); itBoid != neighbours.end(); ++itBoid) {
		if(*itBoid == this) continue;	
		
		vCenterOfMass.add((*itBoid)->getVelocity());
		++neighborCount;
	}
	
	if(neighborCount > 0) {
		vCenterOfMass.devide(neighborCount);
		
		if(vCenterOfMass.distanceToSQ(getVelocity()) > 5.0f * 5.0f) {
			Vector2D vel = vCenterOfMass;
			Vector2D pos = position;
			pos.normalize();
			
			vel.subtract(pos);
			vel.subtract(getVelocity());
			vel.scale(0.1);
			
			velocity.add(vel);
		}
	}
	
	
}
Example #6
0
void ParticleAttraction::updateForce(Particle *particle, real duration) {
    if (!particle->hasFiniteMass()) return;
    Vector2D force;
    particle->getPosition(&force);

    // Calculate direction from particle to force's origin
    force = origin - force;
    force.normalize();
    particle->addForce(force * magnitude * particle->getMass());
}
Example #7
0
void ZombieDefenseManager::makeBullet(const Sprite& shooter, const Sprite& target) { 	   
   Vector2D origins = shooter.getCoordinate();
   origins[0] += (shooter.getFrame()->getWidth() / 2);
   origins[1] += (shooter.getFrame()->getHeight() / 2);
   float radius = shooter.getFrame()->getWidth() / 2 + shooter.getFrame()->getHeight() / 2;
   Vector2D shoot = target.getCoordinate() - origins;
   shoot.normalize();
   origins += radius * shoot;
   bullets.push_back(Bullet(origins, shoot * game->getBulletSpeed(), fact->getFrame(bulletSurfaceString)));
}
Example #8
0
void ParticleDrag::updateForce(Particle *particle, real duration) {
    Vector2D force;
    particle->getVelocity(&force);

    // Calculate the total drag coefficient.
    real dragCoeff = force.magnitude();
    dragCoeff = k1 * dragCoeff + k2 * dragCoeff * dragCoeff;

    // Calculate the final force and apply it.
    force.normalize();
    force *= -dragCoeff;
    particle->addForce(force);
}
Example #9
0
void ParticleAnchoredSpring::updateForce(Particle *particle, real duration) {
    // Calculate the vector of the spring
    Vector2D force;
    particle->getPosition(&force);
    force -= *anchor;

    // Calculate the magnitude of the force
    real magnitude = force.magnitude();
    magnitude = (restLength - magnitude) * springConstant;

    // Calculate the final force and apply it
    force.normalize();
    force *= magnitude;
    particle->addForce(force);
}
Example #10
0
void MediumPlane::shoot()
{
	if (Game::Instance()->isPracticeMode())
		return;

	m_target = Game::Instance()->getRandomPLayerCenter();

	if (m_enemyWeapon)
	{
		Vector2D shootDirection = m_target - (m_shootingOffset + m_position);
		shootDirection.normalize();
		Vector2D shootPosition = m_shootingOffset + m_position;
		m_enemyWeapon->shoot(shootPosition, shootDirection);
	}
}
Example #11
0
void ParticleSpring::updateForce(Particle *particle, real duration) {
    // Calculate the vector of the spring
    Vector2D force;
    particle->getPosition(&force);
    force -= other->getPosition();

    // Calculate the magnitude of the force
    real magnitude = force.magnitude();
    magnitude = real_abs(magnitude - restLength);
    magnitude *= springConstant;

    // Calculate the final force and apply it
    force.normalize();
    force *= -magnitude;
    particle->addForce(force);
}
Example #12
0
Steering* DynamicArriveSteering::getSteering()
{
	Vector2D direction = mpTarget->getPosition() - mpMover->getPosition();
	float distance = direction.getLength();

	//are we there?
	if( distance < mTargetRadius )
	{
		mLinear = gZeroVector2D;
		mAngular = 0.0f;

		return this;
	}

	float targetSpeed = 0.0f;

	//are we outside slow radius?
	if( distance > mSlowRadius )
	{
		targetSpeed = mpMover->getMaxVelocity();
	}
	else
	{
		targetSpeed = ( mpMover->getMaxVelocity() * distance ) / mSlowRadius;
	}

	//combine speed and direction to get targetVelocity
	Vector2D targetVelocity = direction;
	targetVelocity.normalize();
	targetVelocity *= targetSpeed;

	//set acceleration
	mLinear = targetVelocity - mpMover->getVelocity();
	mLinear /= mTimeToTarget;

	//check if too fast
	if( mLinear.getLength() > mpMover->getMaxAcceleration() )
	{
		//cut down to max
		mLinear.normalize();
		mLinear*= mpMover->getMaxAcceleration();
	}

	mAngular = 0.0f;

	return this;
}
Example #13
0
void Server::regulateSpeed(Entity *ent)
{
	float entVelX		  = ent->getPosition().getX_vel();
	float entVelY         = ent->getPosition().getY_vel();
	float entSpeedSquared = ent->getSpeedSquared();

	float entMaxSpeed     = ent->getMaxSpeed();

	//moving too fast; shorten the velocity vector to maxSpeed
	if (entSpeedSquared > entMaxSpeed * entMaxSpeed)
	{
		Vector2D oldVel = Vector2D(entVelX, entVelY);
		oldVel.normalize();
		oldVel *= entMaxSpeed;
		ent->getPosition().setX_vel(oldVel.x);
		ent->getPosition().setY_vel(oldVel.y);
	}
}
Example #14
0
void ParticleBungee::updateForce(Particle *particle, real duration) {
    // Calculate the vector of the spring
    Vector2D force;
    particle->getPosition(&force);
    force -= other->getPosition();

    // Check if the bungee is compressed
    real magnitude = force.magnitude();
    if (magnitude <= restLength) return;

    // Calculate the magnitude of the force
    magnitude = springConstant * (restLength - magnitude);

    // Calculate the final force and apply it
    force.normalize();
    force *= -magnitude;
    particle->addForce(force);
}
void MultiDirectionalEar::renderFeedback(Object *object, Environment *environment) {
    if (object == environment->getPlayer()) {
        Vector2D windowPos = environment->getWindowPos(object->getPosition());        
        for (int i = 0; i < nearestObjects.size(); i++) {
            Object *nearestObject = nearestObjects[i];
            if (nearestObject->getVoice() && nearestObject->getVoiceInterval() <= 0.0) {
                nearestObject->resetVoiceInterval();
                Vector2D objectWindowPos = environment->getWindowPos(nearestObject->getPosition());
                Vector2D relativePos = objectWindowPos.add(windowPos.mult(-1.0));
                relativePos = relativePos.normalize();
                double pan = relativePos.getX();
                
                double nearestObjectDistance = windowPos.distanceTo(objectWindowPos);
                double gain = 1.0 - (nearestObjectDistance / 1000.0);
                if (gain < 0.0) {
                    gain = 0.0;
                }
                
                al_play_sample(nearestObject->getVoice(), gain, pan, 1.0, ALLEGRO_PLAYMODE_ONCE, NULL);
            }
        }                    
    }
}
Example #16
0
float Vector2D::normalizedDot(Vector2D v){
    Vector2D normedV1 = normalize();
    Vector2D normedV2 = v.normalize();
    return ( normedV1.x*normedV2.x + normedV1.y*normedV2.y );
}
Example #17
0
File: main.cpp Project: CCJY/coliru
int main() {
    Vector2D<float> v = {16,19};
    v.normalize();
    std::cout << v;
}