Two players on one screen. How to prevent them to leave the screen?


So, I have isometric 3D game with 2 players.
Something like this:

And I want to block players movement on the sides of the screen.
How would be better to do that?

I thought about few ways:

  1. Clamp character’s position when both characters near screen edge. but it’s difficult to understand when characters near one or opposite screen edges.
  2. Add special blocking box primitive for every character on screen edge, and change it’s position with character’s positions, but again it needs many calculations with screen edges and character’s positions. + there is no any special collision for specified player character, so this primitive can block other characters.
  3. Create special blocking room around screen, scaled with camera position/FOV, which will block character’s movement. but again, here is trouble with collision for specified player pawns.

Any ideas? Maybe you know some very cheap and good way for this?

With the UDK I made a top down multiplayer shooter for the ios. I used 4 (moveable? dynamic?) blocking volumes that I arranged to make a square. I then moved them around with the camera. Worked well enough. I think had to work out the sliding plane for collision response and was a hacky mess. Hopefully you won’t need to do this.

I would see what I could do with the frustum first -

I am assuming both the players have the same viewtarget set. To find if any of the player is at the screens edge you could try converting their world position into screen position using this code

	//Code modified from function PlayerController::GetHitResultAtScreenPosition()
		ULocalPlayer* LocalPlayer = Cast<ULocalPlayer>(m_playerControl->Player);
		if (LocalPlayer != NULL && LocalPlayer->ViewportClient != NULL && LocalPlayer->ViewportClient->Viewport != NULL)
			// Create a view family for the game viewport
			FSceneViewFamilyContext ViewFamily(FSceneViewFamily::ConstructionValues(
			// Calculate a view where the player is to update the streaming from the players start location
			FVector ViewLocation;
			FRotator ViewRotation;
			FSceneView* SceneView = LocalPlayer->CalcSceneView(&ViewFamily, ViewLocation,  ViewRotation, LocalPlayer->ViewportClient->Viewport);

			if (SceneView)
				//Converts a world position to screen position			
                               SceneView->WorldToScreen(FVector(0, 0, 0)).ToString();

Replace the zero vector with your player position.You can get the screen resolution from usergamesettings

UGameUserSettings* UserSettings = GEngine->GetGameUserSettings();
FIntPoint res = UserSettings->GetScreenResolution();

or from the player controller using GetViewportSize(x, y) , but I think this works only in the editor. With this you can try restricting the player movement or better yet add a collision filter for boxes placed appropriately and attached to the camera that only block your player.