Get Mouse Entity

From CryWiki

Get Mouse Entity
Jump to: navigation, search



C++ Sample Walkthrough

This function returns the EntityId of the entity under the mouse cursor. To utilize this, be sure to actually show the mouse.


C++

EntityId GetMouseEntityID()
{
	static ray_hit hit;
	float x, y, xMouse, yMouse, zMouse = 0.0f;
 
	if(!gEnv->pHardwareMouse || !gEnv->pRenderer || !gEnv->p3DEngine || !gEnv->pSystem 
                                || !gEnv->pEntitySystem || !g_pGame->GetIGameFramework())
		return 0;
 
	IActor *pClientActor = g_pGame->GetIGameFramework()->GetClientActor();
 
	if (!pClientActor)
		return 0;
 
	gEnv->pHardwareMouse->GetHardwareMouseClientPosition(&x,&y);
	y = gEnv->pRenderer->GetHeight() - y;
 
	gEnv->pRenderer->UnProjectFromScreen(x, y, 0.0f,&xMouse,&yMouse,&zMouse);
 
	static const unsigned int flags = rwi_stop_at_pierceable|rwi_colltype_any;
 
	float  fRange = gEnv->p3DEngine->GetMaxViewDistance();
	Vec3  vCamPos = gEnv->pSystem->GetViewCamera().GetPosition();
	Vec3     vDir = (Vec3(xMouse,yMouse,zMouse) - vCamPos).GetNormalizedSafe();
 
	IPhysicalEntity *pPhysicalEnt = pClientActor->GetEntity() ? pClientActor->GetEntity()->GetPhysics() : NULL;
 
	if(!pPhysicalEnt)
		return 0;
 
	if (gEnv->pPhysicalWorld && 
         gEnv->pPhysicalWorld->RayWorldIntersection(vCamPos, vDir * fRange, ent_all, flags, &hit, 1, pPhysicalEnt))
	{
		if (gEnv->p3DEngine->RefineRayHit(&hit, vDir * fRange))
		{	
			if (IEntity *pEntity = gEnv->pEntitySystem->GetEntityFromPhysics(hit.pCollider))
			{
				return pEntity->GetId();
			}
		}	
	}	
 
	return 0;
}

About this Sample

Name

Get mouse entity

Created by

James-Ryan

Description

Gets the entity which is under the mouse cursor. Think of it as a Real-Time Strategy select system.

» View other C++ Samples

» Return to Crysis Modding Code Samples

Personal tools