Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

reflect vector on surface - like a lazer!

reflect vector on surface - like a lazer!

CoreyMurray
Enthusiast Enthusiast
1,885 Views
20 Replies
Message 1 of 21

reflect vector on surface - like a lazer!

CoreyMurray
Enthusiast
Enthusiast

hello,

I'm just getting started with MAXscript.

I want to reflect a line about a surface normal to create a new line to simulate light bouncing. The line is defined by the location of a point helper named lineOrigin01. The end of the line is at the center of an editable mesh object named reflectSurface01. I want to create a new line that is 100m long at the reflected direction.

 

Below is the best I could come up with, with chatGPT helping (or not helping but helping me learn). Get the objects, get the normal vector, reflect function, draw the line. If anyone has ANY tips or ideas they would be gladly received!

 

-- Define the start point of the line as the location of sunORIGIN object
startPoint = sunORIGIN.pos

 

-- Define the surface object to reflect off of as reflectSURFACE
surfaceObj = $reflectSURFACE

 

-- Get the center point of the surface object
surfaceCenter = (surfaceObj.max + surfaceObj.min) * 0.5

 

-- Get the normal vector of the surface object
surfaceNormal = normalize((surfaceObj.normal * surfaceObj.transform) as vector3)

 

-- Calculate the end point of the line using the reflect() function
endPoint = reflect(startPoint - surfaceCenter, surfaceNormal) + surfaceCenter

 

-- Create the line object with a length of 100 units
lineObj = createLine start:endPoint end:(endPoint + surfaceNormal * 100.0)

0 Likes
Accepted solutions (3)
1,886 Views
20 Replies
Replies (20)
Message 2 of 21

Serejah
Advocate
Advocate

@CoreyMurray  написал (-а):

hello,

I'm just getting started with MAXscript.

...

Below is the best I could come up with, with chatGPT helping (or not helping but helping me learn). Get the objects, get the normal vector, reflect function, draw the line. If anyone has ANY tips or ideas they would be gladly received!


It is actually preventing you from learning anything since it doesn't even know the correct maxscript syntax. Reading maxscript reference is a way better method to learn scripting or if you prefer video there's a great maxscript 101 series on vimeo.

0 Likes
Message 3 of 21

CoreyMurray
Enthusiast
Enthusiast

It is far from perfect, and can mix up syntax yes.

 

But for simple syntax and for explaining things I find it quite useful - in addition to reference and tutorials.

 

But thanks for you opinion there, i guess.

Did you have any suggestions on the 'reflect vector' topic and how i might achieve this?

0 Likes
Message 4 of 21

Serejah
Advocate
Advocate

@CoreyMurray  написал (-а):

It is far from perfect, and can mix up syntax yes.

...

Did you have any suggestions on the 'reflect vector' topic and how i might achieve this?


I prefer the old school way and simply google anything I don't know or forgot

https://www.youtube.com/watch?v=7yws8RRDYRA

0 Likes
Message 5 of 21

istan
Advisor
Advisor
0 Likes
Message 6 of 21

leeminardi
Mentor
Mentor

In the attached file I used the equation @Serejah referenced for defining the reflected vector.  I used it to locate a point 10 units from the surface. A line with a Spline IK Control modifier is use to show the reflected "ray". Note, the variable surface is the transform of reflectSurface.

 

leeminardi_0-1678911817423.png

 

lee.minardi
0 Likes
Message 7 of 21

CoreyMurray
Enthusiast
Enthusiast
Hi leeminardi
Thank you very much for your response and the scene file. It never occurred to me to use a control modifier, but seems like a great idea. I will take a look at the scene and educate myself on controllers.
Thanks again for your help
Corey
0 Likes
Message 8 of 21

CoreyMurray
Enthusiast
Enthusiast

Hi again leeminardi

 

That looks like a really tidy solution! When i rotate the surface, the ray reflects based on the normal of the surface, and when I move the 'sunOrigin' helper it also controls the incident angle - so cool!

i guess the last thing i want to do is link the 'sunOrigin' helper to the position of an actual sun in a daylight system, so the the ray origin is always the same as the sun. I tried linking 'sunOrigin' to the daylight system and sun, but it loses connection/link to the reflected ray.

CoreyMurray_0-1678918430558.png


Could I swap out the light ray source to be the sun light object instead of 'sunOrigin'?

Also, how do you bring up the 'script controller' window you have in your screenshot?

 

Sorry if i am missing something, still trying to wrap my head around this.

 

Many thanks really appreciate your help on this.

 

Corey

0 Likes
Message 9 of 21

leeminardi
Mentor
Mentor

Could I swap out the light ray source to be the sun light object instead of 'sunOrigin'?

 

Are you asking about getting the location of the sun for a Sun Position object? If so it may require a calculation using the transform of the Sun Position Compass along with the distance to the sun, the azimuth and the altitude.  I think this should be straight forward.  Typically when you link a point to another object you need to multiply the point's local location times the transform of the parent (the sun).  But, the transform of a Sun Positioner is the location of the its compass not the sun so that's why I think you need to use other parameters to determine the sun location.

 

Also, how do you bring up the 'script controller' window you have in your screenshot?

 

Select Point001, then go to the Motion tab and double-click Position:Position Script under Transform:

 

If you are new to scripts for controllers and using vector and matrix operations you might find a couple of my youtube tutorials of help.  For example: Rigging a Hexapod.

lee.minardi
0 Likes
Message 10 of 21

leeminardi
Mentor
Mentor

It took a little hunting but I found out how to find the location of the sun (not just the compass rose).  

sun.manual_sun_position 

Is the position of the sun relative to the Sun Positioner.  Multiply this times the transform of the Sun Positioner transform to get the world coordinates.

The following will determine the reflected ray

leeminardi_0-1678990677994.png

I think v should equal a vector from the compass rose to the sun and not from the surface. For a sun system all light rays should be considered parallel on earth and not from a point light source close to earth so V should = normalize(sunpos - compassT.pos) and NOT normalize(sunpos - surface.pos).

 

You can manually move the sun  or move/rotate  the surface in the attached file and the "reflected ray" should adjust accordingly.

 

 

lee.minardi
0 Likes
Message 11 of 21

CoreyMurray
Enthusiast
Enthusiast

Hi again Lee, 

Firstly thanks for your help with this!
I typically work with VRay and use the Daylight system (systems panel>daylight), but think it is a similar set up - directional light, controller, compass helper. I am trying to simulate where sunlight would (in theory) be reflected from a solar farm. . At the moment(using the file you so kindly supplied), I can manually move/snap the sunOrigin to the helper to the directional light (sun) - this is already really useful. I guess I thought we could bypass the helper object and reference the directional light object directly, it would always be correct. Failing that I figured a basic position constraint or link from the sunOrigin helper to the dir light would be possible, but when i tried this, it seemed to mess with the surface and reflect line relationship.
I need to get more familiar with controllers and constraints, so will definitely check your tutorials.
Thanks again for your help

Corey

0 Likes
Message 12 of 21

CoreyMurray
Enthusiast
Enthusiast

Hi again Lee, 

Firstly thanks for your help with this!
I typically work with VRay and use the Daylight system (systems panel>daylight), but think it is a similar set up - directional light, controller, compass helper. I am trying to simulate where sunlight would (in theory) be reflected from a solar farm. . At the moment(using the file you so kindly supplied), I can manually move/snap the sunOrigin to the helper to the directional light (sun) - this is already really useful. I guess I thought we could bypass the helper object and reference the directional light object directly, it would always be correct. Failing that I figured a basic position constraint or link from the sunOrigin helper to the dir light would be possible, but when i tried this, it seemed to mess with the surface and reflect line relationship.
I need to get more familiar with controllers and constraints, so will definitely check your tutorials.
Thanks again for your help,

Corey

0 Likes
Message 13 of 21

leeminardi
Mentor
Mentor

Corey,

 

I forgot to attach the file in my last post.

 

Open the file and select SunPositioner001 and then go to the MOdify panel and select the Sun sub-object.  You can move the sun and the "ray" will reflect accordingly.

 

Select Point001 and then double click Position Script in the Motion Panel

 

