Esempio n. 1
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;
}
Esempio n. 2
0
void ParticleUplift::updateForce(Particle *particle, real duration) {
    // Determine if the particle is within range of the origin
    Vector2D relativePos = particle->getPosition() - origin;
    if (relativePos.magnitude() > range) return;

    // Particle is in range; apply uplift force
    particle->addForce(uplift * particle->getMass());
}
Esempio n. 3
0
bool Collision::CircleToLine(Circle &a, Line &b)
{
	Vector2D normal = b.lineNormal();//!< Gets the normal of the line and sets to a new Vector2D
	float mag = normal.magnitude();//!< Finds the magnitude of the normal.
	float c = b.lineOrigin();//!< Find the origin value of the line.
	c /= mag;//!< Normalises the origin value.
	Vector2D normal2 = normal.getUnitVector();//!< normalises the normal.
	float distance = (a.getPosition().dotProduct(normal2) + (c));//!< Finds the distance away from the point.  distance = p.n + 
	return (distance >= 0 - a.getRadius()); //!< If the distance is 0 or less than 0. Return true.
}
Esempio n. 4
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);
}
Esempio n. 5
0
void Collision::CircleToCircle(Circle &a, Circle &b)
{
	ManifoldCtoC m;
	Vector2D diff = a.getPosition().subtract(b.getPosition());//!< Origin of position subtracting origin of other position; 
	float dist = diff.magnitude();//!< Gets the length between the two origins.
	float sumOfRadii = a.getRadius() + b.getRadius();//!< Sum of the radii

	if (dist > sumOfRadii)//!< Exits early if no collision detection.
	{
		return;
	}
	//!send circles and the calculated distance to the manifold.
	m.Initialize(a,b, dist);//!< Initializes the manifold.
	m.ApplyImpulse(a,b);
}
Esempio n. 6
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);
}
Esempio n. 7
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);
}
Esempio n. 8
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);
}
Esempio n. 9
0
int Vector2D::compareTo(Vector2D& v, Vector2D::comparison_type ct) const
{
	double result;
	switch (ct) {
	case BY_X:
		result = x-v.x;
	case BY_Y:
		result = y-v.y;
	case BY_XY:
		if (x-v.x < 0) result = -1;
		if (x-v.x > 0) result = 1;
		result = y-v.y;
	case BY_YX:
		if (y-v.y < 0) result = -1;
		if (y-v.y > 0) result = 1;
		result = x-v.x;
	case BY_MAGNITUDE:
		result = magnitude()-v.magnitude();
	}
	return static_cast<int>(result);
}
// *****************************************************************************
// *****************************************************************************
int TriangleMeshSLK::circle_test(Point2D t1, Point2D t2, Point2D t3, Point2D t) {
    int flag = 0;
    Point2D p1, p2, p3, p;

    // scale incoming triangle to unit max length
    Point2D lo, hi;
    lo[0] = MIN(t1[0], MIN(t2[0], t3[0]));
    lo[1] = MIN(t1[1], MIN(t2[1], t3[1]));
    hi[0] = MAX(t1[0], MAX(t2[0], t3[0]));
    hi[1] = MAX(t1[1], MAX(t2[1], t3[1]));
    double dm = MAX(hi[0] - lo[0], hi[1] - lo[1]);
    p1[0] = (t1[0] - lo[0]) / dm;
    p1[1] = (t1[1] - lo[1]) / dm;
    p2[0] = (t2[0] - lo[0]) / dm;
    p2[1] = (t2[1] - lo[1]) / dm;
    p3[0] = (t3[0] - lo[0]) / dm;
    p3[1] = (t3[1] - lo[1]) / dm;
    p[0] = (t[0] - lo[0]) / dm;
    p[1] = (t[1] - lo[1]) / dm;

    //
    //  compute circumscribing radius
    //
    Point2D cc;

    double m[3][3];
    double a, bx, by, c;

    m[0][0] = p1[0];
    m[1][0] = p2[0];
    m[2][0] = p3[0];
    m[0][1] = p1[1];
    m[1][1] = p2[1];
    m[2][1] = p3[1];
    m[0][2] = 1.0;
    m[1][2] = 1.0;
    m[2][2] = 1.0;
    a = DetMat3X3(m);

    m[0][0] = p1[0] * p1[0] + p1[1] * p1[1];
    m[1][0] = p2[0] * p2[0] + p2[1] * p2[1];
    m[2][0] = p3[0] * p3[0] + p3[1] * p3[1];
    bx = DetMat3X3(m);

    m[0][1] = p1[0];
    m[1][1] = p2[0];
    m[2][1] = p3[0];
    by = DetMat3X3(m);

    m[0][2] = p1[1];
    m[1][2] = p2[1];
    m[2][2] = p3[1];
    c = DetMat3X3(m);

    cc = Point2D(0.5 * bx / a, -0.5 * by / a);

    double rc, rp;
    Vector2D vp;
    vp = Vector2D(cc, p);
    rp = vp.magnitude();

    rc = 0.5 * sqrt(bx * bx + by * by + 4.0 * a * c) / fabs(a);

    if (rp <= rc)
        flag = 1;

    return (flag);
}