PDA

View Full Version : Flash Help



Kalub
April 4th, 2009, 06:14 PM
Hey, yea, hi....


I'm just playing with flash attempting to make a simple platformer game engine. Anyways, I can't figure out how to do slopes. Would anyone be so kind as to link some code or kinda walk my newb ass through it?

(Keep in mind, I've been attempting this forever, so I might have already tried your code if you found it on google)

This is in AS2. Thanks.

legionaire45
April 6th, 2009, 10:19 PM
Slopes? As in, a sloped surface? Or finding the slope of a function?

For a sloped surface, well, lets talk some physics for a second.

For every force exerted there is an equal and opposite force that is also exerted on the thing that originally exerts the force. This is Newton's 3rd law (IIRC). We also need to consider Newton's 2nd law, which states that Force is proportional to the change in it's momentum in a given time; For our purposes, just remember that Force is mass x acceleration., or Fnet = ma. This also works for the components (basically, what part of the vector is along a single axis, either x, y, or z), so Fnet,z = maz for example.

Why is this part important? Lets say you have a mass of 60kg and you are standing on a perfectly flat surface. Because there is gravity, you have weight (W, or Fg), which is a force. This force (your weight) will equal roughly 600 Newtons (kg*m/s/s [kilogram meters per second per second]) because the acceleration due to Earth's gravity is 9.8 meters per second per second (m/s/s). This is Newton's 2nd law.

When trying to figure out where these things are going, you need to identify what the acceleration of the object is.

Now, if you are imparting a force on the Earth and have acceleration, why aren't you moving through the ground or free falling? This is where Newton's 3rd law comes in. Your mass is exerting a force (your weight) on the earth and the earth is also exerting a force on you - this is called the normal force (Fn) and is exactly equal to your weight. Let's draw a force diagram for this.

http://www.morgancabral.com/writing/phys_article/fig1.png
So that means that this mass is perfectly balanced while sitting there by itself. But what happens when we sit it on an incline?

Something I didn't mention before - the normal force is always perpendicular to the surface that the force is being applied to. So if our object is on an incline than the force diagram will look like this, assuming there is no friction:

http://www.morgancabral.com/writing/phys_article/fig2.png
Notice now that the normal force is pointing off in a different direction (in this case the incline is at an angle of 45 degrees) - perpendicular to the incline. The normal force is no longer canceling out the force of gravity; in fact, the y component of the gravitation force is greater than the upward force produced by the normal force. The normal force is also pointing away from the incline, which means that there will be some force on the x-axis. What does all of this mean?
http://www.morgancabral.com/writing/phys_article/fig3.png

Yup, the object will slide down the incline. But how do we determine the net force of the object?

The easy way is to just remember this formula: Fnet = Fg * sinθ, where θ is the degree of the incline. If you want a detailed description of how I got this, PM me.

Back to our example; suppose you weigh 60kg and are lying on a frictionless 45 degree incline. Assuming you aren't clinging onto the incline for dear life, you would be sliding down the incline with roughly 424.26 Newtons of force: Fnet = ( 60 x 10 ) x sin(45) = 600 x 0.707 = 424.26. From here you can find your acceleration by using Fnet = ma; your acceleration would end up being 7.071 m/s/s.

Luckily, adding friction is really simple. To begin, there are two kinds of frictional force; static friction and kinetic friction.

A good example to explain these two types of friction is that of a crate on a surface with friction. If you lightly tap the crate, it probably will not move. If you continue to push the crate a little bit harder, it still will not move. However, if you put every ounce of energy you can into moving it, the object will begin to move, although it feels as if something is lightly pushing the crate back towards you; not enough to make it impossible to move, but it still isn't incredibly easy to move.

The friction that you experience just before the crate begins to move is the static friction; as soon as you reach that maximum amount of static friction the object will begin to move and you will be experiencing kinetic friction. Kinetic friction is light opposing force you feel while moving the crate.

In picture form:

http://www.morgancabral.com/writing/phys_article/fig4.png

Where the x-axis represents the amount of force you exert and the y axis represents the amount of force the crate exerts back through friction. Basically, at any point before the maximum static friction, the object will stay put. Any point after that and it will move.

The math behind determining these forces is very easy. What makes this even easier is that the frictional force is perpendicular to the incline or surface; The net force is just the force of the object minus the friction.

The maximum force of static friction is determined by the function fμs,max = μs*Fn. Because an object does not move while under the influence of static friction, the net force is always 0 in these instances and there is no acceleration.

The force of kinetic energy is determined by the function fμk = μk*Fn. In this case there is a net force (since the object is moving) and this can be determined by subtracting the force of the object sliding down the incline ( F = Fg * sinθ ) by the frictional force and setting it equal to the net force. So...

Fnet = Fg x sinθ - μk x Fn

If we wanted to find the acceleration of the object we could combine this with the formula for net acceleration like so....

Fnet = ma
Fnet = Fg x sinθ - μk x Fn
ma = ( ( m x g ) x sinθ ) - ( μk x( m x g ) )
a = g x sinθ - ( μk x g)

There you go. You now have the acceleration of the object.

"But, umm...How does this relate to Flash again?"

Using the following formulas you can get the displacement, velocity or acceleration as long as you have a few of the others. I'll let you figure the algebra part.

(note: these functions only work when acceleration is constant)

v = v0 + at; missing (x - x0)
x - x0 = v0t + (at^2)/2; missing v
v^2 = v0^2 + 2a(x - x0); missing t
x - x0 = ((v0 + v)t)/ 2; missing a
x - x0 = vt - (at^2)/2; missing v0

Where anything with a 0 following it is the starting value ( I guess it could also be the value as of last rendered frame; not sure about that though ).

For the programming part, I'm honestly not sure how you should go about implementing that. From a lesson I sat in during a visit to Digipen I heard that one way to go about doing that would be to make a list of forces for every object that is moving, add those up at the end of a frame, find the acceleration and displace the object based off of that. Other than that, I really don't know.

I'll leave you to figure out the rest :P.


If you want some more material/better explanations, there's a ton of stuff here (http://www.gamedev.net/reference/list.asp?categoryid=28).

Limited
April 6th, 2009, 10:35 PM
I think he means, how to tell if theres a collision on a slope, as hes making a platformer.

If thats true, I'd first do bounding box collision to see if its within the surrounding box, then do pixel perfect collision using pixel colour of the ramp part. How to find the location to put the character? Not 100% sure, but it will involve geometry calculations.

Also legionniare, we need to talk.

legionaire45
April 7th, 2009, 03:12 AM
After talking with Limited about some stuff, I realized that there are a few more things that I should note.

It is incredibly important that you watch what units you are using. You cannot add velocity and acceleration, for example; this would be like adding mass and speed, it just doesn't give you anything and doesn't really work. If you have to, write things out on paper before programming them. Do some sample math, try whatever works. What I explained was more of a math thing than a programming thing but you must keep these math concepts in mind when working on your implementation.

Another thing that may make your life easier: work under the assumption that your acceleration is constant along each axis. Working with gravity as the only vertical acceleration and the player's movement for the horizontal will not only simplify what you have to do but it will also allow you to use those formulas I mentioned. Those formulas are really useful, make sure to use them.

Here is a description of the three basic units that you will be working with, if my quick description above didn't help.

Displacement is a vector (it has magnitude and direction) that describes how much something has moved. To figure out the displacement from one point to another, simple subtract the new position from the old. Mathematically, xnet = x - x0. Normally this would be measured in meters or feet ( m ) but you could probably get away with using pixels for displacement instead of translating it from meters.

Velocity is another vector that describes the time required to move from one point to another. You could also say that it is the speed, but that is not technically correct since speed is not a vector. Anyway, velocity is measured in distance units over time. Again, you would normally use meters or feet per second ( m / s ), but since we are dealing with a computer you could probably get away with pixels per second.

Acceleration is yet another vector but in this case it describes how fast something gets fast. It is usually measured in meters or feet per second per second ( m / s / s ), but again you could probably get away with pixels per second per second.

Using these units along with t and mass, you should be able to find force and the other units, assuming you left your acceleration as a constant. You should have a better idea of what these units are.

Kalub
April 8th, 2009, 07:51 PM
Holy shit, k no?




I just need to test basic collision, and if there is collision move the player up 1px y. I figured that out, now I also found out how to have the player's rotation the normal of the angle of the slope. With a second point to compare I just use Math.atan2(y1-y2, x1-x2) and then assign it to my character rotation point.


However, thanks for the physics class :)

legionaire45
April 8th, 2009, 11:01 PM
"BUT YOU AREN'T DOING IT THE ACCURATE MATHY WAY >=U"

You're welcome, I needed the studying anyway xD.

Kalub
April 9th, 2009, 09:23 PM
:awesome: I'm finished

Results:

width=550 height =400

InnerGoat
April 9th, 2009, 09:29 PM
Hotlink failure.

Kalub
April 9th, 2009, 09:33 PM
Ugh, that's so gay.... fixing

Limited
April 9th, 2009, 09:35 PM
http://megaswf.com/upload_files/bbc090ba931588cdfcabe8e9e51e0818.swf

Click link ^.

So how did you get it to detect where the slope was?

Kalub
April 9th, 2009, 09:51 PM
It uses the players origin, and another object's origin. Then it gets the tangent of that angle and then converts the radians to degrees and applies them to player rotation.



radians = (Math.atan2(this._y-slopedot._y, this._x-slopedot._x));
degrees = radians*(Math.PI/180);
this._rotation = degrees;

legionaire45
April 10th, 2009, 01:47 AM
Limited's link isn't working for me =/. It's saying I need a member's account or something.

Kalub
April 10th, 2009, 12:27 PM
What about the one on the other page?


GAH FUCKERS BLOCKING MY HOTLINKS

Ill host the god damn thin myself

width=550 height =400

Limited
April 10th, 2009, 01:38 PM
Cant imageshack host swf's?

Kalub
April 10th, 2009, 04:30 PM
You have to download some gay toolbar or something, f-that

MetKiller Joe
April 10th, 2009, 04:43 PM
Huh, this might come in handy. I'm trying to do collisions between circles. Gave me an idea.