#Help with player translation

1 messages · Page 1 of 1 (latest)

quartz vigil
#

Can you explain the issue again? I didn't quite understand what happens

quartz vigil
#

I see. I believe this is because you are rotating the player transform, which means the player collider follows. Because the collider has different sizes on each axis, it leads to the position changing. (Imagine your player lying on the ground)

#

I would probably remedy this by removing the camera from your player entity, and instead spawning the camera with a new entity as a child, then apply the camera logic to that entity.

stoic finch
#

alright i'll try that

#

thank you

stoic finch
#

@quartz vigil okay so i spawned it as a parent/child hierarchy but now my mouse movement code is completely obsolete

    commands.spawn((
        Player { ..Default::default() },
        RigidBody::Dynamic,
        GravityScale(0.9),
        Sleeping::disabled(),
        Collider::cuboid(0.15, 0.8, 0.15),
        LockedAxes::ROTATION_LOCKED,
        ActiveEvents::COLLISION_EVENTS,
        Ccd { enabled: true }
    )).with_children(|parent| {
        parent.spawn(Camera3dBundle::default());
    }).insert(TransformBundle::from(
        Transform::from_xyz(
            spawn_point.x,
            spawn_point.y,
            spawn_point.z
        )
    ));

since i spawned them as one it all kinda just worked but now i have no clue how to connect my mouse movement code to the camera? can't find anything in the docs either ☹️

pub fn mouse_callback(
    mut query: Query<(&mut Player, &mut Transform), With<Camera>>,
    mut mouse_motion_events: EventReader<MouseMotion>
) {
    for (mut player, mut transform) in query.iter_mut() {
        for event in mouse_motion_events.read() {
            const SENSITIVITY: f32 = 0.001;
            const MAX_VERTICAL_ANGLE: f32 = std::f32::consts::FRAC_PI_2 - 0.02;
            
            player.pitch += -event.delta.y * SENSITIVITY;
            player.yaw += -event.delta.x * SENSITIVITY;
            
            player.pitch = player.pitch.clamp(-MAX_VERTICAL_ANGLE, MAX_VERTICAL_ANGLE);

            transform.rotation = Quat::from_axis_angle(Vec3::Y, player.yaw) * Quat::from_axis_angle(Vec3::X, player.pitch);
        }
    }
}
quartz vigil
#

I would probably do some restructuring. Maybe create a new component CameraController, that contains all the data you need, like sensitivity, pitch yaw etc. Then query for that instead the Player component

#

The actual code is probably gonna be about the same

stoic finch
#

oh i got it working

#

thanks dude :))

stoic finch
#

hey @quartz vigil could i bother you for another question? this is my movement code:

use crate::modules::player::PlayerController;
use bevy::prelude::*;

pub fn update(
    key_event: Res<Input<KeyCode>>,
    mut query: Query<(&mut Transform, &mut PlayerController)>,
    time: Res<Time>,
) {
    for (mut transform, mut player) in query.iter_mut() {
        let delta_time = time.delta_seconds();
        let jump_height = player.jump_height;
        let friction = 0.9;
        let mut speed = player.speed;

        let forward = Vec3::new(transform.forward().x, 0.0, transform.forward().z).normalize_or_zero();
        let backward = -forward;
        let right = Vec3::new(transform.right().x, 0.0, transform.right().z).normalize_or_zero();
        let left = -right;

        let mut horizontal_velocity = Vec3::ZERO;

        if key_event.pressed(KeyCode::W) {
            horizontal_velocity += forward;
        }
        if key_event.pressed(KeyCode::S) {
            horizontal_velocity += backward;
        }
        if key_event.pressed(KeyCode::A) {
            horizontal_velocity += left;
        }
        if key_event.pressed(KeyCode::D) {
            horizontal_velocity += right;
        }
        if key_event.just_pressed(KeyCode::Space) {
            player.velocity.y = jump_height;
        }        
        if key_event.pressed(KeyCode::ShiftLeft) {
            speed = player.speed / 1.5;
        }
        if key_event.just_released(KeyCode::ShiftLeft) {
            speed = player.speed;
        }
        
        horizontal_velocity = horizontal_velocity.normalize_or_zero();

        player.velocity.x = horizontal_velocity.x * friction;
        player.velocity.y *= friction;
        player.velocity.z = horizontal_velocity.z * friction;

        transform.translation += player.velocity * speed * delta_time;
    }
}

