[Closed] Lightning and random point on surface
I started this project to learn some maxscript, keep in mind this is my first ‘big’ coding project for max so the code aint that clean.
The script:
http://users.tkk.fi/~jylilamm/Lightning.ms
Simply drag&drop it on max, or run it from Maxscript -> Run Script…
I’ve still got few things on my to-do list like support for animated and deformable objects, additional forks hitting target object and strikes dependable on distance between target points.
Now the script only takes source and target objects pivot and the lightning is created between these two points. However, I want to also be able to use vertex as target, which shouldn’t be too hard to code, and objects surface (faces). Here lies the problem, how to get random point on objects surface. It should be calculated in that kind of manner that the point is locked surface so it stays on it even when its animated and deformed.
First I would random 1 face from target object, based on its area, larger faces having bigger chance of getting selected so random points are evenly distributed on surface. The next thing would be using position data of 3 vertex which form the face to get random point on it. How to get this random point is above me. So if anybody knows the best and most efficient way to get it please tell.
Oh and btw, I use quite many globals, storing user given values so they are available to different functions used when creating lightnings, should I perhaps use some other method than globals instead?
Hey MasterBercon, havn’t tested your script yet but here is an example how to get a random point on a face defined by 3 verts:
onelength = vert2pos – vert1pos –vector from the first vert to the second
randomlength1 = onelength * (random 0.0 1.0) –vector scaled by random value
firstpos = vert1pos + randomlength1 –random position between the 2 first verts
secondlength = vert3pos – firstpos –vector from the first random pos to the third vert
randomlength2 = secondlength * (random 0.0 1.0) –vector scaled by random value
randompos = firstpos + randomlength2 –random position on face
If you save both random values in a variable you can use them to calculate the same relative position for every frame.
There is also something called barycentric coordinates wich is a points position relative to face wich could be used for editable meshes. Perhaps look that up in the help.
Let me know if you have any question. Good luck with the script.
CML