Archive for the ‘physics’ Category

I recently spent a little time recreating some older spring physics code, in AS3. It is the sort of thing you’ve probably seen 100 times over the years, but working on stuff like this never gets old to me. I’m sure I’ll recode it again in a few years 🙂

In this post you’ll see a quick demo of the spring behavior, a link to the source, and a little bit of explanation of physics.

click to view example

A little explanation
Spring behavior can be coded by first understanding Hooke’s Law. Imagine that you had a spring and a mass attached to the end of that spring. Hooke’s Law specifies the force that the spring applies to the mass when displaced from it’s rest position.

F = -k*x

Where x is the amount of displacement from the rest position, k is the spring-specific constant (spring “tightness”), and F is the force exerted by the spring on the mass. The force tries to get the mass to return to the rest position, so it is in that direction.

In my code I use the Vector3D class to generalized the equation from just x to x and y. Also, I connect two equal masses with a spring rather than just having a spring with a single mass. Two masses and a single spring obey the same equation. It is just that the two masses have the force applied in opposite directions.

F = -k*displacement

Where displacement is the distance between the two masses minus their distance at rest. If F is applied to one of the masses, -F is applied to the other one. The minus sign can be manually applied to wrapped into the displacement if calculated relatively.

A single mass can be connected to N springs. To achieve the correct resultant behavior the forces acting on each mass need to be summed each iteration and then used to affect the velocity.

What is missing?
I am not properly letting the system lose energy. To properly lose energy the springs should apply a resistive force against the direction of movement of the mass in the amount b*velocity, where b is a spring-specific constant. Currently I’m just slowing all things down by a factor of ~.95 every iteration…sort of like if the whole system was in oil.

Also, I’m currently assuming all masses are equal. This could be easily enhanced to support varying masses.

What is it good for?
This type of behavior comes up from time to time in games. Off the top of my head I remember using this behavior to simulate how a car shakes due to breaking quickly (from side view). The game was Pimp My Ride: Crowd Magnet. I think I created a rectangle with 4 masses and 6 springs. The springs connected the perimeter and the two diagonals. I hid the visibility of this frame but had it move on screen along with the car. I used the two top nodes to tell how to move/rotate the frame of the car as it stopped.

There was also a non-game UI that I created years ago for a client similar to what you see at Visual Thesaurus. Nodes were connected via springs, but then the nodes themselves also had a gravitational pull toward each other and a slight magnetic repulsion. It creates an interesting interface!

You could use a spring system to mimic a blob or cell. Turn off the lines and nodes and fill it with a color and allow it to bounce off of things. Or make a chain/rope that your character can swing from. Uses for this pop up from time to time!


Read Full Post »

This post is related to a post I made a few days ago titled Ball Bouncing off Line Calculation.

The code used to determine the collision reaction of a ball bouncing off of a line shown in the previous post is the same as the code required to do it in 3D! So, you can achieve a ball bouncing off of a plane. Or, when you add gravity plus a little damping on the floor you can have things rolling around.

This example was designed so the ball would hopefully roll for a long time without rolling off. But if the ball was going a little faster you’d see how it can bounce into the air a bit if it hits an incline fast. Anyway, just thought I’d share!

(marble madness anyone?)

Note: if it seems a little slow, it is probably the 5 other blogs on this page that are running more intense things.

click to view example

Read Full Post »

I’m working on a new game that requires me to dig into some physics and math that I haven’t touched in a while. The Vector3D class was introduced with Flash Player 10. I used this as an opportunity to get familiar with that class and see if it could make life a little easier, and it does!

The Vector3D class keeps track of x and y (and z if you want it to) values. It uses the sign on the value to imply direction. You can then perform cross products or dot products and a few other basic vector actions via its methods.

Conceptually, here is how to make a ball (or sphere) bounce off of a line (assuming you can calculate that the collision is occurring).

  1. Use a vector for the ball velocity
  2. Use a vector to represent the line normal (perpendicular to line)
  3. Do simple vector algebra to get new velocity

Step 1
Represent the ball velocity as a vector. For instance,
var velocity:Vector3D = new Vector3D(2, 0);

Step 2
Represent the line normal as a vector (which it is). For instance,
var normal:Vector3D = new Vector3D(1, -1/line_slope);

Step 3
Do the vector math when a collision is detected. You can imagine that the velocity vector can be projected onto the normal, and it is only that bit that will be reflected off of the surface. So, we’ll project it onto the normal and set the net change to 2 * this value. 1* the value makes the velocity 0, 2 * the value sends it back an equal amount. We then take this reflected vector and subtract from the incident velocity vector.

var reflectionScalar:Number = 2 *velocity.dotProduct(normal);
var reactionProjection:Vector3D = normal.clone();

Download source

click to view example

Read Full Post »

Predictive Shooting

It has been quite a while since my last blog post. I have a lot of useful ideas that would make great posts but some super cool Electrotank projects have been sucking up my life.

One of our smaller projects is a tower defense game due out soon. For this game we developed a way to precisely answer this question: If a tower wants to fire off a bullet right now and hit a specific moving target, at what angle should it shoot?

The end result is a function that can find that angle for you. If you use it then you’ll get a direct hit every time! If that makes your AI too perfect, then tack on some random variation to the angle and it will be less precise the further away it is from the target.

The known information are these things:

  • Current position of the target
  • The target’s velocity (acceleration not supported here)
  • Position of the tower
  • Shot speed
  • Length of tower muzzle, if any

The equations of motion for the target are:

x1 = x1o + v1*cos(theta)*t
y1 = y1o + v1*sin(theta)*t

  • x1o – Current x position of target
  • y1o – Current y position of target
  • v1 – Speed of target
  • theta – Angular direction of target
  • t – Time variable

The equations of motion for the bullet are:

x2 = (x2o+n*cos(alpha)) + v2*cos(alpha)*t
y2 = (y20+n*sin(alpha)) + v2*sin(alpha)*t

  • y1o – Y position of tower
  • x2o – X position of tower
  • v2 – Bullet speed
  • alpha – The unknown angle to shoot
  • n – The length of the muzzle
  • t – Time variable

You can download the source code which contains the solution here:
Download source

click to view example

Read Full Post »

I was going through a list of games that I’ve programmed recently and was reminded about Dynamite Fishing, a game we (at Electrotank) created for Comedy Central. For that game I programmed a series of wave physics classes that shows waves moving through water at a side view.

I had fun creating that and thought that I’d rewrite it a little more cleanly for ActionScript 3 and make the source available. It allows you to inject waves at any x position with any amplitude. Waves obey superposition – that means that you can continually inject waves and they’ll add/subtract each other to produce the appropriate result.

In the source provided you can specify any DisplayObject to show up and float in the water.

Download source

In the example below you can just click anywhere to inject a wave. Notice too that the water never quite settles down. That’s because I set it up to inject a small disturbance at random intervals.

click to view example

Read Full Post »