playMaker

Author Topic: [SOLVED] How do you clamp the X and Y of a scene viewable area?  (Read 12202 times)

Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • Disastercake
I'm trying to set up a viewing clamp on the top, bottom, left, and right of my 2D world map.  I want the user to be able to zoom in and out ( which is completed), and pan the camera (completed).  But I don't want them to see outside a specific boundary.  How would the math and programming look behind calculating if the user is seeing outside of the level boundary?  Since the user can zoom in and out that starts to get a little complicated, and I'm not sure how to approach this.

Any help would be greatly appreciated.


The information I've gathered is the position of the camera for each side at the largest and smallest zoom.  I imagine I need to have some sort of calculation that can figure the in-between numbers that are acceptable.

Side: Smallest /// Largest
Top (Y): 11.65808 /// 6.277246
Bottom (Y): -9.491721 /// -0.1629976
Left (X): 18.12113 /// 7.855795
Right (X): -18.29644 /// -6.274414

I also wonder if there's a more simple ray casting that can be used.  Like making 4 boxes that surround the viewable area, and if the camera corners ray cast into any of them, the camera snaps back to where it should be depending on the box it ray cast on.

However, I'm not sure how the code would look for this raycasting.
« Last Edit: May 16, 2012, 10:54:14 AM by Disastercake »
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com

justifun

  • 1.2 Beta
  • Sr. Member
  • *
  • Posts: 280
Re: How do you clamp the X and Y of a scene viewable area?
« Reply #1 on: May 13, 2012, 10:48:13 PM »
Give this a shot,

There's an action called Is visible, that will send an event once you can see a certain game object,

If you were to create a wall out of invisible meshes around your level, and then use the "is visible action" to interrupt your camera movement FSM states. so that it suddenly stops moving when it detects and edge?

Just thinking off the top of my head.


Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: How do you clamp the X and Y of a scene viewable area?
« Reply #2 on: May 14, 2012, 12:39:48 AM »
Not at my computer, but if orthographic size of the camera is in world units, then this should be pretty easy. The minimum limit for the camera position would be the lower corner of the map plus half the orthographic size. The upper limit would be the upper corner minus half the orthographic size. You can calculate the x and y limits separately and limit them with Float Clamp.

Try drawing a diagram, and the math should become more obvious... or maybe another solution will jump out at you. Personally I always draw a diagram to solve this kind of problem...

Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • Disastercake
Re: How do you clamp the X and Y of a scene viewable area?
« Reply #3 on: May 14, 2012, 12:41:09 AM »
I'm going to give both these solution a shot and let you guys know how it works! =D
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com

Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • Disastercake
Re: How do you clamp the X and Y of a scene viewable area?
« Reply #4 on: May 14, 2012, 01:08:25 AM »
I just tried the Is Visible, but apparently this doesn't concern what's visible in the direct view port.  This just checks if the object is rendered or not, anywhere on the scene.  So this won't work, unfortunately.

Also ,I'm not sure if I understand how to approach the math you're suggesting Alex.  I'm not sure how to tell if the orthographic camera uses world units.  I don't think it does.
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: How do you clamp the X and Y of a scene viewable area?
« Reply #5 on: May 14, 2012, 11:01:11 AM »
I found this on Unity Answers:
Quote
... the orthographic size value represents the amount of in-game units (meters) half of your vertical screen size takes.
http://answers.unity3d.com/questions/58262/orthographic-camera-size.html

From that you should be able to figure out the MinX, MaxX, MinY, MaxY for the camera given the current orthographic size.

If I get a chance I'll also write a custom action for this - seems pretty useful for 2d games!

amaranth

  • Full Member
  • ***
  • Posts: 172
Re: How do you clamp the X and Y of a scene viewable area?
« Reply #6 on: May 14, 2012, 12:46:52 PM »
I would use it, too! :)

Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • Disastercake
Re: How do you clamp the X and Y of a scene viewable area?
« Reply #7 on: May 14, 2012, 06:54:48 PM »
So this means that if the orthographic camera size is 10, that it is 20 game units long for the whole viewport?

Does this also mean that one whole tick of a Y and X value is 1 game unit?

so if one object is at 0,0 and another object is at 10.056,0, they are 10.056 game units apart?

Also, how would you calculate the distance apart if one value is a negative number and the other value is a positive number?  For example if an object is at x= -6 and another object is at x = 6, how do you tell that they are 12 units apart?


Sorry for all the math questions.
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: How do you clamp the X and Y of a scene viewable area?
« Reply #8 on: May 15, 2012, 12:38:12 AM »
So this means that if the orthographic camera size is 10, that it is 20 game units long for the whole viewport?

It means the viewport is 20 units high. You would have to use the aspect ration of the viewport to calculate the width. E.g., if 4:3,  the width is 20 x 4/3.

Quote
Does this also mean that one whole tick of a Y and X value is 1 game unit?
so if one object is at 0,0 and another object is at 10.056,0, they are 10.056 game units apart?

Yes.

Quote
Also, how would you calculate the distance apart if one value is a negative number and the other value is a positive number?  For example if an object is at x= -6 and another object is at x = 6, how do you tell that they are 12 units apart?

Just subtract one from the other.  6 - (-6) = 6 + 6 = 12

