Maxscript: intersectRay

Maxscript: intersectRay

Anonymous
Not applicable
4,723 Views
4 Replies
Message 1 of 5

Maxscript: intersectRay

Anonymous
Not applicable

Hello,

I'm trying to develop a script to get shapes constrained to meshsurface.

Much like Shape:Freehand:Constrain works but for linetool, as i need more precision in my line of work than freehand offers.

I'm getting an error with IntersectRay function,where more and more rays get undefined the farther away in z you move the meshsurface. I probably misunderstood how to use IntersectRay in some way.

I also don't know how to use the rays i get in output. They seem a bit off in precision if the first index is supposed to be points in intersection?.

 

Code:

 

macroscript MovePointToSurface category: "Testscripts"
(
        --create a spline  to be projected. Name it Line001
        --create a mesh with normals facing spline. name it Plane001


    for i = 1 to (numKnots $Line001 1) do
    (
    local raySP = (getKnotPoint $Line001 1 i)
    local rayEP = raySP + [0,0,10000]
    local tempray = (ray raySP rayEP)
    hits = intersectRay $Plane001 tempray
    print hits
    )

 

Thanks in advance

 

/Fred

0 Likes
Accepted solutions (1)
4,724 Views
4 Replies
Replies (4)
Message 2 of 5

miauuuu
Collaborator
Collaborator
Accepted solution

You fire the ray in positive Z direction, so if the knots of the line001 are above the plane001(they have higher Z position) the ray goes in direction oposite to the plane and there is no intersection to be detected.

 

(
	for i = 1 to (numKnots $Line001 1) do
    (
		local raySP = (getKnotPoint $Line001 1 i)
		
		local rayEP = raySP + [0,0,-10000]
		
		local tempray = (ray raySP rayEP)
		hits = intersectRay $Plane001 tempray
		
		if hits != undefined do
		(
			setKnotPoint $Line001 1 i hits.pos
		)
    )
	updateShape $Line001
)

 

 

 

https://miauu-maxscript.com/
0 Likes
Message 3 of 5

Anonymous
Not applicable

I was actually fireing the rays in the correct vector/position, but when changing z to a bigger number, the problem does not occur anymore. So i consider this solved. Thanks!.

 

How did you know intersectray had a .pos?. Where can i read about this?. Is there more ways to use intersectray?, as the values you get is somewhat hard to understand what they are for me.

 

(
    for i = 1 to (numKnots $Line001 1) do
    (
        local raySP = (getKnotPoint $Line001 1 i)
        
        local rayEP = raySP + [0,0,10000000000000]
        
        local tempray = (ray raySP rayEP)
        hits = intersectRay $Plane001 tempray
        
        if hits != undefined do
        (
            setKnotPoint $Line001 1 i hits.pos
        )
    print hits
    )
    updateShape $Line001
)

 

/Fred

0 Likes
Message 4 of 5

miauuuu
Collaborator
Collaborator

How did you know intersectray had a .pos?

 

It is written in the Maxscript Help file:

intersectRay <node> <ray> 

Computes the closest intersection of the ray and the surface of the given node. Returns another Ray which defines the position of intersection in 3D space and the surface normal direction vector at that point. The intersection test respects the face normals of the node. If a face's normal points away from the ray's source, an intersection test is not performed on that face . intersectRay() works if the world state of the node (the state of the node at the top of the node's stack) has a surface such as, an editable mesh, a Standard, Extended, or Compound Primitive, a Patch, or a NURBS surface object. Splines and NURBS curves do not have a surface. Returns undefined if the ray does not intersect the node, the faces it does intersect point away from the ray's position, or the node does not have a surface.

https://miauu-maxscript.com/
0 Likes
Message 5 of 5

remainz
Advocate
Advocate

posting error

0 Likes