Exemplo n.º 1
0
void Particle::advance(int phase){

    if(active()) {
        QVector2D force;
        foreach(Planet* planet, gameScene()->planets()) {
            QVector2D r = QVector2D(this->position() - planet->position());
            // save the length and lengthSquared to memory to avoid recalculations (with square roots!)
    //        double distance = r.length();
            double distanceSquared = r.lengthSquared();
            QVector2D rn = r.normalized();
            if(distanceSquared != 0) {
                QVector2D gravity = - rn * GravityConstant * planet->mass() / distanceSquared;
                force += gravity;

                if (r.lengthSquared() < planet->radius()*planet->radius()) { //Vj: Temporary fix while testing :O)
                    planet->collideWithParticle();
                    gameScene()->removeParticle(this);
                    return;
                }
            }
        }
        QVector2D acceleration = force / 1.0;
        _velocity += acceleration * gameScene()->dt();
        _position += _velocity * gameScene()->dt();
        resized();
    }
Exemplo n.º 2
0
float Window::projection_on_curve(const QVector2D& projected) {
	//This is the distance where the curves changed in terms of the window size
	const float radius = 0.8f;
	float z = 0.0f;
	if (projected.lengthSquared() <= (0.5f * radius * radius)) {
		//Inside the sphere
		z = sqrt(radius * radius - projected.lengthSquared());
	}
	else {
		//Outside of the sphere using hyperbolic sheet
		z = (0.5f * radius * radius) / projected.length();
	}
	return z;
}
Exemplo n.º 3
0
/**
* Computes the intersection between two line segments on the XY plane
* Segments must intersect within their extents for the intersection to be valid
* z coordinate is ignored
**/
bool Polygon3D::segmentSegmentIntersectXY(QVector2D &a, QVector2D &b, QVector2D &c, QVector2D &d,
	float *tab, float *tcd, bool segmentOnly, QVector2D &intPoint)
{
	QVector2D u = b - a;
	QVector2D v = d - c;

	if( u.lengthSquared() < MTC_FLOAT_TOL  ||  v.lengthSquared() < MTC_FLOAT_TOL )
	{
		return false;
	}

	float numer = v.x()*(c.y()-a.y()) + v.y()*(a.x()-c.x());
	float denom = u.y()*v.x() - u.x()*v.y();

	if (denom == 0.0f)  {
		// they are parallel
		*tab = 0.0f;
		*tcd = 0.0f;
		return false;
	}

	float t0 = numer / denom;

	QVector2D ipt = a + t0*u;
	QVector2D tmp = ipt - c;
	float t1;
	if (QVector2D::dotProduct(tmp, v) > 0.0f){
		t1 = tmp.length() / v.length();
	}
	else {
		t1 = -1.0f * tmp.length() / v.length();
	}

	//Check if intersection is within segments
	if( !( (t0 >= MTC_FLOAT_TOL) && (t0 <= 1.0f-MTC_FLOAT_TOL) && (t1 >= MTC_FLOAT_TOL) && (t1 <= 1.0f-MTC_FLOAT_TOL) ) ){
		return false;
	}

	*tab = t0;
	*tcd = t1;
	QVector2D dirVec = b-a;

	intPoint = a+(*tab)*dirVec;
	return true;
}
Exemplo n.º 4
0
QVector2D NavigationMath::rayCircleIntersection(
	bool & valid
,	const QVector2D & origin
,	const QVector2D & ray
,	const float radius)
{
	if(ray.isNull())
	{
		valid = false;
		return QVector2D();
	}

	const float a = ray.lengthSquared();
	const float b = 2. * QVector2D::dotProduct(ray, origin);
    const float c = origin.lengthSquared() - static_cast<float>(radius * radius);

	const float delta = b * b - 4. * a * c;

	if (delta < 0.0) 
	{
		valid = false;
		return QVector2D();
	}

	const float s = sqrt(delta);

	// the two intersections
	const float t0 = (-b + s) / (2.0 * a);
	const float t1 = (-b - s) / (2.0 * a);

	// nearest one
	const float t = qMin<float>(t0, t1);

	valid = true;
	return t * ray + origin;
}
Exemplo n.º 5
0
static QList<int> bounce(QPoint & p0, QVector2D vel, QSize size, const QRectList & rectsIn)
{
  QList<int> touched;
#if 0
  QList<QPair<int, QRect> > rects;
  QRect swept = QRect(p0, size).united(QRect(movedBy(p0, vel), size));
  for (int i = 0; i < rectsIn.count(); ++i)
    if (rectsIn.at(i).intersects(swept)) rects << qMakePair(i, rectsIn.at(i));
  QVector2D p(p0.x(), p0.y());
  QVector2D d(td.manhattanNormalize(vel));
  while (vel.lengthSquared() > d.lengthSquared()) {

  }

#endif
  return touched;
}