Example #1
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 #2
0
bool Collision::CircleToAABB(Circle &a, Box &b, bool &hitFloor,bool &hitSide, bool &hitUnder)
{
	
	Vector2D extents(b.getWidth()/2,b.getHeight()/2);//!< puts the extents of the box into a vector2d.
	Vector2D disti = b.getPosition().subtract(a.getPosition());//!< Finds the distance between the two origins.
	Vector2D clamp = clamp.minMax(disti,extents);//!<Clamp closet point?
	disti = disti.subtract(clamp);//!< Calculates the distance from the box. In the X and Y.
	distance = disti.magnitude() - a.getRadius();//!< Gets the length between the two objects. 
	if(distance > 0)
	{
		return false; //!<Exits if the distance is greater than 0
	}
	//return true;
	normal2 = disti.getUnitVector();//!< Calculates the normal
	//!Used to find out if ive hit top side or under
	if(normal2.getY() > 0.2f)
	{ 
		hitFloor = true;
	}
	else hitFloor = false;
	if(normal2.getY() < -0.5f)
	{ 
		hitUnder = true;
	}
	if(normal2.getX() == -1.f || normal2.getX() == 1.f )
	{ 
		hitSide = true;
	}

	return true;
}