Mouse Object Script

I’m working on a script that will cause an object representing the cursor to move along the plane defined by X = 0 in the local coordinates of another object (the character). This object that follows the cursor is able to add objects to the scene, like some kind of magical conjuring.

I have a working script that does this along the plane defined by X = 0 in the world coordinates. The script is called “avatar” in the attached blend file. Here it is for reference:

cont = GameLogic.getCurrentController()
own = cont.getOwner()

over = cont.getSensor(“over”)

if over.isPositive():
pos = over.getHitPosition()
own.setPosition([0,pos[1],pos[2]])

It works nicely. The problem is that I want to give the player the ability to move the character from left to right instead of running along a track, with the same ability to conjure objects directly ahead in the sky and on the ground. I need to know how to convert the mouse position in the character’s local coordinates into world coordinates and pass those to the cursor object, so that it can set it’s own position. This is the best way that I can think of to accomplish what I need. Here is what I have so far:

cont = GameLogic.getCurrentController()
own = cont.getOwner()

over = cont.getSensor(“over”)

if over.isPositive():
pos = over.localposition()
targetlocal = ([0,pos[1],pos[2]])
targetworld =

The old script uses some deprecated functions, so I am trying to update them, but I’m not sure if I’m doing that right. I would pass “targetworld” to the cursor object, and set it’s position to that.

Here is the blend file. It is just an alpha. Eventually, there will be a snowboarding Yeti.

Thanks for any help.

Attachments

yetiprototype005.blend (808 KB)

Hopefully this is close enough:


from Mathutils import Matrix, Vector

cont = GameLogic.getCurrentController()
own = cont.owner

over = cont.sensors["over"]

if over.positive:

    # Get character object from scene object list by it's name
    charobj = GameLogic.getCurrentScene().objects["OBYeti"]

    # Get character object world position and
    # orientation as a vector and a matrix
    # Note: Vector and matrix conversions not needed for 2.5
    charpos = Vector(charobj.worldPosition)
    charori = Matrix(*charobj.worldOrientation)

    # Get mouse cursor hit position, which is a world position
    # and convert it to a local position in space of charobj
    worldhitpos = Vector(over.hitPosition)
    localhitpos = (worldhitpos - charpos) * charori

    # Set the local x-position to zero
    localhitpos.x = 0

    # Convert back to world position and
    # set target (owner) position to it
    targetworldpos = charori * localhitpos + charpos
    own.worldPosition = targetworldpos

That attached to a mouse sensor with positive pulse mode enabled and you can get rid of the Always sensor.


I might be missing something here, most likely the whole point, but why not parent a ghost plane to the yeti and use the first script to set the target position on that one…?

Thanks, Supersocks. You are absolutely right about the ghost plane. That occurred to me last night. I’m new at this, and I came about this dilemma through the back door.

The game was conceived as a side-scroller after “Lucidity”, where the player doesn’t control the character but helps them by adding objects to the environment. I realized that the snowboarding aspect of the game would benefit from a slightly more forward-looking point of view, as some of the jumps are very large - jumping from mountain top to mountain top. And once you are looking forward, the player is going to expect to move side-to-side. So, I convinced myself that I’d have to apply some of the matrix algebra I’ve been teaching to myself.

Now, thanks to you, I know about the parented ghost plane, and the code to translate from local to world coordinates. In the future, I will be able to crunch some numbers on missiles and such.