TorqueX PSK : Camera Movement Limits Determined By Current Player Position?
by AWannaBe · in Torque X Platformer Kit · 03/09/2010 (5:28 pm) · 3 replies
Hello all!
Let me just say kudos to the makers of the PSK and to GarageGames for a fantastic engine to work with.
I've recently picked up the TorqueX PSK which I purchased last year and have revived the project for which I had originally purchased the kit. I'm currently working on adding project specific functionality.
One such function is the ability for the player to move the camera around to allow greater flexibility for the player to determine what's ahead and hopefully make better decisions about how to proceed.
I've implemented the ability for the user to maneuver the camera via analog stick and a toggle button, and to then have it reset back to the player's position when the toggle is lifted or the analog stick returns back to the resting position as well (X:0Y:).
I did this via the following:
PlayerController.cs : Private Members
PlayerController.cs : ProcessTick(Move, float)
PlayerController.cs : _MoveCamera()
PlayerController.cs : _setupInputMap()
This works great on the Xbox360, I can press the right bumper and then move the right analog stick around and the camera does what I expect in terms of movement.
Now I want to be able to limit the movement of the camera so that it can only be moved to the extent that the current player is still visible on the screen.
To attempt to do this I added the following member and method to the PlayerController.cs file:
I've tried various ways of calculating whether the camera is moving outside the range of the current player but so far they have a much greater extent than I anticipated, and worse than that there starts to exist a horrible redrawing artifact. I believe the redrawing artifacts are just simply a caculation problem and the screen is actually bouncing between to locations to draw due to my woefully out of date math skills. ;)
So to summarize I believe there actually two questions here:
Sorry for the length of the post and thanks in advance for any ideas!
Let me just say kudos to the makers of the PSK and to GarageGames for a fantastic engine to work with.
I've recently picked up the TorqueX PSK which I purchased last year and have revived the project for which I had originally purchased the kit. I'm currently working on adding project specific functionality.
One such function is the ability for the player to move the camera around to allow greater flexibility for the player to determine what's ahead and hopefully make better decisions about how to proceed.
I've implemented the ability for the user to maneuver the camera via analog stick and a toggle button, and to then have it reset back to the player's position when the toggle is lifted or the analog stick returns back to the resting position as well (X:0Y:).
I did this via the following:
PlayerController.cs : Private Members
private bool _resetCamera;
private bool _shiftCamera;
private float _camX;
private float _camY;PlayerController.cs : ProcessTick(Move, float)
// Check to see if the player has pressed the assigned camera shift button.
if (move.Buttons[1].Pushed)
_shiftCamera = true;
else
_shiftCamera = false;
if (_shiftCamera)
{
// Try and shift the camera.
// We know that we don't want to start out by resetting the camera so make sure that is set to false.
_resetCamera = false;
// The player has moved the right analog stick along the axis (X) in either a positive [RIGHT] or negative [LEFT] direction.
if (move.Sticks[1].X != 0)
{
if (_playerInView)
_camX += move.Sticks[1].X;
}
// The player has moved the right analog stick along the axis (Y) in either a positive [UP] or negative [DOWN] direction.
if (move.Sticks[1].Y != 0)
{
if (_playerInView)
_camY -= move.Sticks[1].Y;
}
// The player has moved the right analog stick to a resting [X.0, Y.0] position.
else if (move.Sticks[1].X == 0 && move.Sticks[1].Y == 0)
{
_camX = 0;
_camY = 0;
}
}
else
_resetCamera = true;
// Now let's call to change the camera position, if necessary.
_MoveCamera();PlayerController.cs : _MoveCamera()
protected void _MoveCamera()
{
T2DSceneCamera camera = TorqueObjectDatabase.Instance.FindObject<T2DSceneCamera>("Camera");
T2DSceneObject player = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>("Player");
if (camera != null && player != null)
{
if (!camera.IsMounted)
camera.Mount(player, "", true);
if (_resetCamera)
{ _camX = 0; _camY = 0; }
else if (_shiftCamera)
{
_playerInView = _IsPlayerInView(player.Name);
if (_playerInView)
{
Vector2 _newCamPos = new Vector2((_camX), (_camY));
camera.Position = player.Position + _newCamPos;
}
}
}
}PlayerController.cs : _setupInputMap()
inputMap.BindMove(gamepadId, (int)XGamePadDevice.GamePadObjects.RightThumbX, MoveMapTypes.StickAnalogHorizontal, 1); inputMap.BindMove(gamepadId, (int)XGamePadDevice.GamePadObjects.RightThumbY, MoveMapTypes.StickAnalogVertical, 1); inputMap.BindMove(gamepadId, (int)XGamePadDevice.GamePadObjects.RightShoulder, MoveMapTypes.Button, 1);
This works great on the Xbox360, I can press the right bumper and then move the right analog stick around and the camera does what I expect in terms of movement.
Now I want to be able to limit the movement of the camera so that it can only be moved to the extent that the current player is still visible on the screen.
To attempt to do this I added the following member and method to the PlayerController.cs file:
private bool _playerInView;
protected bool _IsPlayerInView(string actorName)
{
bool result = false;
T2DSceneCamera camera = TorqueObjectDatabase.Instance.FindObject<T2DSceneCamera>("Camera");
T2DSceneObject actor = TorqueObjectDatabase.Instance.FindObject<T2DSceneObject>(actorName);
if (camera != null && actor != null)
{
float _minY, _maxY, _minX, _maxX;
_minY = {?:What should this calculation be.:?}
_maxY = {?:What should this calculation be.:?}
_minX = {?:What should this calculation be.:?}
_maxX = {?:What should this calculation be.:?}
if (actor.Position.X > _minX && actor.Position.X < _maxX
&& actor.Position.Y > _minY && actor.Position.Y < _maxY)
result = true;
}
return result;
}I've tried various ways of calculating whether the camera is moving outside the range of the current player but so far they have a much greater extent than I anticipated, and worse than that there starts to exist a horrible redrawing artifact. I believe the redrawing artifacts are just simply a caculation problem and the screen is actually bouncing between to locations to draw due to my woefully out of date math skills. ;)
So to summarize I believe there actually two questions here:
- How would I calculate whether the specified player component is actually within the viewable bounds of the specified camera?
- Is this implementation of player invoked camera movement prudent, or is there a more efficient way to implement this type of functionality within a game context?
Sorry for the length of the post and thanks in advance for any ideas!
About the author
http://www.pdxmodgames.com
Recent Threads
#2
2. Look at the date of the OP, pointless necro thread..
3. For the OP, I once added a World Limit Component to a T2DSceneCamera, and it actually worked to stop camera movement on the world limits, you can try it, see if that works.
10/22/2010 (5:14 pm)
1. Wrong forum2. Look at the date of the OP, pointless necro thread..
3. For the OP, I once added a World Limit Component to a T2DSceneCamera, and it actually worked to stop camera movement on the world limits, you can try it, see if that works.
#3
;-)
10/25/2010 (6:36 am)
Thanks guys both interesting ideas. I will go back and see what I did (if anything to resolve this originally.) Always nice to get refreshers.;-)
Torque 3D Owner Alebrije Estudios
Default Studio Name
function someClass::someFunction(%this) { %point = sceneWindow2D.getWindowPoint(%this.getPositionX() SPC %this.getPositionY()); %pointX = getWord(%point, 0); %pointY = getWord(%point, 1); if(%pointX > 480 || %pointX < 0 || %pointY > 320 || %pointY < 0) { //outside camera view } else { //inside camera view } }In this case the screen is 480x320 (iPhone), which is where the values in the if statement come from.