Help with script?
3 posts • Page 1 of 1
Help with script?
Hello, can someone tell me why this script ain't working. It is supossed to spawn a box that is pointed towards the activated laser. Instead it spawnes with that angle - or + 90 degrees. Here's the code:
- Code: Select all
(e)=>{
Scene.addBox({
color := [0.42, 0.42, 0.42, 1.0];
onCollide := (e)=>{e.this.collideSet = 32};
heteroCollide := true;
friction := 1.0;
drawBorder := true;
airFrictionMult := 0.0;
density := 10.0;
collideSet := 64;
zDepth := +inf;
pos := e.pos;
angle := math.atan(e.normal(1) / (e.normal(0) + 1)) * 2;
size := [0.085, 0.6]
})
}
-

Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
Re: Help with script?
Rideg wrote:Hello, can someone tell me why this script ain't working. It is supossed to spawn a box that is pointed towards the activated laser. Instead it spawnes with that angle - or + 90 degrees. Here's the code:
- Code: Select all
(e)=>{
Scene.addBox({
color := [0.42, 0.42, 0.42, 1.0];
onCollide := (e)=>{e.this.collideSet = 32};
heteroCollide := true;
friction := 1.0;
drawBorder := true;
airFrictionMult := 0.0;
density := 10.0;
collideSet := 64;
zDepth := +inf;
pos := e.pos;
angle := math.atan(e.normal(1) / (e.normal(0) + 1)) * 2;
size := [0.085, 0.6]
})
}
This is a side effect of the fact that a/b = -a/-b, so when you call the inverse tangent function with the value you have given, it's difficult to differentiate two positive values from two negative values.
There is a thread with a function - atan2, which takes both points (in an array) and returns a value in radians, Some extra math functions.
(atan2 function by standardtoaster)
- Code: Select all
Scene.my.atan2=(P)=>{2 * math.atan(P(1) / (((P(0) ^ 2 + P(1) ^ 2) ^ 0.5) + P(0)))}
The function can simply be used by putting in:
- Code: Select all
Scene.my.atan2(e.normal)
Or if you need to modify the normal:
- Code: Select all
Scene.my.atan2([e.normal(0) + x, e.normal(1) + y])
(where x and y are any numbers, just for demonstration)
To change this to point to the laser, you would probably be better off getting both points and working out the angle between them.
Hopefully this helped
- gradyfitz
- Posts: 174
- Joined: Tue Sep 01, 2009 8:33 am
- Location: Victoria, Australia
Re: Help with script?
-

Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
3 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 2 guests