You can also get the absolute (non negative) value of a float with Float Abs:
https://hutonggames.fogbugz.com/default.asp?W238

Quote
Sorry for all the math questions.

No problem! You will probably need some basic math like this at some point to make a game...

Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • Disastercake
Re: How do you clamp the X and Y of a scene viewable area?
« Reply #9 on: May 15, 2012, 04:21:00 PM »
I got my brain wrapped around the concept now and worked out the psudeo code, but which functions would you recommend working with to do the calculations in Playmaker?  Would it be better to do most of the calculating in a custom action?
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com

Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • Disastercake
Re: How do you clamp the X and Y of a scene viewable area?
« Reply #10 on: May 15, 2012, 05:33:39 PM »
I made a custom action to check and adjust that runs every update.
But this is crashing unity when the DoMoveUp() function is ran.  And I'm not even sure if my math is right.  I did kind of a hacky job puling pieces from other actions.  Do you see what's wrong with it?

Code: [Select]
//
//Action for boundary checking. 
//
[ActionCategory("Soul Saga")]
[Tooltip("!!!!Gets the Y Position of the mouse and stores it in a Float Variable.")]
public class CameraBoundaryCheck : FsmStateAction
{
[RequiredField]
[Tooltip("The game object to translate.")]
public FsmOwnerDefault goCamera;

[RequiredField]
[UIHint(UIHint.Variable)]
public FsmVector3 v3Camera;
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmFloat floatCameraX;
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmFloat floatCameraY;

[RequiredField]
[UIHint(UIHint.Variable)]
public FsmVector3 v3BotLeft;
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmFloat floatBotLeftX;
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmFloat floatBotLeftY;

[RequiredField]
[UIHint(UIHint.Variable)]
public FsmVector3 v3TopRight;
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmFloat floatTopRightX;
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmFloat floatTopRightY;

public bool everyFrame;
public bool lateUpdate;

float floatCameraSizeHalf;
float x;
float y;
float z;
FsmVector3 vector;

public override void Reset()
{
vector = null;
}

public override void OnEnter()
{
DoCheck();

if (!everyFrame)
Finish();
}

public override void OnUpdate()
{
if (everyFrame)
DoCheck();
}

//public override void OnLateUpdate()
//{
// if (lateUpdate)
// DoCheck();
//}

void DoCheck()
{
//Bottom Check
//Uses Y values of Camera and BotLeft.
//Requirement: CameraY > (halfCameraSize+BotLeftY) (((Reverse sign to find the opposite)))
if (floatCameraY.Value < 10+floatBotLeftY.Value)
DoMoveUp();
}

void DoMoveUp()
{
// init
Space space = Space.Self;

var go = Fsm.GetOwnerDefaultTarget(goCamera);
if (go == null)
{
return;
}

// Use vector if specified

x = 0;
y = floatCameraY.Value - (10+floatBotLeftY.Value);
z = 0;


var translate = vector.IsNone ? new Vector3(x, y, z) : vector.Value;


//y = floatCameraY.Value - (10+floatBotLeftY.Value);

// override any axis

//if (!x.IsNone) translate.x = x.Value;
//if (!y.IsNone) translate.y = y.Value;
//if (!z.IsNone) translate.z = z.Value;
translate.y = y;

// apply

//if (!perSecond)
//{
// go.transform.Translate(translate, space);
//}
//else
//{
go.transform.Translate(translate * Time.deltaTime, space);
//}

//Custom Clamp check
if (go == null)
{
return;
}
else
{
Debug.Log("Running Custom Clamp check");
var currentPosition = space == Space.World ? go.transform.position : go.transform.localPosition;

//vector.Value = currentPosition;
//x.Value = currentPosition.x;
//y.Value = currentPosition.y;
//z.Value = currentPosition.z;

//if (y < -0.2500495)
//{
currentPosition.y = 11.71901f;
//currentPosition.y = floatCameraY.Value+(10+floatBotLeftY.Value);
go.transform.position = currentPosition;
//}
}
}
}
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com

amaranth

  • Full Member
  • ***
  • Posts: 172
Re: How do you clamp the X and Y of a scene viewable area?
« Reply #11 on: May 15, 2012, 07:55:35 PM »
I made an action about a week ago that deals with the camera and bounds. Maybe you could find something to scavenge from it? The script lets you drag the camera around. If you hit a boundry, the camera stops. It doesn't have zoom in and zoom out though. And it's based on fingergestures. Here's the link: http://hutonggames.com/playmakerforum/index.php?topic=1553.0

Disastercake

  • Full Member
  • ***
  • Posts: 101
  • www.disastercake.com
    • Disastercake
Re: How do you clamp the X and Y of a scene viewable area?
« Reply #12 on: May 16, 2012, 12:59:01 AM »
The difficulty with making this is really only stemming from the fact that it can zoom, but thank you. =)

I actually ended up getting it working (with the zoom) after pulling out some hair.  It ended up being very simple and I'm not sure why I was stressing over it so much!

Thanks for everyone's patience and help! =D
« Last Edit: May 16, 2012, 02:15:28 AM by Disastercake »
Soul Saga - Anime themed adventure RPG by Disastercake

http://www.disastercake.com