0
\$\begingroup\$

I am developing a space sandbox (3D). Now I am working on an asteroid belt around a planet. I have an Asteroid class and an AsteroidBelt class that has a collection of the Asteroid instances. Each Asteroid object is placed randomly during initialization.

Asteroid class:

Matrix4 transform;  
double angle;
float x, y, z;
Vector3 initPosition;    

public Asteroid(ModelObject object, Texture diffuseTex){

    initPosition = new Vector3(
            MathUtils.random(-Settings.RingsDepth, Settings.RingsDepth), //x
            MathUtils.random(-.5f, .5f), //y
            MathUtils.random(-Settings.RingsDepth, Settings.RingsDepth)); //z
}

public void update(Camera camera){

    angle += Settings.RingsRotationalSpeed;
    if(angle>360) angle=0;
    if(angle<0) angle = 360;

    double angleRad = angle * MathUtils.degreesToRadians;

    x = (float) (Math.sin(angleRad) * Settings.RingsRadius  + initPosition.x);
    y = initPosition.y;
    z = (float) (Math.cos(angleRad) * Settings.RingsRadius + initPosition.z);

    transform.idt();
    transform.translate(x, y, z);
}

The code above works correctly, but it doesn't allow to rotate the asteroid belt around a planet. How to modify x, y, and z to rotate the asteroid belt around a planet freely? I hope the illustration below will clarify my problem:

Now the asteroid belt looks like below: initial But I need to rotate it from -90 to 90 degrees by X and Y axis: around X axis around Z axis around X and Z axis Any help will be appreciated!

\$\endgroup\$
  • \$\begingroup\$ This looks similar to a previous question answered here — are you able to adapt that solution to your needs? \$\endgroup\$ – DMGregory Feb 22 at 19:54
  • \$\begingroup\$ @DMGregory, thank you, but I've already solved it. \$\endgroup\$ – Nolesh Feb 23 at 4:52
0
\$\begingroup\$

LibGDX allows us to get rid of complex mathematics. I've solved my issue by using Matrix4 class:

Asteroid class:

Vector3 position;
Matrix4 rotationMatrtix;

public void update(Camera camera){

    angle += Settings.RingsRotationalSpeed;
    if(angle>360) angle=0;
    if(angle<0) angle = 360;

    double angleRad = angle * MathUtils.degreesToRadians;

    x = (float) (Math.sin(angleRad) * Settings.RingsRadius  + initPosition.x);
    y = initPosition.y;
    z = (float) (Math.cos(angleRad) * Settings.RingsRadius + initPosition.z);

    position.set(x, y, z);

    //rotate asteroid around X-axis
    rotationMatrtix.setToRotation(Vector3.X, Settings.RingsXRotation);
    position.mul(rotationMatrtix);

    //rotate asteroid around Z-axis
    rotationMatrtix.setToRotation(Vector3.Z, Settings.RingsZRotation);
    position.mul(rotationMatrtix);

    transform.idt();
    transform.translate(position);
}

Don't forget to create instances of the position and rotationMatrtix

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.