martes, 24 de marzo de 2015

Control de la coordenada "y" al pulsar una tecla

En el menú de script en onKey debes añadir:

e)=>{
    keys.isdown("a") ? {
        pos = [pos(0), 1]
    } : {}

}

miércoles, 13 de noviembre de 2013

Demostración de salto

Salta con la tecla s

Coloca este script en el menú de scripts  onKey

keys.isdown("s") ? {
        pos = [pos(0) + 1, pos(1) + 1]
    } : {}



Bájate si quieres esta escena para estudiar la demostración.

Demostración de subir y avanza

Puedes incluir estos scripts en onKey

Subir 

keys.isdown("Up") ? {
        pos = [pos(0), pos(1) + 4]
    } : {}
Avanzar
keys.isdown("Right") ? {
        pos = [pos(0) + 2, pos(1)
Ambos

keys.isdown("Up") ? {
        pos = [pos(0), pos(1) + 4]
    } : {};
    keys.isdown("Right") ? {
        pos = [pos(0) + 2, pos(1)]
    } : {}


Bájate si quieres esta  escena con los scripts

jueves, 31 de octubre de 2013

Frank's guide to sweet suspension

Postby Frank » Tue Sep 14, 2010 5:49 am
This is a guide, constructed of many generally stand-alone articles, on how to construct suspension that will help a wheeled car zoom over bumps and various obstacles quickly and efficiently. If you're looking for tips intended for slowly crawling over extreme obstacles, there probably isn't much of value in here. I'm hoping this guide comes out useful to a wide range of skill levels. It's primarily intended for people who have already built at least a few cars, though.



DAMPING
If you're attracted to a game like this, you probably know what spring damping is, but not everyone does and not everyone knows how to use it best. Damping is essentially how much energy the spring dissipates. You'd probably want extremely low damping in clockwork, where efficiency is extremely important. Suspension usually needs pretty high damping, unless you want your car to bounce a lot. Maxing out the damping on the springs in your suspension isn't always a good idea, though. Too much can make it kinda sluggish and hard. If the damping and spring strength are too high relative to the mass of objects they're attached to, the physics will also freak out and vibrate and/or explode. Every car and every design has its own optimum damping properties, and what works for one course may not work perfectly for any other even on the same car.

Why Pistons Suck

The long and short of it is that piston-based suspension breaks. It's the only real problem, but it's a killer. Algodoo runs at only 100 frames per second. Because of this, sudden jolts can move one part outside of the other in just one time step, and no collision is registered. Or, slower but more powerful forces can simply push one object through another. Anything that relies on collisions to hold it together is going to break if it gets hit hard enough. Building bulky pistons can help this problem a lot, but they can never be indestructible. Increasing the frequency that Algodoo runs at can help this a lot, but it increases lag and may cause many scenes to work differently.

Better Alternative to Piston Suspension
So you read the above about how piston-based suspension is fragile, and now you're thinking, "OK, so how the hell can I make suspension without some kind of piston?" Your best bet is torsion bars. The idea is simple. You put the wheel on one end of a stick, you hinge the other end of the stick to the body, and you use a spring or something to hold it in place. The design, angle, and placement of a torsion bar can have as much influence on its performance as the spring. Build it wrong, and you'd be better off without any suspension at all. Build it right, and it will let you zip over huge bumps like they aren't there, or even help your car get over obstacles that would normally be impossible.

Unsuspended Weight and Center of Gravity

Unsuspended weight is the amount of mass that isn't on top of your suspension. It's a bad thing. Making your wheels and other suspension parts heavy will give you very clunky and sluggish suspension that, to a certain extent, defeats the purpose of suspension. The point is to cruise over bumps without influencing the momentum of the car. If a quarter of the car's mass stops going forward and gets redirected upwards every time the front wheel hits a bump, it isn't doing much good.
On the other hand, making all of the lowest parts of the car very light will raise the car's center of gravity and give it trouble on hill climbs and generally be more likely to land on its top. A low center of gravity can be achieved without harming the suspension by using small weighted blocks in the frame and keeping the rest of the car as light as possible.

Ideal Front Suspension
This guide assumes a two-wheeled car. All the same principles apply to more complicated systems, though. Your front wheel should be the first thing that hits a bump. Your front suspension is responsible for the hardest part of getting over an obstacle, getting your center of gravity on top of it. It has the full mass of the car pushing on it when it hits bumps. It isn't normally the primary power source of the vehicle. Because of these factors, it needs to be light and soft and capable of giving and resetting very quickly. One of the most effective features that can be built into front suspension is the tendency to move back as it moves up. An obstacle that takes a quarter of a second to get over will stop your whole car without this feature, but if the suspension has some front-to-back give, the front wheel can take a little time trying to get over it without slowing down the rest of the car.
With pistons, this is easy. Just tilt it a little, and life is good. It's a little trickier with torsion bars. Ideally, the torsion bar is hinged above and in front of the wheel, letting it swing up and back when it hits something. Unfortunately, not every car design makes this possible. When it isn't, behind will work as long as it's not far from horizontal. Another thing to do is build some give into where the bar hinges to the frame. I've had the most success with putting the torsion bar on another, vertical torsion bar. Vertical motion is handled by the bar attached to the wheel, and horizontal forces are handled by the vertical one. Hinge suspension techniques work well in that application.

Ideal Rear Suspension
The back wheel faces a simpler task. By the time it hits a bump, it has the whole weight of the car pulling it over. Effective power transmission is much more important for the rear suspension, too. While it's still a good idea to angle the torsion bar above and in front of the wheel, the best angle for rear suspension is much closer to horizontal than for front suspension. When the car is accelerating, you don't want the forward force of the wheel to get translated into upward force by a high-angled torsion bar. That will make it jump a bit and seriously reduce the accelerating ability of the car, and also cause a hell of a lot of trouble on a hill climb. A little bit of side-to-side play can help some, but it should not be added if it reduces the grip of the wheel.
For rear suspension, it's OK to be a little clunky. Light weight isn't quite as important, and power transmission shouldn't be seriously sacrificed for anything. The momentum of the car will do a lot to help the rear wheel get over things.

Soft Wheels

Soft wheels have their applications. They can do a lot of the little fast and sharp bumps with efficiency you could never get out of the main suspension. The problems are that they can reduce the traction of the wheel, many designs can get broken or tangled, and they often don't perform well at high speeds. There is only one basic design that I have ever found entirely satisfactory: an extremely low-density circle hinged in multiple places to an overlapping, smaller, regular-density circle. The smaller circle produces/transmits the power and only the larger circle normally makes contact with the ground. If both circles are aligned perfectly with each other, this design has no issues at reasonably high speeds and never breaks. It can still have grip issues, though, depending on how it's calibrated. I rarely doubt whether this design is better than an equivalent hard wheel, but I'm always asking myself that kind of question when I try other designs.

Transmission
Adding transmission to a suspension setup isn't actually that hard. A drive shaft built from three or four evenly spaced connecting rods can double as a torsion bar. A joint can be built by making a drive shaft to a circle, and then building another drive shaft from that circle to the wheel that needs power. With a joint in the drive shaft, it becomes possible to add some front-to-back give.

Building the Indestructo-Car
The key to making an extremely tough car is ensuring that no part has more than one possible resting position. Pistons have many: one spot inside the track, and a whole circle of positions once the sliding part gets knocked out. And it will get knocked out. Torsion bars usually have two: one with the wheel where it belongs, and another with the wheel usually sticking up instead of down. Depending on design factors, a few other funky angles are possible. You don't want this happening, so there are a couple of ways to ensure it can't.
One way is to build the springs so that they only reach a balance when the wheel is where it belongs. The easiest way is to use only one spring with zero target length, but not every car design makes it possible to do that. Multiple springs with non-zero length can work too, and it isn't hard to do, but the mechanics of achieving it are hard to explain.
Another way is to use a non-colliding chain to limit the range of motion in the torsion bar, so that it can't ever get knocked into the second resting position of the spring. A chain can also be used to make it impossible for the suspension to bottom out.



This guide may or may not be added and modified over time, depending on whether I notice any flaws or reader requests/corrections. Here is a list of some very successful cars I made while learning these principles.

http://www.algodoo.com/algobox/details.php?id=34807
Probably my favorite car ever. Smooth suspension includes chains, soft wheels, and lots of front-back motion. Hull design and center of gravity are great for landing and staying on its feet.

http://www.algodoo.com/algobox/details.php?id=30320
Demonstrates transmission and suspension working together. Also abuses Algodoo's physics to get optimal power out of a hinge with very limited energy, but I'm not saying how.

http://www.algodoo.com/algobox/details.php?id=37350
It's not a wheeled car, but it demonstrates the principles of torsion bar positioning well. The hull is designed to help it get over tall obstacles.