and ignore the simplicity but now it's also ruined since the transform is different now

orchid isle
#

hey so wahts happening now?

stoic finch
#

my transform doesnt properly rotate based no my camera rotation anymore so if i hold W to move forward it just goes one way no matter where i look

#

since i made the camera a child of the player transform stopped working the way i intend it to

orchid isle
#

For FPS games what I'd typically do is when you move the mouse, the Yaw is applied to the Transform of the Character, and Pitch is applied to the Transform of the Camera.

#

Like basically imagine you're unable to turn your head side to side, but you can look up and down. And if you want to see something to the right you need to turn your whole body.

stoic finch
#

how would i seperate the transform of the player vs the camera though? i thought transform (for a child, so my camera in this instance) is relative to the position of the parent

#

and im not quite sure how to go about separating/accessing the transform for two different objects because all i know right now is adding Transform as a whole as a query

orchid isle
#

ah, let me show you some code, give me a sec

#

actually can I see your code that handles the mouse movement stuff

stoic finch
#

yes wait

#
pub fn mouse_callback(
    mut query: Query<(&mut CameraController, &mut Transform), With<Camera>>,
    mut mouse_motion_events: EventReader<MouseMotion>
) {
    for (mut camera, mut transform) in query.iter_mut() {
        for event in mouse_motion_events.read() {
            const MAX_VERTICAL_ANGLE: f32 = std::f32::consts::FRAC_PI_2 - 0.02;
            
            camera.pitch += -event.delta.y * camera.sensitivity;
            camera.yaw += -event.delta.x * camera.sensitivity;
            
            camera.pitch = camera.pitch.clamp(-MAX_VERTICAL_ANGLE, MAX_VERTICAL_ANGLE);

            transform.rotation = Quat::from_axis_angle(Vec3::Y, camera.yaw) * Quat::from_axis_angle(Vec3::X, camera.pitch);
        }
    }
}
orchid isle
#

@stoic finch ok this is completely untested and a bit messy, but you can try this:

pub fn mouse_callback(
    mut character_query: Query<&Transform, With<Character>>,
    mut camera_query: Query<(&mut CameraController, &mut Transform), (With<Camera>, Without<Character>)>,
    mut mouse_motion_events: EventReader<MouseMotion>
) {
    for event in mouse_motion_events.read() {
        for (mut camera, mut camera_transform) in camera_query.iter_mut() {
            for (mut character_transform) in character_query.iter() {
                const MAX_VERTICAL_ANGLE: f32 = std::f32::consts::FRAC_PI_2 - 0.02;
                
                camera.pitch += -event.delta.y * camera.sensitivity;
                camera.yaw += -event.delta.x * camera.sensitivity;
                
                camera.pitch = camera.pitch.clamp(-MAX_VERTICAL_ANGLE, MAX_VERTICAL_ANGLE);
    
                camera_transform.rotation = Quat::from_axis_angle(Vec3::X, camera.pitch);
                character_transform.rotation = Quat::from_axis_angle(Vec3::Y, camera.yaw)
            }
        }
    }
}
#

note the Without<> statement

#

oops typo in there, sec

#

there

#

ill show you a slightly different way that IMO reads better

stoic finch
#

i assume with Character you mean my player structure i created?

orchid isle
#

oh sorry I should have kept it consistent, yes

#

whatever is the parent of your camera

stoic finch
#

i would like to point out the languages i am quite comfy with do not really use references

orchid isle
#

oh, Query<&mut Transform, With<PlayerController>>,

stoic finch
#

thank you

#

let me try it

#

oh

#

wow it just works like that

orchid isle
#

haha right on

stoic finch
#

damn

#

thank you dude

#

i think i can close this thread now

orchid isle
#

cool 😄