Spawning Multiple Objects?
25 posts • Page 1 of 1
Spawning Multiple Objects?
Hello, I need some help with getting an oncollide "happen" ten times when it hits something. (when it hits another object ten circles should be spawned). I've tried pos:=[e.pos, e.pos, e.pos, e.pos, e.pos,.....] but that didn't work If You got any ideas please tell me
make sure to check out my work.
-
Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
Re: Spawning Multiple Objects?
Rideg wrote:Hello, I need some help with getting an oncollide "happen" ten times when it hits something. (when it hits another object ten circles should be spawned). I've tried pos:=[e.pos, e.pos, e.pos, e.pos, e.pos,.....] but that didn't work If You got any ideas please tell me
A code like this should do it:
- Code: Select all
(e)=>{ ! Scene.my.Running ? { Scene.my.Running = true; for(10, (x)=>{e.this.onCollide(e)}); Scene.my.Running = false } : {Scene.my.ExValue = Scene.my.ExValue + 1} }
Declare Scene.my.Running as false to use this (this particular function also requires you to declare Scene.my.ExValue as anything (numerical)).
(e)=>{ ! Scene.my.Running ? { Scene.my.Running = true; for(<REPEATS>, (x)=>{e.this.onCollide(e)}); Scene.my.Running = false } : {<ACTION>} }
Replace <REPEATS> with a number, under ~45 or so (you can stack for functions one after the other but toying with this code can easily crash Algodoo by infinite recursion if you aren't careful), and replace <ACTION> with the action you want to repeat 10 times, this action can include (e) values as it sends the ClassObject into the next onCollide . this particular method makes for quick, easy reading, this particular method could be replaced with something less "dangerous" like just copying and pasting the code over and over one after the other .
This method works by getting the opposite of Scene.my.Running (no particular reason, probably easier to just edit the end brackets than the starting ones or something ) and then sends it into the if_then_else infixed operation, if it's true (opposite), it sets Scene.my.Running to true, so, if the script is run while this script is running, it will not enter this loop and begin infinite recursion, it is then told to send the class object e (from the original call) into e.this.onCollide <REPEATS> many times, it then sets Scene.my.Running back to false, allowing the repeats to begin again if the object is hit again, my example script just adds 1 to Scene.my.ExValue 10 times .
Good Luck, if you have any troubles I should be able to help .
--
Grady
- gradyfitz
- Posts: 174
- Joined: Tue Sep 01, 2009 8:33 am
- Location: Victoria, Australia
Re: Spawning Multiple Objects?
wow nice explanation really! but is there a way doing this without using any variables??
make sure to check out my work.
-
Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
Re: Spawning Multiple Objects?
Easily.
Put that into the onCollide but fill out the "circle stuff here" with your actual parameters you used.
- Code: Select all
for(10, (x)=>{Scene.addCircle({circle stuff here})})
Put that into the onCollide but fill out the "circle stuff here" with your actual parameters you used.
-
standardtoaster - Posts: 606
- Joined: Mon Aug 31, 2009 7:57 pm
Re: Spawning Multiple Objects?
i'm typing (10, (e)=>{scene.addCircle({Circle Stuff})}) but it doesn't seem to work
make sure to check out my work.
-
Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
Re: Spawning Multiple Objects?
Can you please post what you currently have?
-
standardtoaster - Posts: 606
- Joined: Mon Aug 31, 2009 7:57 pm
Re: Spawning Multiple Objects?
- Code: Select all
(10, (e)=>{
scene.addCircle({ radius := 0.2; color := [0.3, 0.3, 0.3, 1.0]; density := 120; vel := [0, 20]; pos := e.pos }) })
make sure to check out my work.
-
Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
Re: Spawning Multiple Objects?
You forgot to put for in front of the first parenthesis.
Fixed it.
- Code: Select all
for(10, (e)=>{ scene.addCircle({ radius := 0.2; color := [0.3, 0.3, 0.3, 1.0]; density := 120; vel := [0, 20]; pos := e.pos }) })
Fixed it.
-
standardtoaster - Posts: 606
- Joined: Mon Aug 31, 2009 7:57 pm
Re: Spawning Multiple Objects?
wtf? it created 10 at the ,middle of the scene before the sim even began to run..?!
make sure to check out my work.
-
Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
Re: Spawning Multiple Objects?
plus the script messe up and tells stuff about the circle
make sure to check out my work.
-
Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
Re: Spawning Multiple Objects?
Could you please explain further. I can't quite understand what you mean.
-
standardtoaster - Posts: 606
- Joined: Mon Aug 31, 2009 7:57 pm
Re: Spawning Multiple Objects?
well i typed in "for" as you said then when I pressed enter, 10 Circles was spawned when the scene was paused and at the position of [0,0] and when I checked the script it had info about the spawned circle, Major bug or am I doing something wrong??
make sure to check out my work.
-
Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
Re: Spawning Multiple Objects?
Rideg wrote:well i typed in "for" as you said then when I pressed enter, 10 Circles was spawned when the scene was paused and at the position of [0,0] and when I checked the script it had info about the spawned circle, Major bug or am I doing something wrong??
- Code: Select all
for(10, (e)=>{ scene.addCircle({ radius := 0.2; color := [0.3, 0.3, 0.3, 1.0]; density := 120; vel := [0, 20]; pos := e.pos }) })
Is a risky affair because you are sending "e" into the for structure, thus, scopes are confused, a similar effect can be seen if you put color = [1,1,1,1] into the console, and try to load a scene, though this isn't the same sort of problem, it has the same cause, "e" is sent into onCollide (this is changable, but all your "e".pos scripts would have to be changed to whatever you changed to as well), the solution is to use a different thing other than e for the for structure
z for example.
- Code: Select all
for(10, (z)=>{ scene.addCircle({ radius := 0.2; color := [0.3, 0.3, 0.3, 1.0]; density := 120; vel := [0, 20]; pos := e.pos }) })
You could use any name you wanted for it as it isn't used in the calculations at all. just best not to use "e" as this may or may not override the onCollide scope (it sometimes works and sometimes doesn't). It is difficult for the Thyme parser to know that you want the "e" sent into onCollide not the "e" sent into the for structure.
I imagine choosing "e" as the argument placeholder was because of so many scripts not mattering if you used "e" or another letter, but using for structures do require you to use a different letter than e for reasons of scope.
Hopefully this helped.
- gradyfitz
- Posts: 174
- Joined: Tue Sep 01, 2009 8:33 am
- Location: Victoria, Australia
Re: Spawning Multiple Objects?
I just tested it in Algodoo and Phun. They both worked perfectly. I don't see what you're getting.
-
standardtoaster - Posts: 606
- Joined: Mon Aug 31, 2009 7:57 pm
Re: Spawning Multiple Objects?
When looking at the code it looks fine but after reading it's not working for you I wanted to be sure if it's not only working for standardtoaster. And yeah, I tested it and it works just as expected:
10 grey circles get spawned at the collision event with a velocity of 20m/s upwards.
10 grey circles get spawned at the collision event with a velocity of 20m/s upwards.
-
KarateBrot - Posts: 825
- Joined: Mon Aug 31, 2009 7:32 pm
- Location: Germany
Re: Spawning Multiple Objects?
Are you using the exact code that is posted above? Did you click on select all, press control-c or command-c, go back to phun/algodoo, click in the onCollide text box and put the insertion point between the curly brackets, and then paste?
-
standardtoaster - Posts: 606
- Joined: Mon Aug 31, 2009 7:57 pm
Re: Spawning Multiple Objects?
Very odd. Have you tried making a new scene with this script? Have you deleted your config.cfg yet?
-
standardtoaster - Posts: 606
- Joined: Mon Aug 31, 2009 7:57 pm
Re: Spawning Multiple Objects?
oh it works thank you! Is there a way getting an objects pos with something like "e.geom.pos"?
make sure to check out my work.
-
Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
Re: Spawning Multiple Objects?
Not really. You could attach a beacon to the center of the object to track its position.
-
standardtoaster - Posts: 606
- Joined: Mon Aug 31, 2009 7:57 pm
Re: Spawning Multiple Objects?
okey is it possible to make number 2, 6, 5, 9 hetero collide of the ten spawned circles???
make sure to check out my work.
-
Rideg - Posts: 948
- Joined: Tue Dec 15, 2009 5:17 pm
- Location: Östersund, Sweden
Re: Spawning Multiple Objects?
- Code: Select all
for(10, (z)=>{ z == 1 ? { scene.addCircle({ heteroCollide := true; radius := 0.2; color := [0.3, 0.3, 0.3, 1.0]; density := 120; vel := [0, 20]; pos := App.mousePos }) } : { z == 4 ? { scene.addCircle({ heteroCollide := true; radius := 0.2; color := [0.3, 0.3, 0.3, 1.0]; density := 120; vel := [0, 20]; pos := App.mousePos }) } : { z == 5 ? { scene.addCircle({ heteroCollide := true; radius := 0.2; color := [0.3, 0.3, 0.3, 1.0]; density := 120; vel := [0, 20]; pos := App.mousePos }) } : { z == 8 ? { scene.addCircle({ heteroCollide := true; radius := 0.2; color := [0.3, 0.3, 0.3, 1.0]; density := 120; vel := [0, 20]; pos := App.mousePos }) } : { scene.addCircle({ radius := 0.2; color := [0.3, 0.3, 0.3, 1.0]; density := 120; vel := [0, 20]; pos := App.mousePos }) } } } } })
Hopefully this is that you want.
-
standardtoaster - Posts: 606
- Joined: Mon Aug 31, 2009 7:57 pm
25 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 15 guests