Is it possible to make an object track-to the mouse cursor? I know a good bit of Python, so coding isn’t a problem… Plus, I’d love to learn something new!
How do you want it to track? In 2D or in 3D? If 3D, do you want it to point toward the ray target (away from the camera) or toward the ray source (toward the camera)? The mouse over any sensor (KX_MouseFocusSensor) has raySource and rayTarget attributes which you can use to track in 3D.
I just want a turret to track to the x and y positions of the mouse… Which one would I use for that?
The whole point is that the mouse returns 2D with x and y. In 3D it represents an infinite line not a point.
So you need to define a third coordinate on that line. So you could choose a fixed z value, or the point where the line hits a face (mouse.hitPosition) etc…
Look at the S2A. It contains functions to set the position of an object to the hitPosition of an sensor (In your case mouse over any sensor). There is no need to learn the code, just use it. It includes a very short tutorial and a demo.
How do I install it? This looks great, btw.
Sorrry, but I just don’t see how I could get this onto my computer! There are probably directions that I missed, but…
download the file
copy the text S2A.py to your blend file or place it as external file to the folder where your .blend is located.
add Logic bricks as described in the above thread. Make sure to use “Module” rather than Script on the Python controllers.
You can look at the different layers of the demo file to see how the logic is configured.
You need to actuate one layer from top row and one layer from buttom row, to see an effect.
The top row layers contain some testobjects
(<1>- arrow keys move this object, <2>- added objects, <3> - moving cube)
The buttom row layers contain objects demonstrating different functions.
<sift><alt><1>-hitObjectToObject - works with <1> (not so good with <2>,<3>)
<sift><alt><2>-hitNormalToObject - works with <1> (not so good with <2>,<3>)
<sift><alt><3>-closestHitObjectToObject - works with <1>,<2> (even good with <1>,<shift><2>)
<sift><alt><4>-hitDirectionToAlign + hitPositionToPosition - works with <1> (not so good with <2>,<3>)
<sift><alt><5>-hitNormalToAlign + hitPositionToPosition - works with <1> (not so good with <2>,<3>)
<sift><alt><6>-closestHitObjectToObject - works with <1>,<2> (even good with <1>,<shift><2>)
<sift><alt><7>-raySourceToPosition doesn’t matter
<sift><alt><8>-rayTargetToPosition doesn’t matter
AH! I don’t get a module controller option!
Python controller in Module mode.
UGh. I apologize, but I simply cannot understand how to work this! I have everything set up. I just don’t know which things to type in! In your file you have empty script controllers, not module controllers.
You see the first picture in the S2Athread?
Step by step:
- Open the S2A.py in the text editor or place it in your .blend folder.
- Add an empty call it trackTarget.
- Add to your tracking object (e.g. tracker) an actuator tracking trackTarget. Enable 3D. Make sure the actuator is activated (an Always sensor without pulses -> AND -> trackTo should be enough ).
- Add to trackTarget an Mouse Sensor in MouseOverAny mode. Enable True Pulse (to get it updated when moving the mouse cursor).
- Add to trackTarget an Python controller switch from Script to Module. Enter S2A.hitPositionToPosition as Module.
- Add any actuator. It doesn’t matter what type is, it will not be activated. The position of the actuators owner will be set to the hitPosition of the mouse sensor. (The actuator might be on an other object)
- Connect the mouse sensor with the ython controller
- Connect the python controller with the actuator.
- first the tracker will track the empty trackTarget at its initial position.
- when the mouse is over an other object the trackTarget will move to the point the mouse cursor is pointing to.
- the tracker will follow the trackTarget.
- the trackTarget will not be moved if the mouse is not over an object!
- the current view must be perspective (e.g. looking through the camera). Mouse over does not work in orthogonal mode.
If you want to see the position of trackTarget, parent any visible object to it (e.g. a sphere). Make sure the spheres faces have collision disabled in edit mode!
Optional, if you want to show the mouse cursor:
Open the Mouse.py in the text editor or place it in your .blend folder.
To any object (I recommend the camera) add following logic bricks:
an always sensor (no Pulses) connected with
an Python controller in Module mode with Mouse.show
I hope it helps