Example #1
0
	Vector2D Surface::GetTangentialVelocity( Vector2D velocityTowardsSurface )
	{
		/* We need to find the projection of the velocity vector in the direction of the surface vector. To start, we'll 
		   need to compute the surface vector in both directions. We'll use the dot product to find the angle between each 
		   of them, and use the one with the smaller angle for the remaining calculation, since we know the angle between 
		   surface and velocity should be between 0 and 90. */
		Vector2D surfaceVector1 = _surfaceEdge._point1 - _surfaceEdge._point2;
		Vector2D surfaceVector2 = _surfaceEdge._point2 - _surfaceEdge._point1;

		/* Use the dot product to get the angle between velocity and surface for each surface vector and save minimum angle */
		float angle1 = acos( ( velocityTowardsSurface * surfaceVector1 ) / ( velocityTowardsSurface.Magnitude() * surfaceVector1.Magnitude() ) );
		float angle2 = acos( ( velocityTowardsSurface * surfaceVector2 ) / ( velocityTowardsSurface.Magnitude() * surfaceVector2.Magnitude() ) );

		float finalAngle = 0;
		Vector2D projectionUnitVector;
		if( angle1 < angle2 )
		{
			finalAngle = angle1;
			projectionUnitVector = surfaceVector1.UnitVector();
		}
		else
		{
			finalAngle = angle2;
			projectionUnitVector = surfaceVector2.UnitVector();
		}
		
		float projectionMagnitude = velocityTowardsSurface.Magnitude() * cos( finalAngle );

		return projectionUnitVector * projectionMagnitude;
	}
Example #2
0
Vector2D Vector2D::Reflect(const Vector2D & v, const Vector2D &a) {
	Vector2D n = Normal(a);
	float co = -2 * ((float)v.Dot(n) / (n.Magnitude() * n.Magnitude()));
	Vector2D r = {};
	r.x = v.x + co * n.x;
	r.y = r.y + co * n.y;
	return r;
}
Example #3
0
TEST(vec2d, mag)
{
	Vector2D testingVector;
	testingVector.x = 3;
	testingVector.y = 8;
	float magTest = testingVector.Magnitude();
	EXPECT_FLOAT_EQ(8.54400374532, magTest);
}
Example #4
0
TEST(vec2d, norm)
{
	Vector2D testingVector = Vector2D();

	testingVector.x = 1;
	testingVector.y = 0;
	testingVector.Magnitude();
	testingVector.Normalize();
	EXPECT_FLOAT_EQ(1.f, testingVector.x);
	EXPECT_FLOAT_EQ(0.f, testingVector.y);
}
Example #5
0
float Vector2D::BetweenAngle(Vector2D &Vector){
	float fResult;
	fResult = DotProduct(Vector);
	fResult /= Magnitude() * Vector.Magnitude();
	return acos(fResult)*180/3.14f;
}
Example #6
0
void World::Update(const float time_delta) {
	mCurrentLevelTime += time_delta;
	// INPUT!
	const sf::Input& in = GameApp::get_mutable_instance().GetInput();
	sf::View& view = GameApp::get_mutable_instance().GetView();
	if(GameApp::get_mutable_instance().IsEditorMode()) {
		float px = 10;
		if(in.IsKeyDown(sf::Key::LShift) || in.IsKeyDown(sf::Key::RShift)) {
			px *= 5;
		}
		if(in.IsKeyDown(sf::Key::Up)) {
			view.SetCenter(view.GetCenter().x, view.GetCenter().y - px);
		}
		if(in.IsKeyDown(sf::Key::Down)) {
			view.SetCenter(view.GetCenter().x, view.GetCenter().y + px);
		}
		if(in.IsKeyDown(sf::Key::Left)) {
			view.SetCenter(view.GetCenter().x - px, view.GetCenter().y);
		}
		if(in.IsKeyDown(sf::Key::Right)) {
			view.SetCenter(view.GetCenter().x + px, view.GetCenter().y);
		}

		Coordinates mp;
		mp.SetScreenPixel(GameApp::get_mutable_instance().GetMousePosition());
		if(mEditorMouseAction == EMA_GRAB) {
			mEditorMouseActionEntity->SetPosition( mEditorMouseActionStartEntityPosition.GetWorldFloat() +
												   mp.GetWorldFloat() -
												   mEditorMouseActionStartMousePosition.GetWorldFloat() );
		} else if(mEditorMouseAction == EMA_ALPHA) {
			float mpf = mp.GetScreenFloat().y;
			float mspf = mEditorMouseActionStartMousePosition.GetScreenFloat().y;
			float d = (mspf - mpf) * 5.f;
			float a = mEditorMouseActionStartEntityAlpha + d;
			if (a > 1) a = 1;
			if (a < 0) a = 0;
			mEditorMouseActionEntity->SetAlpha( a );
		} else if(mEditorMouseAction == EMA_SCALE) {
			Vector2D md = mEditorMouseActionStartEntityPosition.GetScreenPixel() - mp.GetScreenPixel();
			Vector2D sd = mEditorMouseActionStartEntityPosition.GetScreenPixel() - mEditorMouseActionStartMousePosition.GetScreenPixel();
			float f =  md.Magnitude() / sd.Magnitude();
			mEditorMouseActionEntity->SetScale( mEditorMouseActionStartEntityScale * f );
		} else if(mEditorMouseAction == EMA_ROTATE) {
			Coordinates ep;
			ep.SetWorldFloat(mEditorMouseActionEntity->GetPosition());
			mEditorMouseActionEntity->SetRotation(
					(mEditorMouseActionStartMousePosition.GetWorldFloat()-ep.GetWorldFloat()).Rotation()
					-(mp.GetWorldFloat()-ep.GetWorldFloat()).Rotation()
					+ mEditorMouseActionStartEntityRotation );
		}
	} else if(GameApp::get_mutable_instance().GetAppMode() == AM_PUZZLE) {
		// draw point on closest rail
		Rail* r = GetClosestRail();
		if(r != NULL) {
			Coordinates tmp;
			tmp.SetScreenPixel(GameApp::get_mutable_instance().GetMousePosition());
			float d = r->ClosestPositionOnLine(tmp.GetWorldFloat());
			mClosestRailPoint = r->GetPointFromFloat(d);
		}
		mClosestRail = r;

	}
	if(GetBoxEntity() != NULL && GetBoxEntity()->UsesPhysics() && !GameApp::get_mutable_instance().GetInput().IsMouseButtonDown(sf::Mouse::Left)) {
		mCurrentRail = GetClosestRail(true, GetBoxEntity()->GetBody()->getWorldTransform().getOrigin());
	}

	//mDynamicsWorld->stepSimulation(time_delta, 10);
	if(GameApp::get_mutable_instance().GetAppMode() == AM_PLAY) {
		mDynamicsWorld->stepSimulation(1 / 60.f, 10);
		mDynamicsWorld->clearForces();
	}

	Entity* c = GetClosestEntityOnLayer(GameApp::get_mutable_instance().GetMousePosition(), mEditorLayer);
	BOOST_FOREACH(Entity& entity, mEntities) {
		int h = 0;
		if (mEditorSelectedEntity == &entity) h = 2;
		else if (c == &entity) h = 1;
		entity.SetHighlightMode(h);
		entity.Update(time_delta);
		/*if(entity.GetLifeTime() >= entity.GetTimeToLive()) {
			mDynamicsWorld->removeRigidBody(entity.GetBody().get());
			mEntities.erase_if(boost::bind(&Entity::GetUID, _1) == entity.GetEntityUniqueId());
		}*/
	}