martes, 1 de octubre de 2013

On collide y scripts interesantes

What is OnCollide ScriptingEdit

To create many advanced scenes many users of Algodoo/Phun use OnCollide scripting this basically carries out the script you write during a collision with the object that has that scripting. For instance, if a square has the following oncollide script: "(e)=>{density = 10}" that object will have the density of 10 after it collides with something. On the surface this doesnt look particularly useful however it has the ability to change many more variables than just density it can even change variables however i will not be covering variables in this guide. in this guide i will be giving you a basic understanding of oncollide scripting and maybe a few advanced bits.

First Things First Setting up Algodoo/Phun for oncollide Edit

i suggest if you have only just got on Phun/Algodoo then you read this however if you have had this program for a while you can probably skip to the next stage.
Many of you are probably wondering how to get the more advanced features of algodoo/phun. Well i will now tell you:
  1. Open Algodoo/Phun (Sorta Obvious one that)
  2. Go on any scene
  3. Click on Options in the top Left corner by default
  4. Then Interface
  5. You will then see as one of the first options: "Level"
  6. Set this to "Advanced Mode"
  7. Your Done! Now go and start scripting ;)
Now that you have your workspace set up it is time to begin working with it.

Changing Simple Properties With OnCollideEdit

Lets start off small. to start with try the script from earlier. to do this open a new scene and draw a rectangle with the rectange tool. now right click on the rectangle and you will see a drop down menu. somewhere on this menu you will find another drop down menu called: "Script Menu...". Click this. this will bring up a more detailed view of the properties of the polygon. these properties will all be numeric values (Before you say True/False is not a numeric value. it is its known as a bollean value but this is irelevant) and can be changed from this menu. Now, On this menu you will see a property called: "onCollide" and a box next to it with "(e)=>{" this is the begining for all oncollide scripts. Now in this box enter the oncollide script from earlier: "(e)=>{density = 10} this will set density of this object to 10 but only when it collides with something like the floor. to make sure the density does change we will first open another window. right click the object again and now click on the tab: "Information..." this will bring up an information window with most of the properties of the object in real-time including during simulations. this is useful for seeing if your script works. now play the simulation and you will see when the rectangle hits the plane the mass of the object will change as you have successfully made your first onCollide Script!

This can be used to change any property of the object by filling in these fields "(e)=>{<Property> = <Value>}
this type of code has a few names but most just call this an assignment statement; a statement in a piece of code that is giving a variable (In this case that variable is density) a numeric value

Changing Multiple properties at OnceEdit

For Those of you who have used programming languages before will be very familliar with this part. When changeing one property and then you want to change another property at the same time all you do is tell Phun/Algodoo that you are going to be changing the value of multiple properties through the use of a semicolon ";". this tells the computer that you have finished one of your assignment statement and are now going to enter another. For Example, "(e)=>{density = 10; friction = 10} this code will change two variables at once. you should know that the computer reads it from the top to the bottom for more advanced pieces of codes.

Changing the Properties of Other ObjectsEdit

Defining the properties of an object that gets hit is often unsuited to the job at hand. if only it was possible to change the properties of the object that hits the onCollide object. Well, it is! First off you should know that density = 10 can also be written as e.this.density = 10 as this is the more accurate name for this property but if you wanted to change the denstiy of an object that gets hit by this object you can use this piece of code: "(e)=>{e.other.density = 10} this will change the density of the object that gets hit by this object to 10. a good use of this could be a hammer in a gun as you could set it to change the density of the bullet it strikes to a higher one so it carries more energy (Believe me it does). this also applies to all the other variables like e.other.restitution = 100 with change the bounciness of the bullet to a much higher one causing much more knockback power. This is extremely useful.

Clever Pieces of Script you may BorrowEdit

Water Spawner (Spawns water on collision, WARNING this WILL crash your computer when the object collides with water, so turn colission with water off) (e)=>{Scene.addWater({vecs = [e.pos, e.pos, e.pos, e.pos, e.pos, e.pos, e.pos, e.pos, e.pos, e.pos, e.pos, e.pos, e.pos, e.pos, e.pos, e.pos, e.pos, e.pos, e.pos, e.pos]})}
Circle Spawner (e)=>{scene.addCircle({ radius := 0.1; density := 2.0; restitution := 0.5; friction := 0.5; pos := e.pos }) };
Box Spawner (e)=>{ Scene.addBox({size := [0.83575642, 0.27858579]; density := 2.00; pos := e.pos; restitution := 0.5; friction = 0.5; angle := 0}) }
Self Collision (e)=>{heteroCollide = false;}
Collide water (e)=>{collideWater = true }
Shape color([R,G,B,A] 0-255) (e)=>{color = [1.0, 0.0, 0.0, 1.0]}
Text Color ([R,G,B,A] 0-255) (e)=>{textColor = [1.0, 0.0, 0.0, 1.0]}
Text Size (e)=>{textScale = 3;}
Density (e)=>{density = 10.02;}
Repel (e)=>{attraction = - 20;}
Attract (e)=>{attraction = 50;}
Killer (e)=>{killer = true }
"Anti-Gravity" (e)=>{Sim.gravityAngleOffset = 3.1415927;}
Pause Sim (e)=>{sim.running = false;}
Change Zoom (e)=>{ = 17.700001}
change Camra Pan (e)=>{ = [ - 446, 14.6];}
Set destroy key (With spawned objects) buttonDestroy := "e"
Set collision (With spawned objects) collideSet := 0;
Add Text (With spawned objects) text := "Hello World!"

No hay comentarios:

Publicar un comentario