orienting a joint to look at mouse?

I’m trying to get my character to point his gun at the mouse. The view is top down, like zelda. I tried using the setH() parameter, like in the looking and gripping tutorial, but it only works when the character is facing south. If facing north, the controls are backwards.

if base.mouseWatcherNode.hasMouse():
            mpos = base.mouseWatcherNode.getMouse()
            self.character.LEFTARM.setH((mpos.getX() * 100))

How would I make it so the gun faces the mouse independently of the character’s direction. lookAt() doesn’t work, because the getMouse() coordinates are from -1 to 1, and lookAt wants real 3d coordinates. Can I attach a node to the mouse somehow?

Thanks for any answers!

Have you seen the “Looking and Gripping” sample program? It does exactly what you want.

That method only works for one direction. If the character turns around, then so do the controls! This makes sense, because it is based on relative direction, but it isn’t what I want. The controls must be independent of character direction, and the gun should always be pointing at the mouse.

Check out the game here. You’ll see what I mean!

Sounds like you need to tell one or more of your transforms what coordinate space to be working in. That, or doing some basic trig on your own to compensate for view/character angle.

Yeah, Looking-and-Gripping.py will show you how to directly translate a mouse’s relative -1,1 coordinates into a pitch and heading.

If you want a more generalized system, such as a loose shoulder-cam view that allows you to aim wherever you point your mouse cursor in the 3d world, you can cast a ray. Do it from the mouse’s reverse-projected coordinates into your world (setFromLens) and then you can get world coordinate of where you are aiming. I am using this method in a project where I want an aiming direction that doesn’t always match the character’s forward direction.

From the collision generated by the ray, you can then use the standard Panda lookAt method to get a rotation towards your aim point.

Of course, it sounds like your requirements are simpler. If the camera will always be behind the character, then I would just use the Looking-and-Gripping method and skip ray casting.

I think I will have to do the rays, because I want the character’s gun to shoot toward whatever the player clicks on. Unfortunately, the camera isn’t behind the character. I’m setting up a fixed camera with a top down view onto the characters.

So, I need to set up a collision plane at the height of my character that a ray from my camera will shoot at? I guess this page will help: panda3d.org/manual/index.php/C … 3D_Objects

When I detect a collision, how do I shoot a bullet from the character to the collision point? In pygame, I could just calculate the slope between the two objects. Is there a better way in panda3d, using vectors maybe?

Thanks for your help everyone! Panda3d is great, but the forum members make it ten times better. I’ve never had this much fun making games before.

You can do the math yourself, but it’s easier to let Panda do it for you. :slight_smile:

You can do something like:

bullet.lookAt (rayIntersectPoint)

This will orient your bullet (in the Y-forward direction) towards the point that your ray-with-plane-intersection test returned. (Assuming bullet and point are both in world coordinate space.)

From there, you can just move the bullet along its forward direction with something like:

deltaVel = bullet.getQuat().getForward() * speed * dt
bullet.setPos(bullet.getPos() + deltaVel)

great! I got panda to give back real world coordinates using a ray that is normal to the collision plane I put in. The camera itself can be placed anywhere in the scene. You can check it out here, if you like.

http://sites.google.com/site/misterhawksfarm/Home/legends/files/mml15.zip?attredirects=0

Thanks for the code on getting the bullets to work. That’s exactly the kind of implementation I was looking for.