5
$\begingroup$

I'm a (very new) beginner in python, and I was trying to follow this older technique for getting mouse location in 3D space in the Game Engine.
However, I can't get it to work and I don't know what could be wrong.

My question is, what did I do wrong? (Or if that is not the case, other ways to achieve this?)

Here is my blendfile

$\endgroup$
0

2 Answers 2

7
$\begingroup$

Welcome to the world of programming. When you are writing scripts the first rule is they are very picky, every word has to be an exact match(case sensitive) and punctuation needs to be in the right place.

Running blender from a terminal window can help as you will see error messages that can help you find the problems - at least the line numbers will help you focus on where python finds the error. I'd recommend you start from a terminal and use the errors to try and see how I found the following fixes.

Not sure it's a big thing but you have the same logic bricks setup for the plane as well as the lamp.

To get you started - in MouseRay.py -
Line 13 the plane after the = is a call to the Plane class above - it needs to use a capital P
Line 15 needs a : at the end
Line 17 needs () after the function name
Line 24 ray.p0 should be ray_p0
Line 27 intersect_lin_plane should be intersect_line_plane

As you see they are very small picky things but you need to specify exactly what you are referring to each time. You have a class called Plane and a variable named plane and an object called plane.004 which could be named plane. Python needs to know exactly which plane you refer to at any given time. It also highlights the need to use helpful distinct names for everything. Even if python can tell which one you are referring to given the context - can you? without spending 5 minutes ticking off what refers to what?

$\endgroup$
2
  • $\begingroup$ case sensitive doesn't apply to the <em>whole</em> programming world ;) $\endgroup$ Commented Nov 27, 2016 at 3:21
  • $\begingroup$ No it doesn't, but we aren't talking about the whole world - here we are only talking about using python inside blender - it is case sensitive. $\endgroup$
    – sambler
    Commented Nov 27, 2016 at 8:49
5
$\begingroup$

I like to use a python-only approach to compute the mouseToWorld transform:

from bge import logic

def mouse2world():
    cam = logic.getCurrentScene().active_camera
    vec = cam.getScreenVect(*logic.mouse.position)
    camPos = cam.worldPosition
    projectedPos = [0,0,0]
    z = 10      # user-set depth
    projectedPos[0] = camPos[0] - vec[0] * z
    projectedPos[1] = camPos[1] - vec[1] * z
    projectedPos[2] = camPos[2] - vec[2] * z

    return projectedPos

Link to sample file

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .