#raylib camera rotation around a point

1 messages · Page 1 of 1 (latest)

autumn grove
#

Hello there, please tell me what I'm doing wrong... I'm trying to ratate the camera around the player on Y axis using mouse movement. This is what I came up with after lots of searching (came up with is a strong statement as realy I copy pasted this): `
rot_angle : Vector2 = GetMouseDelta() * GetFrameTime()

csx : f32 = math.cos(rot_angle.x)
snx : f32 = math.sin(rot_angle.x)
tx : f32 = camera.position.x - player_position.x
tz : f32 = camera.position.z - player_position.z
nx := (tx * csx - tz * snx) + player_position.x
nz := (tx * snx + tz * csx) + player_position.z

camera.position = {nx, camera.position.y, nz}
camera.target = player_position`

camera sort of rotates around the player but it doesn't follow the player, just stays at the same possition. So when I move the player it gets further and further away from camera. What stupid am I doing?
mossy abyss
#

Doing this from my phone so sorry:

Replace the ‘camera.position’ line with
‘camera.position += { nx, 0, nx }’

#

Again not sure just a shot in the dark to try

autumn grove
mossy abyss
#

I usually just set the camera position to the object in questions position, so “cam.pos = player.pos - offset”

#

But again I can’t test or use the code so I’m not really any help lmao my bad

autumn grove
near quarry
#

i just did this on a raylib camera, i think this will give you a good starting point?

FocusCamera :: struct {
    ray_cam:     rl.Camera,
    position:    Vector3, // <-- technically target
    yaw:         f64,  // RADIANS
    pitch:       f64,  // RADIANS
    distance:      f64,
}
// this is called by my update_camera wrapper, which is called in the game-loop
update_ray_cam :: proc(fc: ^FocusCamera) {
    fc.ray_cam.position = la.to_f32(get_camera_position(fc)).xyz
    fc.ray_cam.target = la.to_f32(fc.position).xyz
}

get_camera_position :: proc(fc: ^FocusCamera) -> Vector3 {
    direction := Vector3 {
        math.cos(fc.yaw) * math.cos(fc.pitch),
        math.sin(fc.pitch),
        math.sin(fc.yaw) * math.cos(fc.pitch)
    }
    direction = la.normalize(direction)
    relative_offset := direction * fc.distance
    // Do world-space xform _here_ if not aligned to raylib's global frame
    final_position :=  fc.position + relative_offset

    return final_position
}
autumn grove
#

hi, appreciate it but I sort of figured it out using matrixes. However now I have a problem that translating models transform scales it insted. so if I do linalg.matrix4_translate_f32(velocity) and then multiply it by models transform it just deforms the model. Any ideas on this one? I'm sure I'm just missing something, my linear algebra is weak to say the leaset...