#How to make 3d camera rotation?

138 messages ยท Page 1 of 1 (latest)

dusky rivet
#

I have been working on this for a bit. And don't have it quite right yet. Here is where I am at right now: https://github.com/bentondrew/farmsim-game/blob/clamp-camera-vertical-orbit/src/characters/player/camera/control.rs#L161.
It does the horizontal rotation just fine but I want the vertical rotation to pin to PI/2 above or below the xz plane but I don't have the rotation quite right and if I am not directly on the z axis, it will start going back down when it gets to the top.

GitHub

Our farmsim to play together. Contribute to bentondrew/farmsim-game development by creating an account on GitHub.

plush slate
woeful tiger
#

Hmm, thank you for this information.
When I read everything this, and find what I need, I add tag resolved.

plush slate
#

Feel free to ask

woeful tiger
woeful tiger
plush slate
#
let (mut x, mut y, z) = camera_transform.rotation.to_euler();
x += x_coordinate;
y += y_coordinate;
camera_transform.rotation = Quat::from_euler(x, y, z);
#

I might've confused mouse x with y

#

and this might be too fast, so you might need to divide it by some number

plush slate
#

ah, what's your up direction?

#

EulerRot::XYZ

#
let (mut x, mut y, z) = camera_transform.rotation.to_euler(EulerRot::XYZ);
woeful tiger
plush slate
woeful tiger
#

+= not work.

plush slate
woeful tiger
#

The video shows that it is impossible to move.

plush slate
#

can you share the code with the latest changes?

#

do you have movement code? or something that sets transform?

plush slate
#

would you mind showing it?

#

still thinking about it

#

it is weird

#

can you move with it?

woeful tiger
plush slate
#

ah, found the sneaky

#

instead of dividing it there

#

divide x_coordinate and y_coordinate here

#

x += x_coordinate / 100.0

#

like so

#

it should work then

#

we were dividing our coordiante by 100, so eventually it became closer to 0 each frame

#

if your mouse movement were enough, you could escape for several frames

#

but then it would go back to 100

plush slate
#

it should only divide delta (or change) by 100

woeful tiger
#

One moment, converting video.

plush slate
#

