Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by angad singh · Sep 24, 2010 at 05:43 AM · parentchildcuberubikscube

trying to make the inside of a rubik's cube

imagine being on a platform on the inside of an object which is like a 4X4 or 5X5 rubik's cube, we need to be able to look around and move whatever slices of the cube that have to be moved by pointing at them and choosing to move a 'vertical' or 'horizontal' slice.

the basic idea i had while tinkering around int eh gui was setting a slice as the child of the 'horizontal' or 'vertical'. can this be done dynamically in unity? changing the 'parent/child' relationships during gameplay by the player? any idea?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by skovacs1 · Sep 24, 2010 at 04:40 PM

Yes, dynamic re-parenting can be done, but it's up to you if that's the best way to go in this case as there's going to be 3n - 2 (for an odd n) or 3n pivots (for an even n) to keep track of for an n x n x n cube. Since you're doing all the work of changing and re-calculating your slices anyways, it's less work to actually forget about the re-parenting in some cases. Also, depending on your use case, you may need to be concerned with misalignment of the slices.

To dynamically re-parent and rotate, you would do something like:

//Assuming you've figured out the objects in your slice, the pivot and axis to rotate on
function RotateSlice(slice : Transform[], pivot : Transform, axis : Vector3) {
    for(var child : Transform in pivot) child = null; //clean out any old parenting
    for(var cube : Transform in slice) cube.parent = pivot; //parent the slice
    pivot.Rotate(axis, 90, Space.World); //Rotate
}

The thing is that once you already have the cubes in your slice, determining the center might be more trivial depending upon your implementation. Once you have the center cube, you could just do something simple and not have to worry about the parenting at all:

for(var cube : transform in slice) cube.RotateAround(centerCube.position, axis, 90);

There are any number of ways you can decide to force the alignment of the slices. Likewise, depending on if and how you want to track the rotations, either implementation might better suit your use case.

The problem comes with determining the axis. "horizontal" and "vertical" can change as your orientation relative to the surface changes (looking at the roof while turning in place or tilting your head while looking at a wall for example). You will need to resolve which way is which. Something like this could be done fairly easily, but is very dependent upon the setup of your scene:

var axis : Vector3; if(rotateVertical) axis = mainCamera.transform.right; else if(rotateHorizontal) axis = mainCamera.transform.up;

if(axis) { var dot : float = Vector3.Dot(axis, Vector3.up); //Check the cube up if(dot >= 0.5) axis = Vector3.up; else if(dot <= -0.5) axis = -Vector3.up; else { //one of the sides is our axis //Check the cube right dot = Vector3.Dot(axis, Vector3.right); if(dot >= 0.5) axis = Vector3.right; else if(dot <= -0.5) axis = -Vector3.right; //Check the cube forward else if(Vector3.Dot(axis, Vector3.forward >= 0.5) axis = Vector3.forward; else axis = -Vector3.forward; } }

It's at worst 3 dot products to tell which axis to rotate around. You could obviously use the Rubik's cube's actual axes rather than the world axes, but it was simpler to assume a world-aligned cube than to assume a parent cube to all of the cubes that comprise it because of the parenting stuff. If you were to have a parent to all of the cubes (and pivots too if you like), instead of setting the parents to null, you would set them to the parent cube.

This post was about something similar, but was concerned with running atop the cube to rotate the slices. My answer there is much longer, but pretty much says the same thing.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image angad singh · Sep 29, 2010 at 06:52 AM 0
Share

thanks :) the rota$$anonymous$$round did it for me actually...it turned out to be much easier than i'd thought!!!

avatar image dot · Oct 10, 2010 at 12:00 AM 0
Share

what does this "dot" code actually do?

avatar image skovacs1 · Oct 12, 2010 at 03:00 PM 0
Share

a dot product is the scalar projection of one vector onto another. Since the vectors are normalized, it returns the cosine of the angle between the two which is larger positives when it is closer to parallel and smaller negatives as it is closer to being negatively parallel. It's like using Vector3.Angle for our purposes, but less computationally expensive. In this case, we check which of the cube's axes is closest to the one on the camera that we tell it to rotate on.

avatar image
0

Answer by tomekkie2 · Jun 15, 2011 at 03:53 PM

Why not to base the selecting of slices on "subcubes" position?. A user could click a "subcube" and its position and the area where the user clicks it would determine the slice position and orientation. Once the slice position and orientation is sorted out, the whole slice is being selected as well as one of the six possible rotation axes (Vector3.up,-Vector3.up,Vector3.right,-Vector3.right,Vector3.forward,-Vector3.forward). That is just my solution and I have made available the code at http://virtualplayground.d2.pl.

Once the selection is made and target rotation calculated, I tween the rotation using Quaternion.Slerp on Update function, after temporary disabling of the cube interactivity for the tweening time.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Make a simple tree 1 Answer

Making cube a child of an object. 1 Answer

Spawn Object As Child? 1 Answer

Select a child's variables from the parent. 1 Answer

Rotate parent so child is facing a specific rotation 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges