Sign in to follow this  

Unity [Unity] Supporting Voxel Rotation

This topic is 674 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In Unity, I've been working on procedurally generated planets made out of cubes. After a long time of testing and tweaking, I have it pretty much where I want it. The problem is, I want these planets to move around a star, which works okay, but I also want these planets to spin. In my code I use the position of where a raycast hits to find the block that would be built / broken. When rotating, though, there is no way for the planet to know that its rotated, meaning when you try to break a block on one side, it still thinks your breaking blocks on wherever that was originally. Is there any way to have it track its own rotation and use that to figure out (accurately) which block is being chosen? Or do I have to approach the problem from another angle?

 

Current method of replacing blocks:

    public void SetBlock(Vector3 pos, Block block)
    {

        float x = pos.x;
        float y = pos.y;
        float z = pos.z;

        x = Mathf.RoundToInt(x);
        y = Mathf.RoundToInt(y);
        z = Mathf.RoundToInt(z);

        x = x - this.transform.position.x;
        y = y - this.transform.position.y;
        z = z - this.transform.position.z;

        pos = new Vector3(x, y, z);

        blocks[(int)pos.x, (int)pos.y, (int)pos.z] = block;


        UpdatePlanet();

    }

 Thanks in advance!

Share this post


Link to post
Share on other sites
Use the transform class to transform the point. It will handle the rotation for you.

    public void SetBlock(Vector3 pos, Block block)
    {
        Vector3 localPos = this.transform.InverseTransformPoint(pos);
        float x = localPos.x;
        float y = localPos.y;
        float z = localPos.z;

        x = Mathf.RoundToInt(x);
        y = Mathf.RoundToInt(y);
        z = Mathf.RoundToInt(z);

        pos = new Vector3(x, y, z);

        blocks[(int)pos.x, (int)pos.y, (int)pos.z] = block;


        UpdatePlanet();

    }

Share this post


Link to post
Share on other sites

HappyCoder, what you said works 50% - but half the time it picks the wrong block, 1 or two away from where it was hit. SwiftCoder, how exactly would I transform the way? I'm planning on multiple planets, so not all of them will be rotated the same way.

Share this post


Link to post
Share on other sites


SwiftCoder, how exactly would I transform the way? I'm planning on multiple planets, so not all of them will be rotated the same way.

You have to raycast against each planet individually, but that shouldn't be an issue (do planets intersect each other?).

 

Then just use Transform.TransformPoint() to transform the ray origin into planet space, and Transform.TransformDirection() to transform the directio into planet space.

Share this post


Link to post
Share on other sites

How exactly would I transform the ray to the planet's transform before it knows which planet it is going to hit? Sorry if im not understanding, I haven't done much work with positions and rotations in the past.

Share this post


Link to post
Share on other sites


How exactly would I transform the ray to the planet's transform before it knows which planet it is going to hit? Sorry if im not understanding, I haven't done much work with positions and rotations in the past.

You would perform a preliminary raycast against each planet's bounding sphere to see which one it hits.

Share this post


Link to post
Share on other sites

This topic is 674 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this