## Beginner's Guide to Superior Construction

Post your tutorials here.

### Beginner's Guide to Superior Construction

Here, I'm assuming you just downloaded the program a few days ago, and you've learned how to make some simple stuff like a car or a single-shot gun, but you haven't really built anything impressive. This tutorial will not tell you exactly how to make anything in particular, but instead will teach you some techniques and explain some features that you can use to make stuff more skillfully. I was thinking of calling the tutorial How to Not Suck at Phun, but that might put some people off. It's a long one, so don't hesitate to read in chunks.
Well, first things first...

Advanced Mode rules! Before you bother doing anything, make sure you have the interface set to Advanced Mode in the Options menu. This opens up loads of cool tools and options you can use to build more precisely and make more complicated devices. There are a lot of them, as you will see as soon as you check the box. All of the menus will bulge with new options. A few of the complicated ones will be highlighted here, but don't forget to play around with all of them.

Scale Tool
There is a tool on the toolbar that seems to get noticed less than the others, and that's the Scale tool, nestled between the Rotate tool and the Cut tool. Don't forget about it, and it will be your friend. It will stretch objects from the opposite corner or side, if it's a polygon or a box or a machine with multiple parts. One object with one hinge or fixjoint on it will be scaled from the hinge or fixjoint. You can use that to your advantage by placing a hinge at the point you want to scale from and deleting it afterward. Except for circles. Single circles will always be scaled from the center, hinge or no.
If you scale a box or a circle in a way that makes it no longer a perfect rectangle or circle, it will be transformed into a polygon with the desired shape, such as an oval or a parallelogram. Polygon collisions are not handled as well as box and circle collisions are, so be careful with wheels and really skinny boxes.
To do perfect scaling, two options are available to you. Holding SHIFT while scaling will scale it the exact same proportion vertically and horizontally. This will mean your circles will always stay circles and your boxes will always stay boxes and all your perfectly-fitting stuff will fit just the same after scaling. The other option is to hold CTRL while scaling. This will make it scale in perfect increments such as 2X or 1/3X. The axes are independent, so you can make it exactly twice as tall without changing its width.
SHIFT is a useful key throughout many options. It generally makes whatever you're doing "perfect".

