Esempio n. 1
0
// Wat..
void RenderHands(CVPipeline * pipe)
{
	List<CVHand> & hands = pipe->hands;
//	std::vector<std::vector<cv::Point> > c;
	for(int i = 0; i < hands.Size(); i++)
	{
		CVHand & hand = hands[i];

		if (hand.contour && hand.contour->segments.Size())
			RenderContourSegments(hand.contour, pipe);
		else
			RenderContours(pipe);
	

	//	assert(hand.contour);
	//	c.clear();
	//	c.push_back(hand.contour->cvPointList);
		cv::circle(pipe->output, cv::Point(hand.center.x, hand.center.y), 20, cv::Scalar(0, 0, 255), 2);
		int fingersSize = hand.fingers.Size();
		for(int j = 0; j < fingersSize; j++)
		{
#define VEC3FTOCVPOINT(a) (cv::Point(a.x,a.y))
			CVFinger & finger = hand.fingers[j];
			Vector3f fingerPoint = finger.point;
			Vector4f fingerColor = finger.GetColor();
#define VECTOCVSCALAR(a) (cv::Scalar(a.z,a.y,a.x))
			cv::circle(pipe->output, cv::Point(fingerPoint.x, fingerPoint.y), 10, VECTOCVSCALAR(fingerColor), 2);
			cv::line(pipe->output, VEC3FTOCVPOINT(hand.center), VEC3FTOCVPOINT(fingerPoint), VECTOCVSCALAR(fingerColor), 4);

			Vector3f start = finger.start;
			if (start.MaxPart())
				cv::circle(pipe->output, cv::Point(start.x, start.y), 5, cv::Scalar(255,0,125), 2);
			Vector3f stop = finger.stop;
			if (stop.MaxPart())
				cv::circle(pipe->output, cv::Point(stop.x, stop.y), 5, cv::Scalar(0,255,255), 2);

			// Render each parallel pair in some colored circle or line?
			for (int i = 0; i < finger.parallelPairs.Size(); ++i)
			{
				CVContourSegmentPair & csp = finger.parallelPairs[i];
				// Parallel point.
				Vector3f pp = csp.one.rawInputStart, pp2 = csp.two.rawInputStart;
				cv::circle(pipe->output, cv::Point(pp.x, pp.y), 5, cv::Scalar(155,255,155), 2);
				cv::circle(pipe->output, cv::Point(pp2.x, pp2.y), 5, cv::Scalar(155,255,255), 2);
			}

		}

		// Render approximate velocity.
		Vector3f vel = hand.approximateVelocityFromOpticalFlow;
		Vector3f colorVel = vel;
		colorVel.y *= -1;
		Vector4f color = GetColorForDirection(colorVel.NormalizedCopy());
		// Paint it.
		cv::line(pipe->output, VectorToCVPoint(hand.center), VectorToCVPoint(hand.center + vel * 3), 
			VectorToCVScalarColor(color), 5);

		//		std::cout<<"\nfingers drawn: "<<hand.fingers.Size();
	}
}
/** Checks states via InputManager. Regular key-bindings should probably still be defined in the main game state 
	and then passed on as messages to the character with inputFocus turned on.
*/
void FirstPersonPlayerProperty::ProcessInput()
{
	// Skip if CTLR is pressed, should be some other binding then.
	if (InputMan.KeyPressed(KEY::CTRL) || InputMan.KeyPressed(KEY::ALT))
		return;
	forward = 0.f;
	// Should probably check some lexicon of key-bindings here too. or?
	if (InputMan.KeyPressed(KEY::W))
		forward -= 1.f;
	if (InputMan.KeyPressed(KEY::S))
		forward += 1.f;
	right = 0.f;
	if (InputMan.KeyPressed(KEY::A))
		right -= 1.f;
	if (InputMan.KeyPressed(KEY::D))
		right += 1.f;

	/// o.o
	if (InputMan.KeyPressedThisFrame(KEY::R))
	{
		ToggleAutorun();
	}

	if (InputMan.KeyPressed(KEY::SPACE))
	{
		if (!jumping)
		{
			// Jump!
			PhysicsQueue.Add(new PMApplyImpulse(owner, Vector3f(0,jumpSpeed,0), Vector3f()));
			lastJump = Time::Now();
			jumping = true;
			PhysicsQueue.Add(new PMSetEntity(owner, PT_ACCELERATION, Vector3f()));
			/// Cancel auto run as well.
			PhysicsQueue.Add(new PMSetEntity(owner, PT_RELATIVE_ACCELERATION, Vector3f()));
		}
	}

	forward *= movementSpeed;

	float rotationSpeed = 1.2f;
	right *= rotationSpeed;

	Vector3f acc;
	acc[2] = forward;

//	Vector3f rot;
//	rot[1] = right;

	// 
	// Auto-running,.
	if (autorun)
	{
		if (lastAcc != acc)
		{
		}
		if (right != lastRight)
		{
			// Rotate int Y..
			Quaternion q = Quaternion(Vector3f(0,1,0), right);
			PhysicsQueue.Add(new PMSetEntity(owner, PT_ROTATIONAL_VELOCITY, q));
			lastRight = right;
		}
	}
			
	/// o-o cameraaaa focsuuuuuu!
	if (owner->cameraFocus)
	{
		
		// Check mouse position.
		if (raycast)
			UpdateTargetsByCursorPosition();


		// Free-form running (relative to camera)
		if (!autorun)
		{
			/// Get camera transform.
			Camera * camera = owner->cameraFocus;
			if (!camera)
				return;
			Vector3f camLookAt = camera->LookingAt();
			Vector3f forwardVector = -forward * camLookAt;
			forwardVector.Normalize();
			Vector3f rightwardVector = -right * camera->LeftVector();
			rightwardVector.Normalize();
			Vector3f newVelocity = forwardVector + rightwardVector;
			// Remove Y-component.
			newVelocity[1] = 0;
			Vector3f normalizedVelocity = newVelocity.NormalizedCopy();
			// Multiply movement speed.
			newVelocity = normalizedVelocity * movementSpeed;
			UpdateVelocity(newVelocity);
		}


		/// Make sure the camera is rotating around the center of the entity. <- wat.
		float height = 1.7f;
		if (owner->cameraFocus->relativePosition[1] != height)
		{
			GraphicsQueue.Add(new GMSetCamera(owner->cameraFocus, CT_RELATIVE_POSITION_Y, height));
			GraphicsQueue.Add(new GMSetCamera(owner->cameraFocus, CT_TRACKING_POSITION_OFFSET, Vector3f(0,height,0)));
		}
		/// Camera Control, Booyakasha!
		float cameraRight = 0.f;
		if (InputMan.KeyPressed(KEY::LEFT))
			cameraRight += 1.f;
		if (InputMan.KeyPressed(KEY::RIGHT))
			cameraRight -= 1.f;

		// Set it! :D
		static float pastCameraRight = 0.f;
		if (cameraRight != pastCameraRight)
		{
			GraphicsQueue.Add(new GMSetCamera(owner->cameraFocus, CT_ROTATION_SPEED_YAW, -cameraRight));
			pastCameraRight = cameraRight;
		}

		/// Camera updown
		float cameraUp = 0.f;
		if (InputMan.KeyPressed(KEY::UP))
			cameraUp += 1.f;
		if (InputMan.KeyPressed(KEY::DOWN))
			cameraUp -= 1.f;
		static float pastCameraUp = 0.f;
		if (cameraUp != pastCameraUp)
		{
			GraphicsQueue.Add(new GMSetCamera(owner->cameraFocus, CT_ROTATION_SPEED_PITCH, -cameraUp)); 
			pastCameraUp = cameraUp;
		}


		float cameraZoom = 0.f;
		float cameraZoomMultiplier = 1.00f;
#define CONSTANT_ZOOM_SPEED 2.2f
#define ZOOM_MULTIPLIER_SPEED 1.5f
		if (InputMan.KeyPressed(KEY::PG_DOWN))
		{
			cameraZoomMultiplier *= ZOOM_MULTIPLIER_SPEED;
			cameraZoom = CONSTANT_ZOOM_SPEED;
		}
		if (InputMan.KeyPressed(KEY::PG_UP))
		{
			cameraZoomMultiplier /= ZOOM_MULTIPLIER_SPEED;
			cameraZoom = - CONSTANT_ZOOM_SPEED;
		}
		static float pastCameraZoom = 1.f;
		if (cameraZoom != pastCameraZoom)
		{
			GraphicsQueue.Add(new GMSetCamera(owner->cameraFocus, CT_DISTANCE_FROM_CENTER_OF_MOVEMENT_SPEED, cameraZoom));
			GraphicsQueue.Add(new GMSetCamera(owner->cameraFocus, CT_DISTANCE_FROM_CENTER_OF_MOVEMENT_SPEED_MULTIPLIER, cameraZoomMultiplier));
			pastCameraZoom = cameraZoom;
		}
		float cameraTurn = 0.f;
		if (InputMan.KeyPressed(KEY::LEFT))
			cameraTurn += 1.f;
		if (InputMan.KeyPressed(KEY::RIGHT))
			cameraTurn += -1;
		static float pastCameraTurn = 0.f;
		if (cameraTurn != pastCameraTurn)
		{
			cameraTurn *= 2.f;
			pastCameraTurn = cameraTurn;
			GraphicsQueue.Add(new GMSetCamera(owner->cameraFocus, CT_ROTATION_SPEED_YAW, cameraTurn));
		}
	}
}
Esempio n. 3
0
/** All entities sent here should be fully dynamic! 
	Kinematic ones may or may not work (consider adding own integration function).
*/
void SRIntegrator::IntegrateDynamicEntities(List<Entity*> & dynamicEntities, float timeInSeconds)
{
	FirstPersonIntegrator::IntegrateDynamicEntities(dynamicEntities, timeInSeconds);
	for (int i = 0; i < dynamicEntities.Size(); ++i)
	{
		// check for magnetics.
		Entity * entity = dynamicEntities[i];
		if (entity->physics->state & CollisionState::IN_REST)
			continue;
		MagneticProperty * magProp = entity->GetProperty<MagneticProperty>();
		if (magProp)
		{
			// Rotate a bit.
			// Raycast to floor on location, at forward + 1 and side + 1, then rotate so that the ship is flat to the level.
			Vector3f upVec = entity->rotationMatrix.GetColumn(1),
				forwardVec = -entity->rotationMatrix.GetColumn(2),
				rightVec = entity->rotationMatrix.GetColumn(0);
			/// New rolling, check nearest points of track.
			TrackPoint * tp = track->NearestPoint(entity->worldPosition);
			if (!tp || tp->next == 0)
				continue;
			TrackPoint * next = tp->next->next;
			if (!next)
				continue;
			
			/// Desired forward, and up-right vectors of the track.
			Vector3f desForward = (next->pos - tp->pos).NormalizedCopy(),
				desUp = tp->up,
				desRight = tp->right;

			float desForwardDotCurrForward = desForward.NormalizedCopy().DotProduct(forwardVec);
			Vector3f desForwardInCurrDir = desForwardDotCurrForward * desForward; // Less than 1			
			float forwardDotDesUp = forwardVec.DotProduct(desUp);

			// Pitch down or up?
			Vector3f forwardProjected = desForwardDotCurrForward * desForward;
			Vector3f diffForwardCurrForward = forwardVec - desForward;
			/// Pitch needed?
			float pitchNeeded = diffForwardCurrForward.DotProduct(upVec);
//			if (AbsoluteValue(pitchNeeded) > 0.1f)
	//			std::cout<<"\nPitch needed: "<<pitchNeeded;
			if (desForwardDotCurrForward > 0)
				pitchNeeded *= -1;
			// Rotate slightly around side-vector.
			entity->physics->angularVelocity  -= rightVec * pitchNeeded * timeInSeconds * 0.2f;
			
			/// Barrel-roll.
			Vector3f diffUpCurrUp = upVec - desUp;
			float barrelNeeded = diffUpCurrUp.DotProduct(rightVec);
	//		if (AbsoluteValue(barrelNeeded) > 0.1f)
	//			std::cout<<"\nBarrel needed: "<<barrelNeeded;
			entity->physics->angularVelocity += forwardVec * barrelNeeded * timeInSeconds * 0.1f;

			/// Add some velocity towards the next and previous node, acting as magnetism down towards the field?
			entity->physics->velocity -= desUp * timeInSeconds * 5.f + (entity->worldPosition - tp->pos).NormalizedCopy() * 5.f * timeInSeconds;

			/// Old rolling
			/*
			Ray ray(entity->worldPosition, -upVec); // 0 - side, 1 - up, 2 - forward
			ray.collisionFilter = CC_TRACK;
			List<Intersection> iSecs = Physics.Raycast(ray);
			float distBelow = iSecs.Size()? iSecs[0].distance : 0.f;
			// Ray again.
			ray.start += forwardVec;
			iSecs = PhysicsMan.Raycast(ray);
			float distInFront = iSecs.Size()? iSecs[0].distance : 0.f;
			ray.start = entity->worldPosition + rightVec;
			iSecs = PhysicsMan.Raycast(ray);
			float distSide = iSecs.Size()? iSecs[0].distance : 0.f;

			float distPitch = distInFront - distBelow;
			float distRoll = distSide - distBelow;

			// Rotate accordingly for pitch.
			if (distInFront && distBelow && AbsoluteValue(distPitch) < 2.f && distBelow < 5.f)
			{
				// Rotate slightly around side-vector.
				entity->physics->angularVelocity  += rightVec * distPitch * timeInSeconds * 0.2f;
			}
			// Rotate accordingly for roll.
			if (distSide && distBelow && AbsoluteValue(distRoll) < 2.f  && distBelow < 5.f)
			{
				// Rotate slightly around side-vector.
				entity->physics->angularVelocity  -= forwardVec * distRoll * timeInSeconds * 0.1f;
			}


			/// If not above anything, rotate to level-plane.
			if (distBelow == 0)
			{
				magProp->timeOutsideTrack += timeInSeconds;
				if (magProp->timeOutsideTrack > 2.f)
				{
					/// If nothing detected, rotate so that the ship will be level again, by rolling?
					if (forwardVec.y > 0.2f)
					{
						entity->physics->angularVelocity += forwardVec.y * rightVec * timeInSeconds * 0.1f;
					}
					/// Right-vec not aligned with XZ plane? Roll until it is.
					if (AbsoluteValue(rightVec.y) > 0.2f)
					{
						entity->physics->angularVelocity += rightVec.y * forwardVec * timeInSeconds * 0.1f;	
					}
					if (upVec.y < 0.8f)
					{
						// Barrel-roll up.
						entity->physics->angularVelocity += (-1.f + upVec.y) * forwardVec * timeInSeconds * 0.1f;	
					}
				}
			}
			else 
			{
				magProp->timeOutsideTrack = 0;
			}
			*/

			// Same for roll.

			// If not close to a surface... rotate to.. stuff?
//			entity->physics->angularVelocity.x = 0.00f;
		}
	}
}