Example #1
0
double Vector3D::AngleBetween(Vector3D &vector, Vector3D &axis) {
	double angle = AngleBetween(vector);
	Vector3D cross = Cross(vector);
	if (cross.Dot(axis) < 0.0)
		angle *= -1.0;

	return angle;
}
	void ContactResolver::UpdateBodyPenetration(
		RigidBody* body, Contact& contact, int sign, 
		const Vector3D<Real>& relativePos, const Vector3D<Real>& linearChange, 
		const Vector3D<Real>& angularChange)
	{
		UNUSED(body)
		Vector3D<Real> deltaPosition = linearChange + angularChange.CrossProduct(relativePos);
		contact.m_penetration += deltaPosition.Dot(contact.m_contactNormal) * sign;
	}
//
//  Helper.
//  Find the point of intersection of the plane defined by the point p
//  & the normal vector v with the line segment defined by the ordered
//  point pair p1 & p2. If the intersection is within the segment returns
//  true and sets pi to the intersection point.
//
bool FieldView::Intersect(const Point3D& p, const Vector3D& n,
                          const Point3D& p1, const Point3D& p2, Point3D& pi)
{
  Vector3D w = p1 - p;
  Vector3D u = p2 - p1;
  double angle = n.Dot(u);
  if (angle == 0) {
    return false;
  }
  float lambda = -n.Dot(w)/angle;
  if (lambda < 0.0) {
    return false;
  }
  if (lambda > 1.0) {
    return false;
  }
  pi = p1 + u * lambda;
  return true;
}
Example #4
0
bool Triangle2::rayIntersect(const Vector3D &origin, const Vector3D &direction, float &u, float &v, Vector3D &intersect)
{
    // TODO: Make it work for polygon (tri/quad).
    
    Vector3D v0 = vertex(0)->data().position;
    Vector3D v1 = vertex(1)->data().position;
    Vector3D v2 = vertex(2)->data().position;
    
    Vector3D e1 = v1 - v0;
    Vector3D e2 = v2 - v0;
    Vector3D p = direction.Cross(e2);
    float a = e1.Dot(p);
    
    if (fabsf(a) < FLOAT_EPS)
        return false;
    
    float f = 1.0f / a;
    
    Vector3D s = origin - v0;
    u = f * s.Dot(p);
    if (u < 0.0f || u > 1.0f)
        return false;
    
    Vector3D q = s.Cross(e1);
    v = f * direction.Dot(q);
    if (v < 0.0f || u + v > 1.0f)
        return false;
    
    float t = f * e2.Dot(q);
    if (t >= 0.0f)
    {
        intersect = v0 + e1 * u + e2 * v;
        return true;
    }
    return false;
}
Example #5
0
void main()
{
	printf_s("\nDefault Constructor");
	printf_s("\nthisVector -> ");
	Vector3D vectorDefaultConstructor = Vector3D();
	vectorDefaultConstructor.Print();

	printf_s("\nConstructor with floats");
	printf_s("\nthisVector -> ");
	Vector3D thisVector = Vector3D(1, 2, 3);
	thisVector.Print();

	printf_s("\nConstructor with floats");
	printf_s("\notherVector -> ");
	Vector3D otherVector = Vector3D(2, 3, 4);
	otherVector.Print();

	printf_s("\nOperator +=");
	printf_s("\nthisVector += otherVector -> ");
	thisVector += otherVector;
	thisVector.Print();

	printf_s("\nOperator -=");
	printf_s("\nthisVector -= otherVector -> ");
	thisVector -= otherVector;
	thisVector.Print();

	printf_s("\nReset thisVector");
	printf_s("\nthisVector -> ");
	thisVector = Vector3D(1, 2, 3);
	thisVector.Print();
	printf_s("otherVector -> ");
	otherVector.Print();

	printf_s("\nOperator *=");
	printf_s("\nthisVector *= 3 -> ");
	thisVector *= 3;
	thisVector.Print();

	printf_s("\nOperator /=");
	printf_s("\nthisVector /= 3 -> ");
	thisVector /= 3;
	thisVector.Print();

	printf_s("\nOperator =");
	printf_s("\nthisVector = otherVector -> ");
	thisVector = otherVector;
	thisVector.Print();

	printf_s("\nOperator unary -");
	printf_s("\n-thisVector -> ");
	(-thisVector).Print();

	printf_s("\nReset thisVector");
	printf_s("\nthisVector -> ");
	thisVector = Vector3D(1, 2, 3);
	thisVector.Print();
	printf_s("otherVector -> ");
	otherVector.Print();

	printf_s("\nOperator +");
	printf_s("\nthisVector + otherVector -> ");
	(thisVector + otherVector).Print();

	printf_s("\nOperator -");
	printf_s("\nthisVector - otherVector -> ");
	(thisVector - otherVector).Print();

	printf_s("\nOperator *");
	printf_s("\nthisVector * 3 -> ");
	(thisVector * 3).Print();

	printf_s("\nOperator /");
	printf_s("\nthisVector / 3 -> ");
	(thisVector / 3).Print();

	printf_s("\nReset thisVector");
	printf_s("\nthisVector -> ");
	thisVector = Vector3D(1, 2, 3);
	thisVector.Print();
	printf_s("otherVector -> ");
	otherVector.Print();

	printf_s("\nOperator ==");
	printf_s("\nthisVector == otherVector -> ");
	
	if (thisVector == otherVector)
	{
		printf_s("True\n");
	}

	else
	{
		printf_s("False\n");
	}

	printf_s("\nOperator !=");
	printf_s("\nthisVector != otherVector -> ");

	if (thisVector != otherVector)
	{
		printf_s("True\n");
	}

	else
	{
		printf_s("False\n");
	}

	printf_s("\nNormalize");
	printf_s("\nthisVector -> ");
	thisVector.Normalize();
	thisVector.Print();

	printf_s("\nDot Product");
	printf_s("\nthisVector(dot)otherVector -> %.2f\n", thisVector.Dot(otherVector));

	printf_s("\nCross Product");
	printf_s("\nthisVector(cross)otherVector -> ");
	(thisVector.Cross(otherVector)).Print();

	getchar();
}
Example #6
0
		// -----------------------------------------------------------------------------
		Vector3D Vector3D::ProjectOn( const Vector3D& rhs ) const
		{
			return (*this) * (this->Dot(rhs) / rhs.Dot(rhs));
		}