Cut Tool
The Cut Tool is pretty straightforward. You draw a line through an object, and it cuts that object along the line. Hold SHIFT to make a straight line. Take note that cut lines have more vertices than the normal for polygon surfaces. It can be more precise sometimes than the initial drawing. Zoom in for very precise carving. Anything you cut will be turned into two polygons, whatever it was before and no matter how precisely you made the cut. i.e. slicing a box perfectly in half will make two rectangular polygons instead of two boxes.
Take note that cutting can make some very tiny scraps. Also, the border between two cut parts resting on each other can have a lot of collisions at once (especially in Phun; Algodoo has an algorithm to reduce it but it isn't perfect), which can be laggy for big things cut into many pieces. If you want a breakable wall, it's usually a good idea to hinge most of the parts together and make the hinges breakable. A wall held together by breakable hinges can also have the No Self-Collision option checked, allowing it to collapse to almost nothing when destroyed.

Cloning with CTRL
There's a little button that says clone, but you're better off just holding CTRL and dragging. It clones all the selected objects and puts them under the control of your cursor, so you can put them wherever you want immediately. Hold SHIFT too to lock the movement to only one axis. That trick works for more than cloning, but it's very useful for making repeating patterns that follow a perfect grid. Hold CTRL and the mouse button down for a second or two without moving the mouse, and you have created a clone precisely on top of the original.

CSG Rocks my Socks
CSG stands for Constructive Solid Geometry. USE IT. It is the most powerful shape-building tool in Phun/Algodoo. It is used with two or more overlapping shapes. When you have a shape that's overlapping with another, you get four things you can do under the CSG menu, which is in the object's menu.
CUT: This will cut a line in the other shape where the border of the selected shape crosses it.
SUBTRACT: This deletes any portion of the other shape that is overlapping with the shape you have selected.
INTERSECT: This deletes any portion of the other shape that ISN'T overlapping with the selected shape.
ADD: This adds the outline of your shape to the other shape.
These tools are GREAT for making car bodies and other complex shapes into one polygon.

Gridlines
If you have Algodoo, there is a Grid tab in the Options menu. "Show Grid" will display a grid with the given number of axes, in which the largest lines designate the ones crossing the origin. When Snap to Grid is checked, everything you do will follow the grid lines and points exactly. Good for making a perfect piston, and you can also use the grid lines to make things with precise dimensions, like exactly 12 meters tall. Every line you draw in this mode will be straight or made of straight segments, but if you want to make a straight between distant and misaligned grid points, hold SHIFT.

Console
Open up and close the console by pressing F11 in-game. It will display in text what the program has been doing, and show errors when they happen. It's a decent debugging tool for when you're trying to script something, and can be used to modify your config file by entering your own code.

EXTRA TOOLS
These are some debugging and convenience items that you can add to your program without much difficulty, and most of them were included by default in earlier versions of Phun. They are small extensions to your config file, the file that remembers all of your settings while the program isn't running. Take note that if you know a thing or two about Thyme, you can do a lot to customize your config file and put loads of special options in it, from custom functions to spawn commands or more. To add these extra tools, open up the console and paste the following lines into it and press ENTER.

Code: Select all
`Keys.bind("f5", {App.drawCables =  ! App.drawCables});`

Makes F5 toggle a visualization of the forces used when the cable algorithm is activated. The cable algorithm makes chains way stronger and is only activated when they're stretched beyond a certain point. It can also apply to inter-hinged groups that aren't chains, for better or for worse.

Code: Select all
`Keys.bind("f6", {App.drawBodyCenters =  ! App.drawBodyCenters});`

Makes F6 toggle little green dots showing the centers of gravity for single geometries and bodies. Objects glued or fixated together are in the same body. The lines coming off the dots indicate the velocity of the bodies.

Code: Select all
`Keys.bind("f7", {App.drawVertices =  ! App.drawVertices});`

Makes F7 toggle little green dots showing where the vertices of a polygon are.

Code: Select all
`Keys.bind("f8", {App.drawCollisions =  ! App.drawCollisions});`

Makes F8 toggle red dots and lines showing the position and strength of forces from collisions. Green dots and lines show where and how far one object is embedded inside another. I have this one on most of the time. VERY useful for finding out what's wrong with a very complicated machine that isn't working.

Code: Select all
`Keys.bind("f9", {App.grid.grid =  ! App.grid.grid});`

Makes F9 toggle the grid. It's just easier and faster than going into the options menu.

That's it for the more complicated tools. Expect a guide on reducing lag soon!
Made some cool stuff a long time ago

Frank
[Best Tutorial Maker 2010]

Posts: 193
Joined: Mon Aug 31, 2009 6:37 pm
Location: the dark side of the moon

### Re: Beginner's Guide to Superior Construction

Reducing Lag and Improving Performance

Lag will be an issue for the extent of the foreseeable future, but there are ways to cut down the strain a scene puts on the computer and get a better simulation. Clicking Show Sim Info on the File menu will give you a little box with some random statistics, including how fast the scene is running compared to how fast you're trying to run it. It says XX% of realtime, but "realtime" is really how fast you want it to play. First things first...

Have a clean, centered scene
The New Scene button under the File menu is a good thing. You probably know about it, but if you didn't, it will save you some grief. It opens up a blank scene with nothing but four killer planes a kilometer in each direction, and the screen will be centered on the origin. The origin is the center of Phun's coordinate grid. Not being too far from the origin is a good thing because Phun's position variables only have so many digits; rounding errors become larger as the objects move away from the origin. It usually doesn't matter much, but if you travel really huge distances from the origin, very strange things can happen.
The other nice thing about a new scene is that it has no leftover junk from whatever you were doing before. Many scenes have junk in them that shouldn't be there, like invisibly small polygons or stray water particles. Usually not a big issue, but it helps. CTRL+A will select everything in the scene and sometimes you'll spot junk with that, but using F6 or F8 in the extra tools above will make anything out of line stand out. Not too important, but it does reduce the number of contacts in the scene, which is explained later.

Careful with water
Water is laggy. That will probably never change. It's just a lot harder to simulate a liquid than a solid. Water lags the most when it's colliding with a geometry; water-water collisions don't use nearly as much power. Many water scenes I see contain lots of particles stuck between two overlapping boxes or something. Should be impossible, but it happens all the time. That's two high-end collisions per stuck particle every single tick of the game. Like other kinds of junk, drawing collisions or drawing body centers will make them stick out so you can delete them. To avoid particles getting stuck in the first place, there are a few things you can do. One is to angle the geometries you're using to contain the water in a way that they push the water out if it gets stuck between them. thick walls are a good thing because water is very easy to force through other objects. Another way is to use CSG to create containers with fewer, larger parts that have few overlaps to get stuck in.

Careful with spawners
If you have a spawner, be certain that it's built so that the product never builds up. If it's a machine gun, have a button to destroy the projectiles ( using the format of: ButtonDestroy = "X"), or make sure they hit a killer plane eventually, or something. For a rocket, killers built into the machine are the way to go.

Polygons are not preferred
Polygons take more processing power to render than any other geometry, they can get stuck inside each other and boxes, and their collisions still have issues. Not to say they are bad at all; polygons are a necessary core part of Phun. Just take care not to turn your boxes into polygons by scaling them wrong. Geometry Actions > Transform to Box/Transform to Circle are useful things. I've seen a lot of scenes that occasionally fail or don't otherwise work right, just because they use a polygon where a circle or a box would have been better. Stuff like gun mechanics benefits greatly from avoiding polygons. On the other hand, if you're making an offroad course or something, you shouldn't draw it out of one immense polygon to save processing power. The way their collisions are handled, small objects can get stuck on gigantic polygons in weird ways. There will be less sticking and you will find yourself making a better course if you make it out of many smaller chunks.

TEXTURES
You can import most kinds of image file to Phun. There's even a Texture tool for it with webcam support. A texture will add a lot the file size, but won't lag nearly as much as trying to draw out something detailed with polygons. It's a pet peeve of mine when people post art scenes made out of hundreds or thousands of polygons, especially when the finished product doesn't do anything. Large numbers of polygons will make a scene lag even if they never collide with anything. If you really want to show off artwork in motion, draw it in Paint and use it as a texture. Texture functionality was added for a reason.

Reducing Collisions
Collisions, after water, are the biggest source of lag. A more precise term would be contacts. I've calculated some basic elastic collisions on paper before, and it was two pages of very complicated work for a single problem. As far as I know, every time one object touches another, Phun has to complete at least one of those equations, but probably a more complicated version.

This is a problem for brick walls, but the biggest way to save lag from contacts usually involves objects that have been cut and left to sit against each other. Two polygons whose outlines match each other perfectly (or a polygon fitting perfectly around a circle or box) and rest against each other can have a huge number of contacts. In every version before Algodoo, perfectly-fitting polygons would have one contact for every touching vertex on each polygon. All polygons, even ones with only straight lines in them, have dozens of vertices. That means an object that's been sliced into several pieces can have hundreds of contacts. It also means that a ball bearing array or a ball joint can have dozens to hundreds of contacts. To counter this, try to use hinges instead. Use breakable hinges and no self-collision instead of relying on collisions to hold up a cut wall, hinged wheels instead of bearings, etc.
If the polygon is large and the object resting against it is small, it may only touch one or two vertices, though. You'll see if you turn on collision-drawing.
NOTE FOR ALGODOO USERS: Algodoo contains an algorithm to reduce the number of contacts for this kind of situation. It isn't perfect, though.

In a similar vein, using polygons for treads will lag. They will spend a lot of their time jammed partway inside the ground, causing the collision issue described above. When you really want to optimize your scene, it's worth trying to make your chains with as few links as possible too.

Attraction
Attraction can cause some lag, but usually not too much. Every object in a matching collision group is factored in, so the lag from each attracting or repelling body increases with the size of the scene.

Glue to Background and Glue Together
Going to the Geometry Actions menu and using the gluing tools will get you all the benefits of a fixate without the extra object to remember and render. Any object you don't want to ever move should be glued to the background. Inter-fixated groups will look cleaner and use minutely less memory if they are glued instead.

I think that's about it for reducing lag and the tutorial in general. Don't be surprised if I change my mind and add to this though!
Last edited by Frank on Sun Jan 24, 2010 9:09 pm, edited 1 time in total.
Made some cool stuff a long time ago

Frank
[Best Tutorial Maker 2010]

Posts: 193
Joined: Mon Aug 31, 2009 6:37 pm
Location: the dark side of the moon

### Re: Beginner's Guide to Superior Construction

ty i have been looking for the "center of gravity" one!

simey j dude

Posts: 61
Joined: Sat Sep 05, 2009 3:11 am
Location: under your bed!

### Re: Beginner's Guide to Superior Construction

Thank you for these good and detailed guides!
Zirrus

Posts: 2
Joined: Sun Sep 06, 2009 3:40 pm