How can I calculate relative distance in a script?

Post your tutorials here.

How can I calculate relative distance in a script?

Postby windtalkers » Mon May 09, 2011 4:34 pm

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.
windtalkers
 
Posts: 13
Joined: Thu May 05, 2011 7:03 am

Re: How can I calculate relative distance in a script?

Postby bozbez » Mon May 09, 2011 6:02 pm

You know the positions of both objects?
Image

Go here. Now. That's Now. Not in 5 minutes. Now.
User avatar
bozbez
 
Posts: 149
Joined: Tue Apr 12, 2011 7:01 pm

Re: How can I calculate relative distance in a script?

Postby TC42 » Mon May 09, 2011 9:34 pm

Use the distance forumula once you know the points!
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
Image
Spambot wrote:Nowadays, memes usually are studied less in the field of biology, but more with the arena of psychology along with sociology. But, the major area of a meme, getting embraced and imitated all the time is what’s at any core connected with any meme.
User avatar
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?

Postby windtalkers » Tue May 10, 2011 5:34 am

:thumbup: @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. :crazy:
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. :thumbup:
windtalkers
 
Posts: 13
Joined: Thu May 05, 2011 7:03 am

Re: How can I calculate relative distance in a script?

Postby Sparky » Tue May 10, 2011 12:10 pm

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
:arrow:
What do you get when you cross a clown with a fire hydrant?

scene.my.sparkyrulz=true
sandwitch cannon
User avatar
Sparky
 
Posts: 20
Joined: Mon Mar 21, 2011 12:08 pm
Location: The univerese

Re: How can I calculate relative distance in a script?

Postby Sparky » Tue May 10, 2011 12:12 pm

oh i think your first problem might be imposable to solve,but i'll keep looking....
What do you get when you cross a clown with a fire hydrant?

scene.my.sparkyrulz=true
sandwitch cannon
User avatar
Sparky
 
Posts: 20
Joined: Mon Mar 21, 2011 12:08 pm
Location: The univerese

Re: How can I calculate relative distance in a script?

Postby windtalkers » Tue May 10, 2011 1:47 pm

Sparky wrote:scene.addpen({size:=0.5; pos:=e.pos}) that should work.
:arrow:

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?

Postby bozbez » Tue May 10, 2011 7:49 pm

This is going to be a long post.
Anyway, lets start with the problem of multiple ball spawning.

:arrow: 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).

:arrow: Step 2: Put two lasers with the same collision layer as the box in the box. Make both their maxrays = 1.

:arrow: Step 3: Make one of the lasers have the activation key you want.

:arrow: Step 4: In the laser with the activation key's onLaserHit, paste
Code: Select all
(e)=>{scene.my.nexttick = sim.tick + 1}


:arrow: 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}
}


:arrow: 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)

:arrow: Step 7: Mount the box with lasers on where you want it to be mounted.

:arrow: 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.

:arrow: Step 9: Test if it works! (Each time you activate the laser, no more than one circle should be spawned.)

:arrow: 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!
Image

Go here. Now. That's Now. Not in 5 minutes. Now.
User avatar
bozbez
 
Posts: 149
Joined: Tue Apr 12, 2011 7:01 pm

Re: How can I calculate relative distance in a script?

Postby windtalkers » Wed May 11, 2011 3:13 am

bozbez wrote: Hope that explains things!

@bozbez: You are a great tutor! :thumbup: Thank you very much! :lol: yay!
windtalkers
 
Posts: 13
Joined: Thu May 05, 2011 7:03 am

Re: How can I calculate relative distance in a script?

Postby Rideg » Sun May 22, 2011 10:49 pm

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. :angel:
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: How can I calculate relative distance in a script?

Postby windtalkers » Mon May 23, 2011 4:18 am

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. :angel:


Thank you very much! :thumbup:
windtalkers
 
Posts: 13
Joined: Thu May 05, 2011 7:03 am


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests