-
How can you accurately simulate gravity with collisions?
Like for a platformer game... I want to use physics-simulations for gravity. Also, when the object affected by gravity collides with another object it should act differently, depending on the angle of that object(If the object is horizontally placed the object shouldn't fall anymore, if it's at a slight angle it should go at that angle slowly). I've done this with platformer game-style simulations, like slopes and all that(Examples of such techniques: http://www.emanueleferonato.com/2012...d-platformers/ ), but I want to use physics to apply gravity and object collisions as forces. Gravity should be a force with a direction "down", while other objects depending on their rotation should have a counter-gravity force that forces the main object to go along them.
I have come close to simulating this... But it is off by a very small amount on every step...
Here is my code(It's messy and on the timeline, sorry...):
Actionscript Code:
import flash.events.Event;
addEventListener(Event.ENTER_FRAME, ef);
s.rotation = -45; /*s is the object that the main object should collide with (It's like a static object, like a wall/slope)*/
//perpendicular = m • g • cos (45 degrees) = 6930 N - This is the main formula
var g:Number = 9.81 / 2; //Made g smaller to see the offset better
function ef(e:Event):void { a.y += 1 * Math.sin(90 * Math.PI / 180) * g; //a is the main object, it goes down with this line, this is the gravity force if (a.hitTestObject(s)) { var per:Number = 1 * g * Math.cos(45 * Math.PI / 180); //The formula a.y += Math.sin((-45 - 90) * Math.PI / 180) * per; //-45 - 90 because I need to subtract 90 from the wall's angle to get the main objects sliding angle on the wall. This is the wall's force applied to the main object on the y axis. a.x += Math.cos((-45 - 90) * Math.PI / 180) * per; //This is the wall's force applied to the main object on the x axis. } }
Here is the FLA: http://www.mediafire.com/?55y422jl8mn3obj
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|