kindagravity.txt

(1 KB) Pobierz
I'm not a physicist, but I made a 2D game about gravity/magnetism:
http://experimentalgameplay.com/game.php?g=484
Still, I do not know correct gravity-formulas, so I just pulled of a 
formula to get an factor of attraction wich strongly increases when objects 
get closer to each other. I used this value to accelerate one of the 
objects. Flash code would be something like this:

//at lease you need this vars
private var x1:Number;
private var y1:Number;
private var y1:Number;
private var y2:Number;
private var distance:Number;
private var gravityX:Number;
private var gravityY:Number;

//distance between the coordinates, power of 2
private function function distance(x1:Number, x2:Number, y1:Number, 
y2:Number):Number{
return Math.pow(x1-x2,2) Math.pow(y1-y2,2);
}

//gets values you can use for accellerating objects etc. You can multiply 
them to have a certain grade of ?gravity?
private function setGravity():void{
gravityX=(x1-x2)/distance(x1,x2,y1,y2);
gravityY=(y1-y2)/distance(x1,x2,y1,y2);
}

Wrote it on the fly, so there may be typos... but it should work somehow 
like this. Again, this is not ?real- gravity, but in my case it was quite 
adequate (for 2D, but if you need 3D you can implement it quite easily).

Good luck with your project, Marc
Zgłoś jeśli naruszono regulamin