trace ray from camera

I would like to trace a ray from the camera, through the cursor, and find the intersection point on a object.
here is a picture to demonstrate what I mean. The blue circle is the cursor.

This will require python but it is still doable. Look at Social’s FPS Template for a great example of casting a ray then finding the collision point. (That is where the bullet hole is placed after the gun is fired) So just have the ray fire from the camera then test for the position.

Hope that helps.

Like LDS_Marine said, you could use a ray sensor and then use python to get this hit position from the ray. There is a more mathematical way to do this too, but you would need superb calculus skills.

as long as you want to trace through the cursor just use the python command mouseSensor.getHitPosition()- this returns the position of the ray intersection. you can use getHitNormal() too, if you want the surface normal of the intersected face (for example, if you want the cursor to stick out perpendicular to the surface the mouse is over)

first of all, you need python. If you know a little python, try connecting a “mouse>over any” sensor to a script and use dir(your sensor) for a list of useful stuff.
I think there is a ray origin you can get, a ray hit position, etc.

edit:
Captain Oblivion beat me XD

Big thanks, I am about to try it now!

It works like a charm!
Now to get that rotation working…

Now to get that rotation working…

hmm…
The ray sensor (or the rayCast() function) should do everything including rotation.

By the way, what are you trying to make? Ray tracing shadows, field of depth?

I am trying to make a tank-like game, you will control the tank with the arrow/wasd keys, and you aim the turret by pointing the mouse at a point on the ground.

I am trying to make a tank-like game, you will control the tank with the arrow/wasd keys, and you aim the turret by pointing the mouse at a point on the ground.
Oh, I see. I suggest that you use the mouse sensor instead and just use the mouse.getHitPosition() feature. That will do everything you need in less than 2 lines of code.

Edit: I went ahead and made a blend file that does everything you need.

Attachments

mouseTrace.blend (132 KB)

thanks, I all ready got the movement and rotation by using some code from the FPS demo, so I guess the problem is solved.

Thanks to all