[Solved] Object facing mouse in 3d

Hello!

I was working on my first game and I ran into another problem. I have isometric view camera and it follows my player. The code to look at my mouse looks like this :

def lookAtMouse(task):
    if base.mouseWatcherNode.hasMouse():
        mpos = base.mouseWatcherNode.getMouse()
        playerActor.setH( mpos.getX() * 120)

    return Task.cont

This is really stupid try, because it only looks x axis, but I ran out of ideas… Thanks in advance.

So you need the code to look at the mouse? You can also twick this to look at your model too.

Heres what I came up with:

          self.md = base.win.getPointer(0)
          self.x = self.md.getX()
          self.y = self.md.getY()
          if base.win.movePointer(0, base.win.getXSize()/2, base.win.getYSize()/2):
            base.camera.setP((base.camera.getP() -  (self.y - base.win.getYSize()/2)*self.MouseSen) % 360)
            base.camera.setH((base.camera.getH() - (self.x - base.win.getXSize()/2)*self.MouseSen) % 360)

You;ll notice I put 360 % in there. This is to keep H and P from increasing beyound the 360 mark.

Hey.

Thanks for the code, I rewrote it to fit in my game and changed it so the player rotates instead of camera :

def lookAtMouse(task):

    md = base.win.getPointer(0)
    x = md.getX()
    y = md.getY()
    if base.win.movePointer(0, base.win.getXSize()/2, base.win.getYSize()/2):
        playerActor.setH((playerActor.getH() - (x - base.win.getXSize()/2)) % 360)

    return Task.cont

But… it actually spins the object as you move the mouse and the cursor can’t move from center. My game is isometric (http://games.softpedia.com/screenshots/Zombie-Shooter-2-Demo_1.jpg), so the code doesn’t really work.

Thanks for taking your time to help me out!

The %360 is absolutely not necessary. It’s no problem for the heading and pitch to go below zero or past 360.

Hey, are you sure? Since it will get higher and eat more memory as you play. Thanks for reply.

I’m still looking for help, since I didn’t come up with any solution.

Hehe. Trust me, an integer won’t eat more memory as it increases. :slight_smile:
In the worst case, it will wrap around when it reaches its maximum value (and you’ll never reach that). On the other hand, a modulo operation is fairly expensive.

How is it expensive? It just limits the number from increasing beyound that limit. A integer is limited by the OS bit (32->1 and 64->1, more or less, it just waste whats left for it to use. (Don’t get me wrong; I really dont know.) It can be reach if the hpr is saved (like in my game) I know cus I’ve hit it lol or I’m perty sure I have.

well modulo operation IS expensive. if you take multiplication or division or addition most of them are single or at least very-few machine-instruction-cycles.
for risc architectures thats usualy one or 2 machine cycles.
a modulo operation has to be realized with many of those. i once had a case where a modulo operation dragged more 80 asm instructions into my code (on an microcontroller). so at least on microcontrollers it’s a noticeable performance hit. on a standard desktop it’s neglectable as long as you dont use it excessivly.

those things aside. rotations usualy are float values anyway so what’s that integer discussion all about? there are very few cases where you would want to keep your rotation within a fixed range.

btw. IF you go with 64bit integers there is no need to worry. even if your character spins with >100000rotations per second you need hundrets of years to get an overrun. 32bit should withstand at least a few days with speeds <60 rotations per second.

Oh, sorry. Of course it’s floats, not ints. Then there’s no need to worry at all for overflows.

Alrighty then. I’ll keep that in mind, thanks. :wink:

EDIT - after lots of time spent trying, I found a way to do it.