oh, still no worky? :(

woeful tiger
plush slate
#

I think I see two issues there, but let's begin with the obvious one

#

we are mixing the coordinates

plush slate
#

so, when you move your up and down, it is y axis, right?

#

but euler rotation is different, rotation is about "rotating around an axis"

woeful tiger
#

Moved down, camera up.

#

left and right not working.

plush slate
#
x += y_coordinate / 100.0;
y += x_coordinate / 100.0;
plush slate
#

instead of adding, remove it instead

x -= y_coordinate / 100.0;
woeful tiger
plush slate
#

let me come up with a quick demo, I have some ideas but it would be easier if I could test them. will reply in around 15 mins

gray dove
plush slate
#

euler rotation needs to be applied in a sequence, that's where I messed up

#

using EulerRot::YXZ instead

let (mut y, mut x, z) = camera_transform.rotation.to_euler(EulerRot::YXZ);
camera_transform.rotation = Quat::from_euler(EulerRot::YXZ, y, x, z);
#

and removing both coordinates

x -= y_coordinate / 100.0;
y -= x_coordinate / 100.0;
#

though you need to clamp your x coordinate as that's one of the issues of euler math. you can see it in action by moving all the way up

#

you are either way going to clamp your x coordinate, so that's okay

plush slate
#

good luck! ๐Ÿ˜…

woeful tiger
#

It's work!
But now left is up and right is down.

#

๐Ÿ˜ตโ€๐Ÿ’ซ

woeful tiger
#

Why does it even work.

#

Thank you so much @plush slate!

#

If you don't mind, I'll add you as a friend.

#

ะกamera movement regardless of its rotation.

woeful tiger
#

I need to take a nap.

plush slate
plush slate
#

so when you rotate something, its forward direction is no longer its z+

#

so instead of this

#
z_coordinate -= camera_transform.forward() * CAMERA3D_SETTINGS.camera_speed;
#

like so for z axis

#
camera_transform.right() * speed

for the x axis

plush slate
woeful tiger
#

: f32 = ?

plush slate
#

nope
z_coordinate and x_coordinate needs to be combined into a vector3

#

instead of this

#
let mut movement = Vec3::ZERO;
plush slate
#

simply add it into movement

#
movement += camera_transform.forward() * CAMERA3D_SETTINGS.camera_speed;
plush slate
#

otherwise it is like noclip flight ๐Ÿ˜…

plush slate
#

so there is no speed loss due to looking up or down

plush slate
# woeful tiger Why does it even work.

about this:
rotations affect each other. if you rotate your camera downwards first via x axis, then rotate it from y axis to side by side, you change where your y axis points

#

that makes it act weird as it constantly changes your "up" direction

#

but if you rotate around y first, as y is your up direction, it doesn't do that

#

you can then apply your x direction

#

EulerRot::YXZ does that

#

applies rotation in that order

#

so it works

plush slate
#

I think there is nothing left I haven't anwered :P

neat bronze
#

Hi @plush slate I'm new to game dev. Where did you learn about game math rotation, euler, quartenion? Is it experience from another game engine or do you have any youtube or book reference?
Thanks.

plush slate
#

Some are from high school, some are from previous experiences ๐Ÿ˜… Unrelated to rotation but I can suggest this:
https://youtu.be/DPfxjQ6sqrc

This video outlines what I believe are some of the core principles you need to understand to make dynamic computer games, covering vectors, angles and motion. I've tried to present it in such a way that highlights the relationships between these principles, so you can identify when to use one or the other, or combinations of them. It is by no me...

โ–ถ Play video
#

I've also found a playlist way extensive than the video above. I haven't watched it but it looks good:
https://www.youtube.com/watch?v=fjOdtSu4Lm4&list=PLImQaTpSAdsArRFFj8bIfqMk2X7Vlf3XF

Primarily for my students at FutureGames - I will only read chat/superchats during breaks!

Find out more about the school at https://futuregames.se/
โ“ FAQ โฑ https://acegikmo.notion.site/FAQ-8b62a1634746473db702565d890dd3dd

๐Ÿ’– Support me on Patreon โฑ https://www.patreon.com/acegikmo
๐Ÿ“บ I usually stream on twitch โฑ https://www.twitch.tv/acegikmo
๐Ÿ’ฌ...

โ–ถ Play video
gleaming flint
#

for quaternion specifically this video is great https://www.youtube.com/watch?v=zjMuIxRvygQ https://eater.net/quaternions

Go experience the explorable videos: https://eater.net/quaternions
Ben Eater's channel: https://www.youtube.com/user/eaterbc
Help fund future projects: https://www.patreon.com/3blue1brown
An equally valuable form of support is to simply share some of the videos.
Special thanks to these supporters: http://3b1b.co/quaternion-explorable-thanks

Pre...

โ–ถ Play video
neat bronze
woeful tiger
#

so many problems with this movement.

woeful tiger
plush slate
#

abs

#

that's the issue

woeful tiger
#

Removed it, code still works...

plush slate
#

is it solved?

woeful tiger
#

TechoWizard

plush slate
#

so, imagine having a vector pointing fowards towards the Z axis

Vec3::new(0., 0., 1.);

it would be like so, right? Z being 1. that would be your forward vector. taking absolute of it wouldn't change anything as it is positive

#

but if you turn around 180 degrees, look towards back, now your forward vector would be

Vec3::new(0., 0., -1.);
plush slate
#

so, that was the issue

#

but for all components (x, y, z), not just z

#

also, a quick bug I caught

#

if you look towards ground, space or ctrl wouldn't work as you expect

#

because now your new up direction is previous forwards as you rotated towards ground