Controlling avatar with the mouse

Hello :wink: (here I am with an other question)

The goal: if the player is walking and mouse (camera control) is moving left or right then the player will continue walk changing hi heading like the camera.

I thought that it will be easy to perform by a simple:

self.myPlayer.setH(base.cam.getH())

but apparently not.

The result is a strange behaviour with the player orientation which is not perpendicular to the ground anymore! It’s like I’ve changed the whole HPR while I’d only modified the heading!

I can’t understand why and this is blocking me for going further!!!
Did anyone faced the same thing ?

If ll you do is rotating the camera the same amount as the player, then unless the players and cameras position is exactly the same you wont get good results.
A crude sketch:

The red thing is a player the green a camera (with it’s view), before and after rotating 90deg.

I’ve found that reparenting the camera to the avatar works quite good for a simple camera. You just move the avatar and never even touch the camera. Changing the angle of the camera is then just a mater of changing its position relatively to the avatar and calling LookAt.

Your approach is the very first one I tested and is also leading to other questions but maybe I would have explained how I set up my camera system at the begining :slight_smile:

I attached and reparent an empty to my player. Then, I reparented the cam to the empty and here I played with some lookAt(myPlayer) . All this is working perfectly when I rotate my mouse.

1 - The problem is when I’m adding the line:

self.myPlayer.setH(base.cam.getH())

then everything is turning bad as explained in my first post!

2 - Wezu’s post brings an other question:
How to reparent an object to an other for the location only (not the rotation) ?

reparenting changes the parent only, the local transforms relative to the parent are untouched , but since the parent usualy has different ones it will affect the node you parent around,too.

if you want to keep the node in place (and in rotation) you can do

yourcamera.wrtReparentTo(yourplayer)

i cant really follow your exact setup. but usualy things look like this

render->player->camera.
or if you want the camera to be able to rotate around the player.
render->player->dummy->camera.

the dummy is in the same spot as the player, the camera is moved into the spot you want. you propably want to rotate the dummy in this case.

just guessing,thought.

This is exactly my situation where problems start when I want to rotate the player without rotating the cam!

for example, let’s assume I’m pushing the left button and I want my player to walk left direction but camera keep filming forward (doesn’t rotate), I used this which works:

self.myPlayer.setH(self.myPlayer, 90)
self.dummy.setH(self.dummy, -90)

But after that, when I’d like to go forward again (the same direction camera is filming), I should then obviously use something similar to :

self.myPlayer.setH(base.cam.getH())

and here the result is a disaster :slight_smile:

Some will suggest to rotate the player again by 90 degrees but one of the goals here is also to make the player rotate as the camera while he’s walking…
I mean one of the things I’d like to do is assigning the camera direction when the player is walking, like this if the mouse is getting moved → then the player will continu walking following the new heading of the cam.

you can set rotations relative to other nodes.

you can do

yourplayer.setH(yourheading)
yourdummynode.setH(render, 0)

this way you can set the dummynode rotation independantely. if you want it to follow the player rotation just omit the render part. this also works for rotations relative to the node itself

Hmm… What about something like this:

baseNP = render.attachNewNode(...)
# Or attached wherever makes sense in your project, presumably

player = ...
# set up your player, for example loading a model
player.reparentTo(baseNP)

base.camera.reparentTo(baseNP)

# Now, when you want to turn the character, turn "player":
player.setH(...)

# When you want to move the player, do so by moving baseNP,
#  not player.
# Try this; if it doesn't work, you might end up
#  doing it "manually":
baseNP.setPos(player, Point3(0, someDist, 0))

#When you want to rotate the camera, just rotate it:
base.camera.setH(...)

Note that you might have preferable results by placing a node or two between the camera and baseNP, allowing you to, for example, offset it more easily, I think.

I do it like this, and never bother about the camera again (not even if something gets in the way and you can’t see a thing :smiley: ):

base.disableMouse()
base.camera.setPos(self.player, 0,4,4)
base.camera.lookAt(self.player)
base.camera.reparentTo(self.player)

As simple as cheese.