[Closed] intersectRay issues
First off, I should note that I’m aware that what I am doing can easily be done without scripting, and the PRT Volume, available in the eval version of Krakatoa. However, I’m just having a bit of fun here, while learning some basic MAXScripting.
Ok, so I’ve created this code, which is running in a PFlow Birth Script. To my understanding, this code should by creating particles only where it is inside the volume (Said volume is a sphere, converted to a Mesh.). Instead, all positions in the grid evaluate to undefined, and I get a full grid of particles, as seen below.
on ChannelsUsed pCont do
(
pCont.useTime = true
pCont.useAge = true
pCont.usePosition = true
)
on Init pCont do
(
--space between voxels
global VOXSPACING = 6
--use a box as bounds for voxels
global getBounds = nodeGetBoundingBox $voxCage (matrix3 1)
global cageSize = (getBounds[2] - getBounds[1]) / VOXSPACING
)
on Proceed pCont do
(
t1 = pCont.getTimeStart() as float
if t1 < 0 do --spawn before frame 0
(
--step through the grid of voxel positions
for x in 0 to cageSize[1] as integer do (
for y in 0 to cageSize[2] as integer do (
for z in 0 to cageSize[3] as integer do
(
--test a ray from the current vox position to the sphere center
global testRay = intersectRay $sphere01 (ray [x, y, z] $sphere01.pos)
--if inside mesh create particle at position
if testRay == undefined do
(
pCont.AddParticle()
pCont.particleIndex = pCont.NumParticles()
pCont.particleAge = 0
pCont.particlePosition = getBounds[1] + [x, y, z] * VOXSPACING
)
)
)
) --for x
)
)
on Release pCont do ( )
How can this be? If a ray is shooting from the outside of the mesh, to it’s center, it should hit the the mesh, and test true. If the ray originates from inside, it should hit a backside face, and test undefined. And it doesn’t seem to matter if the geometry is a mesh or poly. Any help would be most appreciated. Thanks for reading.
global testRay = intersectRay $sphere01 (ray [x, y, z] $sphere01.pos)
—————————————————————— ^is this the direction vector?
a proper direction vector for the ray from x,y,z the target’s pivot is (targetPosition – SourcePosition)
Test it with two cylinders in different positions… say [20,0,0] and [-20,0,0]
now evaluate on the left cylinder $Cylinder01.dir = $Cylinder02.pos
after that evaluate $Cylinder01.dir = $Cylinder02.pos = $Cylinder01.pos – now you’ve got the proper direction where Cylinder01’s Z axis is pointing directly to the Cylinder02’s pivot.
Thank you! You solved part of the problem with the “targetPosition – SourcePosition” bit. I definitely had some logic missing there. However, after I incorporated this into my code, it still wasn’t working. Then I had a major duh! moment.
[x,y,z] isn’t my particle position. A few lines down…
pCont.particlePosition = getBounds[1] + [x, y, z] * VOXSPACING
DUH! The position is relative to the cage bounds, and the voxel spacing.
if you check a position of particle against a closed geometry it’s enough to cast only one ray in any direction. if the ray intersects first with a backward face the particle is inside. (use intersectRay of RayMeshGridIntersect interface with double-sided option ON)
Thanks denisT. I’m checking out RayMeshGridIntersect right now. It sounds like this will run faster.
honestly, I was very tired last night and didn’t really read your whole code…but said what I noticed. I’m doing something with rays myself and it wasn’t too long ago that I learned how to properly direct the ray.
Oh don’t worry. You helped me plenty. The missing code for particle position is on me. I wasn’t paying attention to what I was doing.
honestly, I was very tired last night and didn’t really read your whole code…but said what I noticed. I’m doing something with rays myself and it wasn’t too long ago that I learned how to properly direct the ray.
Happy to help! Cheers!