Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Rotating a Cube around different pivots, Suggestions?

Discussion in 'Scripting' started by m3taphysics, May 16, 2014.

  1. m3taphysics

    m3taphysics

    Joined:
    Oct 9, 2012
    Posts:
    23
    Hi Guys

    Been toying around with some ideas of how I can rotate a cube along the X axis but rotate it where its corners are its pivots.

    So that effectively it would be a rolling cube. Reason is I wanted the ability to roll it around where I choose. Ive toyed with some maths but not really come up with a nice solution yet...note Im only rotating it along the X axis, so effectively in 2d. I am always rotating it around Vector3.forward.

    The problem is given any rotation that the cube is currently in I need to know what the lowest right most point given any rotation of the cube.

    Maybe Im just being dumb but I cant think of a nice way of doing this?
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Do you need to know the position of the lowest right most corner? Or the position of the lowest right most corner of the axis aligned bounding box?

    If the position of the corner itself, I would child Transforms to each corner. Then you can just get the location of those transforms to know where the corners are.

    If you want to know the corner of the axis aligned bounding box, Renderers and Colliders both have a bounds property that you can use to find the lowest right most point of the bounding box.
    http://docs.unity3d.com/Documentation/ScriptReference/Renderer-bounds.html
    http://docs.unity3d.com/Documentation/ScriptReference/Collider-bounds.html
     
  3. m3taphysics

    m3taphysics

    Joined:
    Oct 9, 2012
    Posts:
    23
    Yeah this is actually what I have done. I assigned some transforms to each of the corners to make things a bit easier. The problem is when it comes to rotating the cube, these will actually flip as its rotating (so everything becomes reversed).

    An axis aligned bbox wont achieve what I need. I need the bottom right corner of the rotated box so that I know what pivot point to rotate around.
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,465
  5. m3taphysics

    m3taphysics

    Joined:
    Oct 9, 2012
    Posts:
    23
  6. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Do you only need to check this after the flip, I.e. when the cube is flat on the ground?

    EDIT: The idea here is that there is only one corner (technically two on a cube) that is both below and to the right of the square. Again, assuming it is flat on the ground and the faces of the cube align with the axises. (axii?)
    Code (csharp):
    1.  
    2. Transform[] cornerTransforms;
    3.  
    4. Transform FindLowerRightTransform() {
    5.   Vector3 centerOfThisCube = transform.position;
    6.   foreach (Transform corner in cornerTransforms) {
    7.     bool rightOfCenter = centerOfThisCube.x < corner.position.x;
    8.     bool belowCenter = corner.position.y < centerOfThisCube.y;
    9.     if (rightOfCenter  belowCenter) {
    10.       return corner;
    11.     }
    12.   }
    13. }
    14.  
     
  7. m3taphysics

    m3taphysics

    Joined:
    Oct 9, 2012
    Posts:
    23

    Ideally I would prefer a solution where I didn't need to assume it was flat. For example if I wanted to change direction MID transform.

    Your solution is what I have been toying with though.
     
  8. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Ya, I chose that assumption because it made the problem simpler. If we can rotate the cube in any direction, we have to track all 8 corners.

    Let's consider some corner cases. (That's a pun!)

    Now, if you have a cube turned 45 degrees, you could have 4 corners that are all equally the most lower right corner. (Two corners are directly below the center, two corners directly to the right of center.)

    If the cube can have any rotation, then you could have two corners that are below and right of the center. One corner can be more below than right, and the other corner could be more right than below, yet the two corners are equally the most lower right corner.

    In any case, I would use a similar technique. Compare the positions of all the corners to figure out which one has the best combination of down and to the right.