Note that there are two lines " v = ..._ The one that is commented out with -- sets the angle of sunlight to the vector from the surface to the sun's position.  The one that is active sets the angle of the sunlight vector from the compass rose to the sun's position.  As I mentioned earlier, I think this is the vector you should use as it defines the direction of sunlight on earth and not from a sun that's close to earth!

 

sunpos = sun.manual_sun_position * compassT
v = normalize(sunpos - compassT.pos)
-- v = normalize(sunpos - surface.pos)
n = surface.row3
r = normalize(2 * (dot v n) *  n - v)
pt = surface.pos + 10 * r

 

Let me know if you have any questions.

 

 

 

lee.minardi
0 Likes
Message 14 of 21

CoreyMurray
Enthusiast
Enthusiast
Accepted solution

Hi Lee

Thanks for the file. OK, that makes sense re using the compass rose instead of the surface - that is also great because I can move the surface around the scene and the angle will still be correct. sun.manual_sun_position certainly is hidden away in there! I was intending to use the 'Date, Time & Location' feature of SunPositioner, but when I switch that in the file it seems to break the controller - but works again if I switch back. I couldnt see anything obvious in the reference for SunPositioner (like sun.dtl_sun_position?!) but I guess when you enable DT&L, sun.manual_sun_position is no longer valid.

I always use the Daylight System (easy to switch to VRay Sun), which I think is similar:
https://help.autodesk.com/view/MAXDEV/2022/ENU/?guid=GUID-9EEFF581-95CC-4D3C-887F-A7E1B2752C4F

it has a compass, and maybe you can access the light using <Sunlight_Driven_Controller>.manual_position ?

 

I will have a go and see if i can swap out the SunPositioner object for a Daylight system. In the attached file, i made the surface only rotate on Y and added a Daylight system

Cheers

 

Corey

0 Likes
Message 15 of 21

leeminardi
Mentor
Mentor

You can use the following to get the sun direction vector  "v"  (the minus sign changes the sense to pointing towards the sun).

TheSun = $Daylight001
v = -TheSun.transform.row3
format "\nSunVector = %" v

lee.minardi
0 Likes
Message 16 of 21

leeminardi
Mentor
Mentor
Accepted solution

You can use the following to get the sun vector "v" ( the minus sign change the sense to pointing towards the sun).

TheSun = $Daylight001
v = -TheSun.transform.row3
format "\nSunVector = %" v

  With this approach you can set the date and time and the vector will update accordingly. 

lee.minardi
0 Likes
Message 17 of 21

CoreyMurray
Enthusiast
Enthusiast

Hi - I added that additional code and got it working, i had to remove the minus sign in line two to get it to reflect rather then continue through the surface. One issue is that it doesn't update when changing the time/date until you hit 'evaluate'.

Before seeing your reply i tried to change the values to use Daylight001 with the previous approach:
compassT - use Daylight System object compass (as previous with SunPositioner)
sun - assigned this to use Daylight001 manual position track.

This seems to work, and updates when i change time/day.

 

CoreyMurray_0-1679521695692.png

CoreyMurray_1-1679521997703.png

 

Your solution seems to utilize the properties of Daylight001 more efficiently.

 

Cheers

 

Corey  

 

0 Likes
Message 18 of 21

leeminardi
Mentor
Mentor

@CoreyMurray it's not clear to me if you have a satisfactory solution.  Does the system update when you change the date/time?   If not, please attach your latest version of the Max file that includes the script for point001.

lee.minardi
0 Likes
Message 19 of 21

CoreyMurray
Enthusiast
Enthusiast

Hi

File attached. This is the previous file you posted, I made the changes to use the Daylight System:

TheSun = $Daylight001
v = -TheSun.transform.row3
format "\nSunVector = %" v

Is there a way to make the reflected line update when the sun changes automatically, without having to hit re-evaluate? Apologies if I have missed something, I'm totally out of my depth here haha.
Thanks ☺

0 Likes
Message 20 of 21

leeminardi
Mentor
Mentor
Accepted solution

Just Assign Node of Daylight001 to TheSun so that changes to Daylight001 will cause the script to be executed.

leeminardi_0-1679579798317.png

 

lee.minardi
0 Likes