Changing laser direction in onHitByLaser

About advanced scenes, and the Thyme scripting language used in Algodoo.

Changing laser direction in onHitByLaser

Postby NeatNit » Thu Jan 11, 2018 12:58 am

I'm new to Thyme and Algodoo, and finding it pretty hard to get around without proper documentation or even a console / error messages. (unless I'm missing something obvious?)

Nevertheless, my goal is to create a perfect thin lens. I figured I could use the onHitByLaser event and redirect the laser in the direction I want it to go. So I figured out I can get the hit position in e.pos and the normal of the surface the laser hit in e.normal. But how do I get the hit angle? And how do I tell the engine which direction I want it to go?

Assuming the angle issue has a solution, if there's no way to control refraction I can always just absorb the laser and shoot out a new one in the desired direction. How would I go about doing that? (ideally the new laser "body" should not be visible)
Posts: 2
Joined: Thu Jan 11, 2018 12:44 am

Re: Changing laser direction in onHitByLaser

Postby Milothewinner » Fri Jan 12, 2018 7:26 pm

Posts: 1
Joined: Fri Jan 12, 2018 6:53 pm

Re: Changing laser direction in onHitByLaser

Postby NeatNit » Sat Jan 13, 2018 6:46 pm

Okay, I looked into it a bit more.

Firstly, I found out that the console was bound to F10, not F11 like every resource says. Being able to open the console was an essential first step :P

Now, it turns out that despite my best efforts, this is impossible to do in the general case. This is because the event arguments don't provide a hit angle. We have the normal of the geometry, but not the angle of the laser hitting the geometry.

The laser's original angle can be acquired with e.laser.rotation, but this doesn't account for any reflections or refractions that happened along the way.

If the laser has just a single path, I can keep track of its last hit position and use it to calculate the hit angle:

Code: Select all
onLaserHit = (e)=>{ = e.pos;
    hitvec = e.pos - e.this.lastHitPos; =  - math.atan2(hitvec(0), hitvec(1)); = e;
    e.this.lastHitPos = e.pos

update = (e)=>{
    e.this.lastHitPos = e.this.pos

( is a polygon I made to visualize stuff - in this case I'm visualizing the hit position and laser's angle)

This works, but lasers don't always follow a single path. Some materials both reflect and refract a laser, causing its path to split in two - and there's no way to know which path we're in, or the execution order of the different paths.

Posts: 2
Joined: Thu Jan 11, 2018 12:44 am

Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 1 guest