PDA

View Full Version : gravity?!!?!?



Hansol
February 8th, 2007, 09:51 PM
Could anyone please explain how gravity to work in the simplest ways for a 2D game? i see the code, and know how it works; but i like figuring out what it all means and so later i wont have to find out other physics from tutorials,basically wondering how they figured out how gravity works, and all those other fancy scripts like tiled games and all , unless there is a tutorial explaining gravity and other physics x.x, hope i explained my question properly :) thanks.

Nich
February 8th, 2007, 10:13 PM
the simplest way to add gravity to your game is use the classic physics formulas

v = a*t
d = v*t

where v is the velocity, a is the acceleration, t is time, and d is displacement (movement)

translated into flash:

yspeed += yacceleration
obj1._y += yspeed

Loop that every frame, and the speed will grow, pulling your object down. To create a jump, make your yspeed a negative number. This will cause your object to begin moving upwards (remember that negative on the y axis is up in flash), but it will be slowing down because you're adding a positive acceleration to it until it eventually becomes positive. This will create the nice parabolic motion we're used to seeing in games and life.

Also note that this only affects the y axis, and a separate set of variables will need to be created to control motion on the x axis. You should also cap your yspeed variable at a certain amount, as it can grow very quickly.

Insane Knux
February 11th, 2007, 05:58 PM
Wouldn't you need to set yacceleration?
Otherwise it would equal zero, thus no movement.
Something like:


yacceleration = 1;
onEnterFrame = function(){
yspeed += yacceleration;
obj1._y += yspeed;
}

Correct?

bombsledder
February 11th, 2007, 06:08 PM
yacceleration = 1;
onEnterFrame= function(){
yspeed += yacceleration;
obj1._y+= yspeed;
}


might be easier to read

Insane Knux
February 11th, 2007, 10:21 PM
Woah I don't know what happened on my code. o_O
:huh:

Nich
February 11th, 2007, 11:05 PM
I assumed he would know that he couldn't just paste that into flash and have gravity. I didn't even define yacceleration, so the object would likely have been moved to 0 on the y axis, having been given an undefined value.

bombsledder
February 11th, 2007, 11:23 PM
sorry your wrong, paste this into the first frame


yacceleration = 1;
onEnterFrame= function(){
yspeed += yacceleration;
obj1._y+= yspeed;
}

now run a trace on yacceleration and see what you get, only thing he didnt put is yspeed so you wouldnt get a obj1._y that would be undefined, but yacceleration according to AS1 is defined right here yacceleration = 1;

Insane Knux
February 12th, 2007, 12:52 AM
yspd = 10;
yacc = 10;
obj.onEnterFrame = function(){
yacc += 1;
yspd * yacc
this._y += yspd;
}
That works very well for me.

bombsledder
February 12th, 2007, 07:13 AM
ok not seem like im trying to find things wrong with your code but shouldnt

yspd * yacc

be

yspd *=yacc or maybe yspd +=yacc

otherwise yacc is a waste of variable, no?

Nich
February 12th, 2007, 08:18 AM
sorry your wrong, paste this into the first frame
ActionScript Code:

yacceleration = 1;
onEnterFrame= function(){
yspeed += yacceleration;
obj1._y+= yspeed;
}




now run a trace on yacceleration and see what you get, only thing he didnt put is yspeed so you wouldnt get a obj1._y that would be undefined, but yacceleration according to AS1 is defined right here yacceleration = 1;

I meant that in my "code" I didn't define yacceleration. Your code is fine (apart from the fact that you didn't define yspeed, as you mentioned).

My point was that the concept was fairly simple to grasp, and I didn't feel the need to hold the op's hand in translating my example to actual code. I wasn't commenting on your code at all.

nathan99
February 12th, 2007, 08:31 AM
MovieClip.prototype.moveWithGravity = function(vel:Number, gravity:Number):Void {
this.dy = vel+gravity;
this.onEnterFrame = function():Void {
this._x += this.dx;
this._y += this.dy;
this.dy += gravity;
};
};
someMovieClip.onMouseDown = function():Void {
delete this.onEnterFrame;
this._y = 233;
this._x = 94;
};
someMovieClip.onMouseUp = function():Void {
this.moveWithGravity(-5, 1);
};