How can I calculate relative distance in a script?
11 posts • Page 1 of 1
How can I calculate relative distance in a script?
Hi, I'm new in algodoo, I want to wirte a little script in a laser, which may calculate relative distance between the laser and the hinge, can anyone help me?
Thank you in advance.
Thank you in advance.
- windtalkers
- Posts: 13
- Joined: Thu May 05, 2011 7:03 am
Re: How can I calculate relative distance in a script?
You know the positions of both objects?
-
bozbez - Posts: 149
- Joined: Tue Apr 12, 2011 7:01 pm
Re: How can I calculate relative distance in a script?
Use the distance forumula once you know the points!
D = sqrt((x1 - x2)^2 + (y1 -y2)^2)
In algodoo that would be
D = sqrt((x1 - x2)^2 + (y1 -y2)^2)
In algodoo that would be
- Code: Select all
scene.my.distance = ((scene.my.point1(0) - scene.my.point2(0))^2 + (scene.my.point1(1) - scene.my.point2(1))^2)^0.5
-
TC42 - Posts: 984
- Joined: Sun Jan 09, 2011 1:53 pm
- Location: $ sudo apt get-install sandwich_
Re: How can I calculate relative distance in a script?
@bozbez, Tc42: thx, now I can fix my problem.
Well, by the way, I use a laser to spawn balls, so I make a box first, filling a little script in onHitByLaser:
(e)=>{
scene.addcircle({
pos:=e.pos;
radius:=0.2; vel:= [5,1];
heterocollide:=true
})
}
Now my first problem is: when I use a laser (triggered by one key, for example, Q ) to shine the box, then more than one balls were spawned.
What I really want is: only one ball spawned every time I press Q to shine the box, I don't know how to do it with alogdoo, can you help me?
The second problem: how can I add a tracer on the ball through the script? Please don't tell me I can copy the tracer and paste it in the script, I want to know how to write a script to make the tracer and the ball are spawned and move together, something like the tracer is sticked to the ball.
Any information will be appreciated.
PS: I learned the little script which posted here from Jesse Bax.
Well, by the way, I use a laser to spawn balls, so I make a box first, filling a little script in onHitByLaser:
(e)=>{
scene.addcircle({
pos:=e.pos;
radius:=0.2; vel:= [5,1];
heterocollide:=true
})
}
Now my first problem is: when I use a laser (triggered by one key, for example, Q ) to shine the box, then more than one balls were spawned.
What I really want is: only one ball spawned every time I press Q to shine the box, I don't know how to do it with alogdoo, can you help me?
The second problem: how can I add a tracer on the ball through the script? Please don't tell me I can copy the tracer and paste it in the script, I want to know how to write a script to make the tracer and the ball are spawned and move together, something like the tracer is sticked to the ball.
Any information will be appreciated.
PS: I learned the little script which posted here from Jesse Bax.
- windtalkers
- Posts: 13
- Joined: Thu May 05, 2011 7:03 am
Re: How can I calculate relative distance in a script?
scene.addpen({size:=0.5; pos:=e.pos}) that should work.
and for the oher problem..... search the forums i remmember reading it here somewhere....
hope this helped
and for the oher problem..... search the forums i remmember reading it here somewhere....
hope this helped
-
Sparky - Posts: 20
- Joined: Mon Mar 21, 2011 12:08 pm
- Location: The univerese
Re: How can I calculate relative distance in a script?
oh i think your first problem might be imposable to solve,but i'll keep looking....
-
Sparky - Posts: 20
- Joined: Mon Mar 21, 2011 12:08 pm
- Location: The univerese
Re: How can I calculate relative distance in a script?
Sparky wrote:scene.addpen({size:=0.5; pos:=e.pos}) that should work.
Thank you! But it doesn't work, no tracer attaches the ball.
- windtalkers
- Posts: 13
- Joined: Thu May 05, 2011 7:03 am
Re: How can I calculate relative distance in a script?
This is going to be a long post.
Anyway, lets start with the problem of multiple ball spawning.
Step 1: Create a box, make it semi-transparent and put it on collision layer g (it can be on any other collision layer, but i keep things like this on g).
Step 2: Put two lasers with the same collision layer as the box in the box. Make both their maxrays = 1.
Step 3: Make one of the lasers have the activation key you want.
Step 4: In the laser with the activation key's onLaserHit, paste
Step 5: In the onLaserHit of the other laser, paste
Step 6: Put in your code where it is pretty obvious to put your code (Remove the brackets around the CODE GOES HERE). (I'll address the tracer problem later)
Step 7: Mount the box with lasers on where you want it to be mounted.
Step 8: Because we haven't declared the variables in the console, the first time you activate the laser, nothing will happen, but it should work the second time.
Step 9: Test if it works! (Each time you activate the laser, no more than one circle should be spawned.)
Step 10: If it doesn't work, post a reply.
Now lets address the tracer problem. If you want a tracer to be glued onto an object you spawn, you have to give the object an geomID. So a simple example would be:
Now, in the tracer, you need the geom and relpoint parameters. So:
Notice the geom parameter is the geomID. The relPoint is the position of the tracer relative to the center of the object.
Hope that explains things!
Anyway, lets start with the problem of multiple ball spawning.
Step 1: Create a box, make it semi-transparent and put it on collision layer g (it can be on any other collision layer, but i keep things like this on g).
Step 2: Put two lasers with the same collision layer as the box in the box. Make both their maxrays = 1.
Step 3: Make one of the lasers have the activation key you want.
Step 4: In the laser with the activation key's onLaserHit, paste
- Code: Select all
(e)=>{scene.my.nexttick = sim.tick + 1}
Step 5: In the onLaserHit of the other laser, paste
- Code: Select all
(e)=>{
scene.my.nexttick > sim.tick ? {
scene.my.alreadyspwned < 1 ? {
{CODE GOES HERE};
scene.my.alreadyspwned = 1
} : {}
} : {scene.my.alreadyspwned = 0}
}
Step 6: Put in your code where it is pretty obvious to put your code (Remove the brackets around the CODE GOES HERE). (I'll address the tracer problem later)
Step 7: Mount the box with lasers on where you want it to be mounted.
Step 8: Because we haven't declared the variables in the console, the first time you activate the laser, nothing will happen, but it should work the second time.
Step 9: Test if it works! (Each time you activate the laser, no more than one circle should be spawned.)
Step 10: If it doesn't work, post a reply.
Now lets address the tracer problem. If you want a tracer to be glued onto an object you spawn, you have to give the object an geomID. So a simple example would be:
- Code: Select all
Scene.addCircle {
color := [0,1,0,0];
collideSet := 1;
geomID := 222545;
pos := e.pos;
radius := 0.25
};
Now, in the tracer, you need the geom and relpoint parameters. So:
- Code: Select all
Scene.addPen {
geom := 222545;
relPoint := [0.0, 0.0];
color := [0, 1, 0, 1];
fadeTime := 1.5;
size := 0.2;
}
Notice the geom parameter is the geomID. The relPoint is the position of the tracer relative to the center of the object.
Hope that explains things!
-
bozbez - Posts: 149
- Joined: Tue Apr 12, 2011 7:01 pm
Re: How can I calculate relative distance in a script?
bozbez wrote: Hope that explains things!
@bozbez: You are a great tutor! Thank you very much! yay!
- windtalkers
- Posts: 13
- Joined: Thu May 05, 2011 7:03 am
Re: How can I calculate relative distance in a script?
- Code: Select all
Scene.addCircle({Properties_Goes_in_Here; geomID := 123}); Scene.addPen({geom := 123; relPoint := [0, 0]})
Useful for zDepths advanced codes and much more reliable than pos := e.pos when it comes to tracer spawning. Hope you find this of any use.
make sure to check out my work.
-
Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
Re: How can I calculate relative distance in a script?
Rideg wrote:
- Code: Select all
Scene.addCircle({Properties_Goes_in_Here; geomID := 123}); Scene.addPen({geom := 123; relPoint := [0, 0]})
Useful for zDepths advanced codes and much more reliable than pos := e.pos when it comes to tracer spawning. Hope you find this of any use.
Thank you very much!
- windtalkers
- Posts: 13
- Joined: Thu May 05, 2011 7:03 am
11 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 4 guests