Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the orbit target without move the camera #6

Open
happydpc opened this issue Jan 22, 2024 · 8 comments
Open

Change the orbit target without move the camera #6

happydpc opened this issue Jan 22, 2024 · 8 comments

Comments

@happydpc
Copy link

happydpc commented Jan 22, 2024

I have a cad model manipulated by trackball crate, it works nice. But when I want to change the orbit center, the camera moves to forwad to the object target soon. I want to keep camera static when change the target. I used this function to change target:

	pub fn set_target(&mut self, target: Point3<N>){
			let eye = self.eye();
		self.pos = target;
		self.zat = (self.pos - eye).norm();
	}
@n3vu0r
Copy link
Member

n3vu0r commented Jan 22, 2024

Hm, the current behavior of set_target seems wrong, also its documentation is misleading. This should be fixed. Does the following implementation work for you?

	/// Sets target position in world space preserving eye position inclusive its roll attitude.
	pub fn set_target(&mut self, pos: Point3<N>) {
		let eye = self.eye();
		let (new_dir, zat) = Unit::new_and_get(pos - eye);
		let old_dir = Unit::new_normalize(self.pos - eye);
		let rot = UnitQuaternion::rotation_between_axis(&old_dir, &new_dir)
			.unwrap_or_else(|| UnitQuaternion::from_axis_angle(&self.yaw_axis(), N::pi()));
		let rot = rot * self.rot;
		*self = Self { pos, rot, zat }
	}

@happydpc
Copy link
Author

Have tried yet myself, with this rot, still moving to the center of the view. that's trange, can't figure out what's wrong.

UnitQuaternion::rotation_between_axis(&old_dir, &new_dir)

@n3vu0r
Copy link
Member

n3vu0r commented Jan 22, 2024

Ok, maybe I misunderstood you. Do you just want to move (translate) your cad model around? This can be achieved with the slide method. Try it with slide(new_target - old_target) or maybe slide(old_target - new_target).

@happydpc
Copy link
Author

I want to keep the camera static, when changing the orbit center. When a object clicked, the orbit center is just assigned by the object center, no camera move. But now the camera moves with a click.

@n3vu0r
Copy link
Member

n3vu0r commented Jan 22, 2024

The camera itself cannot store an orbit center (i.e. target) without looking at it. If you want to assign a new target without moving the camera, you have to store the new target separately and only apply it when you want the camera to move.

The camera stores three elements:

  • pos: Point3: Target position in world space (where the camera is looking to).
  • rot: Quaternion: Eye rotation from camera to world space around pos. In camera space, the eye is always at Point3::new(N::zero(), N::zero(), zat).
  • zat: N: Distance from pos to eye, the radius of the orbit.

@happydpc
Copy link
Author

Oh I see, I can use the second target when orbiting. But the logic will be complicated. If I can make the new look matrix just as it should be. The camera may mot move.

@n3vu0r
Copy link
Member

n3vu0r commented Feb 2, 2024

Have you found a solution? I would try to use the Bevy ECS to store the target assignment as part of the camera bundle which can then be reassigned when clicking an object.

@happydpc
Copy link
Author

Havent solved yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants