Thread: Pls help -openGL boundary checking in skybox

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    135

    Pls help -openGL boundary checking in skybox

    I am doing a boundary checking in skybox where the camera cannot move out of skybox. But using this code:
    Code:
    	if (((Position.z <= 0 && Position.z >= -380) && (Position.y >= 0 && Position.y <= 20)) && (Position.x <= 195 && Position.x >= -195))
    Seems there is a problem in (Position.x <= 195 && Position.x >= -195)) where I still can get out of the skybox. What is the right way of coding such as all of them would not moved out of the skybox. It seemed my codes only the Position.y and Position.z managed to stop the camera from moving out of the skybox. How to compare 3 different position using &&.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    When you hit the edge of the box you actually can't keep moving in the same direction if you want to stay inside of it. All of the equals signs in your condition include edge cases, or so I assume, because you didn't actually give the dimensions of the box. So to correct the condition try removing the equals first.

    edit: Well technically you can "fly around the world" so to speak, and so continue to move in the same direction, if you start again from the opposite side when you hit an edge, so that is a minor correction I should make but don't let it distract you.
    Last edited by whiteflags; 06-02-2012 at 01:42 AM.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    All of the codes have no problem expect the last part where I still can move out of the skybox by pressing button ''a' or 'd'. Is my && wrong. I not really good in comparing 6 position using &&.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    if (((Position.z <= 0 && Position.z >= -380) && (Position.y >= 0 && Position.y <= 20)) && (Position.x <= 195 && Position.x >= -195))
    You will have a nightmare if you continue to use magic numbers like this.
    Define your limits - BLAH_BLAH = < a_known_value> whatever. You simply cannot scale up what you are doing now without this.
    Last edited by rogster001; 06-02-2012 at 09:18 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    My codes have no problem staying in skybox except the x-value. I have already check the values, it is definitely right. What is the correct way to use && when I had to make sure the 6 positions is correct before I allow the camera to move.

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    You might want to ((bracket)..(off)) your expressions when using more complex conditions and see if that helps
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Um...I'm lost. Technically the camera should NEVER hit the skybox b/c the skybox is always centered around the camera. You should be translating your skybox with the camera so the two will never intersect. If this is not happening it is a serious flaw in your system and needs to be corrected. However if you want to know the algorithm to use to keep your 3rd person camera from clipping through other objects in the world you need to look into swept sphere. Alternatively you can use the open source Bullet physics API that offers functionality that calculates this for you and tells you the new position of the object in question so it does not clip.

    If the skybox is centered around your camera then the world positions of the center of it's planes can be calculated easily. Please note these calculations are for a left-handed system right: +x, up: +y, into screen: +z and skyBoxScale is expressed in world coordinates.

    Left = cameraWorldPos.x - Vector3(1.0f,0.0f,0.0f) * (skyBoxScale.x * 0.5f);
    Top = cameraWorldPos.y + Vector3(0.0f,1.0f,0.0f) * (skyBoxScale.y * 0.5f);
    Right = Left + skyBoxScale.x;
    Bottom = Top + skyBoxScale.y;
    Front = cameraWorldPos.x + Vector3(0.0f,0.0f,1.0f) * (skyBoxScale.z * 0.5f);
    Back = Front - skyBoxScale.z;

    Given those centers and the scale of the skybox you can easily calculate the planes.
    Last edited by VirtualAce; 06-02-2012 at 01:36 PM.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    The skybox is a piece of walls and I am inside a house. So I am using boundary checking for preventing camera to move outside of the skybox. Just that I got problem and the camera can still moved out of the skybox in the left and right direction when I pressed button 'a' or 'd' which keys in to move sideways. Other no problem.

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by evildotaing View Post
    The skybox is a piece of walls and I am inside a house. So I am using boundary checking for preventing camera to move outside of the skybox. Just that I got problem and the camera can still moved out of the skybox in the left and right direction when I pressed button 'a' or 'd' which keys in to move sideways. Other no problem.
    Err... walls are not 'usually' skyboxes, I think.
    In this case, just restrict the x-y-z coordinates of the camera to not go beyond the room, in local coordinates.
    (That eliminates the need of any complicated polygon algorithms )

    A skybox, otoh, is 'sort' of attached to the camera.
    It has to be translated or rotated exactly opposite in magnitude with the camera.

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    So, if my code for if (((Position.z <= 0 && Position.z >= -380) && (Position.y >= 0 && Position.y <= 20)) && (Position.x <= 195 && Position.x >= -195)) is wrong? A bit weird that Position.z and Position.y worked but not the Position.x.

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    A bit weird you are still trying to test against the skybox when it should never collide with the camera.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. openGL skybox help
    By evildotaing in forum Game Programming
    Replies: 3
    Last Post: 01-16-2012, 01:36 AM
  2. Front texture for skybox
    By VirtualAce in forum Game Programming
    Replies: 3
    Last Post: 10-04-2007, 01:22 PM
  3. Array boundary checking in C
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 04-13-2007, 04:51 AM
  4. Perfect skybox tool
    By VirtualAce in forum Game Programming
    Replies: 4
    Last Post: 04-03-2005, 02:35 PM
  5. Array boundary and error checking
    By JKI in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2003, 09:27 AM