If have got a question about coördinate finding in 3D with Blender:
Situation:
You are looking at a camera-view in Blender.
You look on a plane.
The camera does not look perpendicular on the plane; so the plane it is tilted with respect to the camera.
Your mouse-cursor is visible but not at the center of your view (not on the Z-axis of the camera).
It does however point at some point on the plane.
Question:
How can I calculate the 3D-position (x,y,z) of the point of the plane where the mouse-cursor is pointing at (global/local)?
with a mouse senor attached to the script (named “mouse” in this case)
mouse.getHitPosition()
[edit] and kller, that’s to find where the mouse is on the screen, which is rather pointless in this situation, unless you want to manually cast a ray from the camera, using the mouse x and y positions to get a vector passing through some position in the camera’s viewing frustum (that’s the right word, right?) and get the position with that…
I think the Captain is rigth; getMousePosisionX gives the mouseposition on the screen.
This can be usefull but is not what I am looking for.
Hi Captain,
This looks promising, but I think I realy have to learn to use this.
I tried the following this morning.
I attached this scipt to a mouse-sensor (of the camera), the sensor reacting to event = left button:
import Rasterizer as R
import Blender as B
from Blender import Scene
from Blender import Window
import GameLogic as G
R.showMouse(True)
cont = GameLogic.getCurrentController()
own = cont.getOwner()
mouse = cont.getSensor(“MSens”)
if mouse.isPositive(): HitPosition = mouse.getHitPosition()
I got an attribute error getHitPosition().
I think I need some more help here.
Thank you both
Jbal
did you put the hitPosition on the same line as the if statement? it has to be on the next line down, indented one level. in python, whitespace matters.
also, gamelogic and blender are imported automatically when the game starts.
Yes, the text after the if-statement is indented (I just can’t get it intended properly using te text-editor of this forum).
So this can’t be the problem.
When I put other commands after the if-statement (all indented properly) these commands are run.
Also if there is just one command after the if-statement you can put it on the same line as the if-statement. It will be run on If = true.
Jbal
I think now the only mouse sensor having a getHitPosition() method are the Mouse over and the Mouse over any mouse-sensors.
I tried these sensors and this time I got no attribute error.
Then I Tried again my first method and yep there was the error again.
But also these sensors don’t do the job for me, both of them only give a pulse to the controler-brick when the cursor gets over an object or leaves it again;
Then getHitposition nicely gives the position where the cursor hits or leaves the object (in local coördinates).
So there is no signal when the mouse-cursor stays over the object but in different places and thats is just what I need to get the mouse-position from.
You can get the hit position of a ray with ray.getHitPosition() .
A mouse over, or mouse over any sensor will provide you with a ray. I suggest using a mouse over any sensor for most situations.
# This script should be attached to an always sensor,
# and a "mouse over any" sensor.
import GameLogic as gl
con = gl.getCurrentController()
own = con.getOwner()
mray = con.getSensor("mray")
if mray.isPositive():
hitpos = mray.getHitPosition()
# There you have it, the hit position of the mouse ray.