#Rotate around a tilted axis

22 messages · Page 1 of 1 (latest)

wicked orchid
#

Hey, I have this code, which rotates my planets around themselves based on their speed (so just planet rotations). What I want is that the planet is tilted e.g. for Earth 23° and then the rotation is around this tilted axis. How could I do that? This is the current state as used in the video:

#
fn axial_tilt_planets(
    mut query: Query<(Entity, &mut AxialTilt, &mut Transform, With<Planet>, Without<Star>, Without<Moon>)>,
    parents_query: Query<(&Transform, &BodyChildren, With<Star>, Without<Planet>, Without<Moon>)>
) {
    for (entity, mut tilt, mut transform, _, _, _) in &mut query {
        if tilt.applied {
            continue;
        }
        
        let parent = parents_query.iter().find(|(_, children, _, _, _)| {
            children.0.contains(&entity)
        });
        if let Some((p_transform, _, _, _, _)) = parent {
            let (u, w) = (transform.translation - p_transform.translation)
                .normalize()
                .any_orthonormal_pair();
//transform.translation is the actual planet and p_transform is the parent (sun)
            let u_p = Quat::from_axis_angle(w, tilt.num.to_radians()).mul_vec3(u);
            let tilted = Quat::from_axis_angle(Vec3::X, tilt.num.to_radians()) * Vec3::Z;
    //        transform.rotate_axis(u_p, 0.0);
            transform.rotate_x((90.0 as f32).to_radians());
        //    transform.rotate_y(tilt.num.to_radians());
        //    transform.rotate_x(tilt.num.to_radians());
            tilt.applied = true;
            tilt.axis = Some(u_p);
        }
    }
}
#
fn rotate_bodies(
    mut planet_query: Query<(&RotationSpeed, &AxialTilt, &mut Transform, &Diameter, With<Planet>, Without<Star>, Without<Moon>)>,
    time: Res<Time>,
    speed: Res<Speed>,
    sub_steps: Res<SubSteps>,
    pause: Res<Pause>,
) {     
    if !pause.0 {
        for (rotation_speed, axis, mut transform, diameter, _, _, _) in &mut planet_query {
            if rotation_speed.0 == 0.0 || diameter.num == 0.0 || axis.axis.is_none() {
                continue;
            }
            
            let speed_modifier = ((speed.0 as f32) * (sub_steps.0 as f32)) / DAY_IN_SECONDS;
            let rotation_duration = rotation_speed.0 * 60.0;
            let rotations_per_day = DAY_IN_SECONDS / (rotation_duration as f32);
            
          //  transform.rotate_axis(axis.axis.unwrap(), 2.0 * PI * (rotations_per_day * time.delta_seconds() * speed_modifier));
            transform.rotate_z(2.0 * PI * (rotations_per_day * time.delta_seconds() * speed_modifier));
            
        }
    }
}
#

Note there are some unused variables from things I tried but they all caused all planets except earth to wobble around

#

and in the first system tilt.num is the tilt in degrees

wicked orchid
#

it already is, as I said the earth rotation looks fine but all other bodies wobble while rotating

wicked orchid
#

I mean that doesn't look right with the lighting

#

That doesn't work with my physics simulation

#

you could say its tilted based of the direction vector between the sun and the earth (in this case)

#

and then the rotation is around this axis

wicked orchid
#

what do you mean? The axis should be calculated at the start and then the planet starts rotating around the same axis

#

oh you are right

#

well

#

all planets, so all planets have a axis tilt property which the planet ideally should rotate around

#

yea thats what I'm doing

#

I already simulate everything from the planet orbits to the rotation speeds

#

but the axis tilt is missing (which all planets have)

#

axial tilt*

#

yea