void Cube::Render(HDC targetDC,
	const Matrix& view,
	const Matrix& projection,
	const Matrix& viewport)
{
	Matrix viewProj = world * view * projection;

	for (int i = 0; i < TRIANGLE_COUNT; ++i)
	{
		Vector3D v1 = 
			vertex[triangles[i].vertexIndex[0]];
		Vector3D v2 =
			vertex[triangles[i].vertexIndex[1]];
		Vector3D v3 =
			vertex[triangles[i].vertexIndex[2]];

		//분리 필요
		/*v1 = viewProj * viewport * v1;
		v2 = viewProj * viewport * v2;
		v3 = viewProj * viewport * v3;*/

		//뷰 메트릭스와 프로젝션 메트릭스 까지만 곱함
		v1 = viewProj * v1;
		v2 = viewProj * v2;
		v3 = viewProj * v3;

		//노멀 벡터 구하기
		Vector3D v1to2 = v2 - v1;
		Vector3D v1to3 = v3 - v1;
		Vector3D normal = v1to2.Cross(v1to3);
		if (normal.Dot(Vector3D(0, 0, 1)) > 0)	//90도 보다 작으므로 컬링
		{
			continue;
		}

		//뷰포트 메트릭스 적용하기
		v1 = viewport * v1;
		v2 = viewport * v2;
		v3 = viewport * v3;

		MoveToEx(targetDC, (int)v1.x, (int)v1.y, nullptr);
		LineTo(targetDC, (int)v2.x, (int)v2.y);
		LineTo(targetDC, (int)v3.x, (int)v3.y);
		LineTo(targetDC, (int)v1.x, (int)v1.y);
	}

	Matrix pipeline = viewProj * viewport;
	for (auto iter = lines.cbegin(); iter != lines.cend(); ++iter)
	{
		Vector3D start = (*iter).start;
		Vector3D end = (*iter).end;

		start = pipeline * start;
		end = pipeline * end;

		COLORREF oldColor = SetDCPenColor(targetDC, (*iter).color);
		MoveToEx(targetDC, (int)start.x, (int)start.y, nullptr);
		LineTo(targetDC, (int)end.x, (int)end.y);
		SetDCPenColor(targetDC, oldColor);
	}
}
Example #8
0
bool ExitingMethod1(VPlacedVolume const *pvol, Vector3D<Precision> const &point, Vector3D<Precision> const &dir)
{
  Vector3D<Precision> normal;
  bool valid = pvol->Normal(point, normal);
  return valid && normal.Dot(dir) > 0;
}