Rotate Gun to target

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
jack1604
Posts: 1
Joined: Wed Oct 10, 2018 1:41 pm

Rotate Gun to target

Post by jack1604 »

Hello,
Please help me solve the problem.
Player in my game have a gun. first point of gun is A(2, 1, 0) , last point of gun is B(0, 1, 0), target point is C(5, 5, 5)
How to rotate first point of gun around point B to line BC to aim target
sorry for bad english
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Rotate Gun to target

Post by Norbo »

There are a variety of ways to handle this depending on the kind of behavior you want. There is no single 'correct' way to point the gun, both in terms of math (an infinite number of valid rotations) and in terms of gameplay design (maybe you want some animations or something). This also doesn't sound specific to bepuphysics or physics engines, so you might have better luck looking at other more focused resources or forums.

That said, conceptually there is a direction in the local space of the gun that you want to align with a world space direction. One option would be to compute the shortest rotation- usually whatever math framework you're using will have some kind of function that takes a start direction and end direction and outputs the shortest rotation between them. The shortest rotation isn't always the one you want, though- it might end up introducing a roll. In that case, you could consider computing a rotation around the up axis so that it's aiming properly horizontally, and then computing another rotation that pitches the gun up or down to aim at the target.
Post Reply