2.56 Mouse hit location

In 2.49 I had a setup with a plane. When you clicked on the plane an object was added at the point you clicked on. Unfortunately I don’t have the blend anymore, and it was awhile ago so my memory is failing me :o but I think it was accomplished with a function like getMouseHitPosition, or something similar. What I need to know is if there’s an equivalent function in the current (2.56) API?


import bge
G = bge.logic
cont = G.getCurrentController()
own = cont.owner
moa = cont.sensors['MouseOverAny'].positive # Mouse Sensor

if moa:
              own.worldPosition = moa.hitPosition

…one way to do it…:yes:

edit:

You must have a Plane, where the Mouse can get the hitPosition from. moa must be a Mouse-Sensor set to Mouse-over-Any

EDIT: That’s good, except cont.sensor needs to be cont.sensors.

Now it says there’s no attribute hitPosition.

Alright, using cont.sensor[‘MouseOverAny’].positive returned an integer; 1 if it was over something 0 if not. Changing that to cont.sensor[‘MouseOverAny’] got the desired result. I’d call this problem solved :). Thanks sevi_.

I have another question. I have a ground plane. I have a cube on top of it. Using a MouseOver sensor on the plane the cube goes wherever the mouse is over the plane. When the mouse is over the cube it does nothing.

I move the mouse off the cube onto the plane. The Cube jumps to that position. repeat x times. This causes an ugly jumpy motion. Any way to make MouseOver ignore the cube?

sorry, I am new to the BGE…so forgive me if I am wrong…about any of this…
these are the steps I would take…but I do not know the commands in BGE py…
1)get the screen location of mouse pointer.
2) create a relative world vector based on mouse posistion
(convert it to 3d space by giving it’s 3rd vector value from the camera view)
3)cast a ray into the scene along the cameras normal(local z I think)
4)return a value if something is hit…(don’t know if code returns values/pointers etc from raycasting internally)
of course this all sounds fine and dandy, but doing it may require additional coding…like I said…I have no clue in BGE…
this is all theoretical.

Oh well, you’re right, cont.sensors[‘mouseoverany’].positive doesnt work for that… but you find it out.
The problem with the jumping effect is clear:

Because the Cube goes everywhere, where the Mouse gets a hit Position from the ground, it will not move, having the mouse over the cube…
To solve this, either make the Cube ‘no collision’ (even the faces), or parent it to an empty wich follows the hit Position… i dont remember exactly how i did it…

if found a script from an old project i was working on, you could try:


def Grab(cont):
        own = cont.owner
        moa = cont.sensors['moa']#MouseOverAny
        lb = cont.sensors['lb'].positive
        lbt = cont.sensors['lbt'].positive
        mho = moa.hitObject
        x = moa.hitPosition[0]
        y = moa.hitPosition[1]
        z = moa.hitPosition[2]
        own.position = (x,0,z)
        #print (len(own.children))
        #print (own.children)
        if moa.positive:
                if 'moveable' in mho:
                        if mho['moveable'] == 1:
                                if lb:
                                        if len(own.children) < 2:
                                                mho.setParent(own)
                                                #print ("Mouse.Grab, set Parent")             
                                else:
                                       mho.removeParent() 
                        

Attach this to a Modul-Controller from an Empty with all needet Sensors, or use it as Script and add:


Grab(cont)

to the script…

Every Object with the Property ‘moveable’ can be moved with that.
For 2.5 you should probably change own.position to own.worldPosition, and anyway dont forget to define ‘cont’ using it as a script (Module not neccesary).

Good luck

Setting it to No Collision works great! Thanks again sevi_.