Mouse Hit Point - RayCast
Hey guys,
how to get the first object ray beam collision with it ? (not another objects such as objects are in back side of front object).
rollout myrollout "hover" (
listbox lb
local theTimer = dotNetObject "System.Windows.Forms.Timer"
fn showundermouse=
(
-- RAYCASTIN JOOOOOOOOOOOOOB HERE
ry = mapScreenToWorldRay mouse.pos
myray=ray ry.pos ry.dir
local intersect = intersectRayScene myray
if intersect.count>0 then
(
print intersect[1][2].pos
)
)
on myrollout open do
(
dotnet.addEventHandler theTimer "tick" showundermouse
theTimer.interval = 100
theTimer.start()
)
on myrollout close do
(
theTimer.stop()
theTimer.dispose()
)
)
createdialog myrollout
Comments
plane-line intersection
I am not aware of something already in maxscript that would let you do that, so it looks like you're gonna have to make it from scratch.
What I would do is individually take the bounding box of every object in the scene and use an imaginary plane that is coplanar with the side of the bBox who's normal vector is pointing most towards the camera.
To get the normal of the imaginary plane, do the cross product of two edges(vectors) of the bBox that are touching.
To find which normal is pointing most closely do the dot product of the look vector of the viewpor (I think its the 3rd row of the camera orientation matrix: getViewTM()), and the imaginary plane normal. Whichever one of the dot products is the smallest is most closely aligned.
At this point you will have only one plane to test per object.
Use the function bellow to see where your mouse click intersects with the imaginary planes of each object. By filtering out the collisions that are outside the bounds of the object your are testing (because the size of the planes rnt finite), and by testing which collition is nearest to the viewport camera position, you will be able to find which node is under your mouse click.
Sry if I made it sound more complex than it was. lol