3
$\begingroup$

I have an empty and I have set a sphere as its child. When I change the empty's worldPosition(via right-clicking with mouse) the sphere teleports to entirely different location. The strange thing is that when I print the sphere's coordinates it is exactly the same as the empty's and the mouse cursor's. Any ideas?

Here is the module I wrote.

#Import the bge library.
import bge

#Make the mouse cursor visible during game running time.
bge.render.showMouse(True)

#Set the current game scene and python controller to variables.
scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()

#The function "initialization" is responsible for setting all scene objects, object sensors and object actuators to variables.
#These variables are created into the function but are for global use. Their names are the names of the objects/sensors/actuators in blender.
def initialization():
    for k in scene.objects:
        exec("globals()[\"%s\"]=scene.objects[\"%s\"]"%(k,k))
    for k in ground.sensors:
        exec("globals()[\"%s\"]=ground.sensors[\"%s\"]"%(k,k))
    for k in cont.actuators:
        exec("globals()[\"%s\"]=cont.actuators[\"%s\"]"%(k,k))

def mouse_2D_coor():
    global mX, mY
    mouse_pos = bge.logic.mouse.position
    #Cramp the coordinates to what the camera sees. (0, 0) at the top left corner and (1, 1) at the bottom right corner.
    mX = max(0.0,min(1.0,mouse_pos[0]))
    mY = max(0.0,min(1.0,mouse_pos[1]))
    #print ("Mouse coor :",mX,mY)

def empty_to_pos():
    cursor_empty.worldPosition = (mop.position[0],mop.position[1],cursor_empty.worldPosition.z)

initialization()
#print (mop.hitPosition)
if (right_click.positive):
    empty_to_pos()
    print (cursor_empty.worldPosition)
    print ("Mouse",mop.position)
    print ("sphere",sphere.worldPosition)

print ("I reached the end of track.py")

Here is the blend file

$\endgroup$
0
$\begingroup$

The answer came from the blenderartist's forum where the user agoose77 wrote

You need to use

mop.hitPosition

. The position attribute of the base class of KX_MouseFocusSensor, SCA_MouseSensor, returns the pixel position of the mouse, which can reach a value in the thousands.

And it worked.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.