# Pitch never appears to get outside of range -90 to 90, but still rotates?

Im very confused, im trying to clamp a free look camera so it stops in the range -90 to 90.

The problem is that the pitch never gets outside of that range despite it continuing to rotate.

Let me explain. I increase pitch relative to mouse movement in the y. It rotates as it should. However, once looking straight up at 90 degrees, it starts to decrease despite adding more! How is this possible?

One would expect that continuing to increase pitch would result in a number increasing beyond 90, more baffeling is if i try to setP(95) just straight up, its value is 85???

So to get around this, i have to just a projected pitch value before setting and then essentially manually clamp. Can anyone explain to me whats going on here?

``````def _handle_gimbal(self, task):
if base.mouseWatcherNode.hasMouse() :
x = base.mouseWatcherNode.getMouseX()
y = base.mouseWatcherNode.getMouseY()
delta_x = self.last_mouse_x - x
delta_y = self.last_mouse_y - y
self.last_mouse_x = x
self.last_mouse_y = y
if self.input_map["right_mouse_down"]:
self.gimbal_x.setH(self.gimbal_x, delta_x * 10000 * globalClock.get_dt())
pitch = -(delta_y * 10000 * globalClock.get_dt())
projected_pitch = self.gimbal_y.getP() + pitch
if projected_pitch > 90:
self.gimbal_y.setP(90)
elif projected_pitch < -90:
self.gimbal_y.setP(-90)
else:
self.gimbal_y.setP(self.gimbal_y, pitch)

velocity = Vec3()
if self.input_map["w"]:
velocity += Vec3.forward()
if self.input_map["s"]:
velocity += Vec3.back()
if self.input_map["a"]:
velocity += Vec3.left()
if self.input_map["d"]:
velocity += Vec3.right()
self.gimbal_y.setPos(self.gimbal_y, velocity * 250 * globalClock.get_dt())
self.gimbal_y.wrtReparentTo(self.render)
self.gimbal_x.setPos(self.gimbal_y.getPos(self.render))
self.gimbal_y.wrtReparentTo(self.gimbal_x)

``````

As an added bonus, if you can also tell he how to rotate about the global z axis that would be great, then I can get rid of this whole parenting / parenting x thing im using to do heading.

I figured out the second part, i just move gimbal_x relative to gimbal_y instead.

Iâ€™m not sure about your first issueâ€“looking back at an old project of mine, I used a similar approach to the one that youâ€™re using.

Regarding the second issue, you say that youâ€™ve figured it out, but as you donâ€™t say what solution you found I would like to note the following just in case:

One can rotate around the global Z-axis simply like so, I believe:
`myNodePath.setH(render, newRotation)`

(Where, should you want to offset the NodePathâ€™s rotation, â€śnewRotationâ€ť can be something like â€ś`myNodePath.getH(render) + someValue`â€ť.)

Also, just to check, do I take it correctly that youâ€™re no longer using the sequence of calls to â€śsetPosâ€ť and â€śwrtReparentToâ€ť at the end of your original code? If so, then thatâ€™s good to read! (That approach was arguably not incorrect, but it was a bit convoluted, I feel.)

(By the way, please note that you can edit your posts to include new information.)

Thank you for response. Yes, ive solved all issues, but, am still terribly confused by the pitch number.

I solved the re-parenting mess with:

``````self.gimbal_x.setPos(self.gimbal_y, velocity * 250 * globalClock.get_dt())
``````

Essentially moving x with respect to ys coordinate space, I think this works because they effectively share the exact same heading (y is a child of x), but I also needed to travel freely along the forward axis of y without rotating y. So positioning is done with respect to y, but rotation is done only on the heading of x.

Code For â€śFree Roamâ€ť camera with pitch constraints:

``````def _handle_gimbal(self, task):
if base.mouseWatcherNode.hasMouse():
x = base.mouseWatcherNode.getMouseX()
y = base.mouseWatcherNode.getMouseY()
delta_x = self.last_mouse_x - x
delta_y = self.last_mouse_y - y
self.last_mouse_x = x
self.last_mouse_y = y
velocity = Vec3()
if self.input_map["w"]:
velocity += Vec3.forward()
if self.input_map["s"]:
velocity += Vec3.back()
if self.input_map["a"]:
velocity += Vec3.left()
if self.input_map["d"]:
velocity += Vec3.right()
if self.input_map["right_mouse_down"]:
self.gimbal_x.setH(self.gimbal_x, delta_x * 10000 * globalClock.get_dt())
pitch = -(delta_y * 10000 * globalClock.get_dt())
projected_pitch = self.gimbal_y.getP() + pitch
if projected_pitch > 90:
self.gimbal_y.setP(90)
elif projected_pitch < -90:
self.gimbal_y.setP(-90)
else:
self.gimbal_y.setP(self.gimbal_y, pitch)

self.gimbal_x.setPos(self.gimbal_y, velocity * 250 * globalClock.get_dt())
``````
1 Like

`myNodePath.setH(render, newRotation)`

So I tried this originally and it starts to rotate but this like â€śfightsâ€ť the rotation. Idk how to best explain that. It like â€śjittersâ€ť and remains looking straight ahead

Yup, that looks good to me!

Iâ€™d probably handle the matter of â€śmoving in the forward directionâ€ť a little differentlyâ€“but your approach looks at least as good as mine, at a glance!

Looking further at the results of changing pitch, it looks like what happens is that the heading and roll values change as one passes a pitch of 90. Iâ€™m guessing that this is some artefact of either Euler angles (that is, the â€śheading, pitch, rollâ€ť system) or converting to Euler angles from the (presumed) internal representation.

I think that youâ€™d find that no such issue would present if the code were rewritten to use quaternions, instead.

I think that youâ€™d find that no such issue would present if the code were rewritten to use quaternions, instead.

I considered this but I have no earthly understanding of the concept and by extension panda3ds version. =)

Thatâ€™s very fair! I make only sparing use of them myself, and my own understanding is limited, I fear! But I wanted the point mentioned as it is an approach in which the problem in question doesnâ€™t show up, I do believe!

1 Like