A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Implementing 2d physics engine?

  1. #1
    Senior Member x-death's Avatar
    Join Date
    Aug 2009
    Posts
    175

    Implementing 2d physics engine?

    basically what i want to do is get a 2d physics engine and learn how to put it into use in order to create a platform engine. the end result i'm hoping is that the character moves smoothly, and can collide with various shaped floors, even circular.

    the problem lies in the fact i don't understand how to implement this stuff, and i don't understand how alot of it works. i don't understand how to do movement of slopes even on a simple level. i don't know if any of that matters.

    i would appreicate anyone who can teach me how to use a physics engine to accomplish this!

  2. #2
    Senior Member
    Join Date
    Oct 2009
    Posts
    117
    You can try Box2D:

    http://box2dflash.sourceforge.net/

    You'll have to check its documentation, wiki and forum to learn how to use it.

    You should also have a look at these excellent tutorials:

    http://www.emanueleferonato.com/2009...ute-beginners/

    This is probably the best start. Just as a warning though.. it's not a trivial thing to learn so be prepared to put in quite a bit of shop time into learning how to use the API

    However... you want to make a platform game with physics?

    In my opinion you don't need to use a full blown physics engine like Box2D. It will be overkill. The physics code that you need for a platform game is minimal, so you should be able to implement it yourself. Plus, the time you put into learning how to do it will directly benefit your skill set. Basic physics is something that I think every game designer should know how to write from scratch. It's a very liberating thing to learn because it opens up all sorts of possibilities. I think it's better to invest the time into learning how to write your own code rather than learning how to use someone else's API.

    My advice is to start working on the game, and as soon as find you want to implement something specific (like slopes) and don't know how, just ask us here at the forum.


  3. #3
    Senior Member
    Join Date
    Dec 2009
    Posts
    109
    you don't really need a physics engine.
    nearly all your problems can be fixed by while statements and hitTests.
    Ex:
    while(ground.hitTest(player._x,player._y,true)){
    player._y-=0.1;
    }
    Syntax Error

  4. #4
    Senior Member x-death's Avatar
    Join Date
    Aug 2009
    Posts
    175
    Adamkyler:
    obviously you didn't read my post. simple hit tests aren't going to allow me to make a movieclip collide with any shaped surface i want them to collide with. i understand hittests at a basic level, i'm not completely stupid. not to be harsh but anyone that knows what there doing will tell you a simply hittest ain't going to replicate a physics engine like Box2D

    please read the entire post before posting further.

    dandylion13:
    thanks for the suggestion for what physics engine to use but tutorials don't explain anything in nearly enough detail as what i'd like them to. like for example i need to understand all files included with the pac and how to use them. well perhaps not all of them, but i need to understand what is needed to make things with the engine.

    tutorials simply say "do, this and do that. and this will be the result". it teachs me nothing, and in reuslt i get annoyed that i took the time to read it. thats why i'm asking for help.

    hopefully someone will come and help me.
    Last edited by x-death; 01-06-2010 at 12:56 AM.

  5. #5
    Senior Member
    Join Date
    Feb 2004
    Posts
    312
    Well in my opinion if you don't understand how to use 2dbox it will be much harder for you implement a physics engine.
    My suggestion: 1) Make a list on all physics your game should make use of. 2) do lots of re-search, understand the concepts and learn to implement them. 3) start making your game. 4) come back and let us know, maybe we can use your engine
    FYI, the process might take from 6 month to the rest of your life, depending how fast you are on grasping math and physics.

    The other alternative use hitTest() . Just Kidding...
    Good Luck.

  6. #6
    Senior Member x-death's Avatar
    Join Date
    Aug 2009
    Posts
    175
    why won't you people listen!

    i told you what i need help with. help doesn't mean rewrite everything i've said already.
    Last edited by tonypa; 01-06-2010 at 05:22 AM.

  7. #7
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    define geometry by simple shapes. ie: Lines, Circles. Create classes for these objects with necessary properties in each. i.e "p1, p2, x, y, radius"

    define player collision as a circle

    - loop over all geometry in the game (circles and lines)
    - narrow down possible collisions by doing simple geometric tests (test if two shapes bounding rectangles overlap)
    - once possible collisions are found run coarse collision detection / resolution
    - depending on the shape check:
    circle to lines - http://local.wasp.uwa.edu.au/~pbourk...ry/sphereline/
    circle to circle - http://local.wasp.uwa.edu.au/~pbourke/geometry/2circle/

    - once intersection data is found (collision normal and depth), resolve collision using physics. http://blog.generalrelativity.org/ac...ctionscript-3/
    - integrate using a simple Euler (vy += gravity, x += vx, y += vy)

    because gravity is constantly affecting the player, slopes will slow the player as collision resolution will be constantly running and using the normal of the slope. Amazing!

    the performance issue affecting physics engines is narrowing down collisions in the most efficent and fastest possible way. The physics itself can't be optimized much.

    If you want to know everything about the physics and collision then take your question to the Math forum here on FK or somewhere else on the world wide web.
    Last edited by mr_malee; 01-06-2010 at 03:09 AM.
    lather yourself up with soap - soap arcade

  8. #8
    Senior Member x-death's Avatar
    Join Date
    Aug 2009
    Posts
    175
    thanks for the reply ,

    so your suggesting i don't use a physics engine, i make my own vector based collision system...i read over the stuff you linked to. sadly i still don't understand how to implement this into anything.

    understanding the math is probably important, but right now i want to be able to implement, understanding the math is something i guess i will do as i'm going along.

    so anyone up ffor helping me figure this out?

  9. #9
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    do you understand classes, OOP?

    here's what a simple Line class might looks like
    Code:
    package {
    
    public class Line {
    
    public var p1:Point; //start point
    public var p2:Point; //end point
    
    public function Line(sx:Number, sy:Number, ex:Number, ey:Number) {
    
    p1 = new Point(sx, sy);
    p2 = new Point(ex, ey);
    }
    
    }
    }
    and a circle

    Code:
    package {
    
    public class Circle {
    
    public var x:Number;
    public var y:Number;
    public var radius:Number;
    
    public function Circle(x:Number, y:Number, radius:Number) {
    
    this.x = x;
    this.y = y;
    this.radius = radius;
    }
    
    }
    }
    to create these make new instances of them

    Code:
    var circle:Circle = new Circle(100, 100, 50); //a circle at 100,100 and radius of 50
    
    var line:Line = new Line(0, 0, 100, 100); //a line extending from 0,0 to 100,100
    now you create a player much the same

    Code:
    var player:Player = new Player(50, 50, 10); //a player at 50,50 and radius of 10
    now add objects to a collision list

    Code:
    var collidable:Array = [circle, line];
    now run a loop

    Code:
    addEventListener(Event.ENTER_FRAME, onMain);
    
    function onMain(event:Event):void {
    
    player.update(); //update player (integrate position and velocity)
    
    checkCollisions(collidable); //check all collisions
    
    //render objects when physics are done
    
    renderObject(player);
    renderObject(line);
    renderObject(circle);
    }
    in your collision function it might look something like:

    Code:
    function checkCollisions(collidable:Array) {
    
    var i:int;
    var len:int = collidable.length;
    var collision:Collision = new Collision(); //get a new collision object for storing collision information
    
    //loop over collidable and check collisions
    
    for (i = 0; i < len; i++) {
    
    var collidable:Object = collidable[i];
    
    collision.clear(); //reset collision so we can test it multiple times
    
    //check collidable type and act appropriately 
    
    if (collidable is Line) {
    
    collision = doLineCollision(collidable as Line);
    }
    else if (collidable is Circle) {
    
    collision = doCircleCollision(collidable as Circle);
    }
    
    //resolve collision if it hits
    
    if (collision.hits == true) {
    
    resolveCollision(collision);
    }
    
    }
    
    }
    collision class is very simple

    Code:
    package {
    
    public class Collision {
    
    public var hits:Boolean;
    public var normal:Point;
    public var depth:Number;
    
    public function Collision() {
    
    hits = false;
    normal = new Point();
    }
    
    public function clear():void {
    
    hits = false;
    normal.x = normal.y = 0;
    }
    
    }
    }
    now all you need to so is fill in the functions with the math side of things:

    Code:
    doLineCollision();
    doCircleCollision();
    resolveCollision();
    and you should be on your way to seeing a circle bounce on a line.

    *Might I add that I've just typed these straight in FK so the chances of them working are very low. And also you could optimize lots here but I won't go into that now.
    Last edited by mr_malee; 01-06-2010 at 04:51 AM.
    lather yourself up with soap - soap arcade

  10. #10
    Senior Member x-death's Avatar
    Join Date
    Aug 2009
    Posts
    175
    i put it together i think i did it right, might not have though. i understand classes at a basic level. i don't use them much. OOP i have read about it a little but i haven't used it to a great deal.

    hmmm...and just a heads up, i'm not using classes when i want to use this. the code will be on the main timeline.

  11. #11
    Senior Member
    Join Date
    Dec 2009
    Posts
    109
    you need multiple hitTests for every object.
    Syntax Error

  12. #12
    Senior Member x-death's Avatar
    Join Date
    Aug 2009
    Posts
    175
    look i'm not saying hitTests may not be envolved. but i'm looking to make a vector based collision system. and i need serious help doing so. so simply saying hitTest isn't going to help make that happen.

    so far you've show collissions on a very, very basic level. and i could this already. but that only works for squares. that isn't even close to what i asked.

  13. #13
    Senior Member
    Join Date
    Jan 2008
    Location
    UK
    Posts
    269
    The is quite amusing. The guy is obviously new to the whole physics thing, and people here are telling him to write his own physics engine, as if it's straight forward. It's not just about collision detection - it's about calculating the appropriate collision response when objects collide, and that really isn't going to be easy for someone trying to make their first platform game.

    My advice is very simple. Use box2d.

    1. Start by reading the box2d user manual.
    2. Take a look at the test bed examples it comes with.
    3. Make something VERY simple with it - using the examples as a starting point.
    4. When you get stuck - ask for help on a specific problem on the box2d forum.

    And DON'T just say 'can someone show me how to use box2d'. You need to do the reading yourself. People here and on the box2d forum will help you solve specific problems, but they aren't going to do the hard work for you in actually learning how to use it.

  14. #14
    Senior Member x-death's Avatar
    Join Date
    Aug 2009
    Posts
    175
    your right it isn't straight forward. its incredibly bloody hard.but you speak like i haven't tried to use engines like box2d, when i have. i actually tried using it once before not to long ago. i was trying to work out how the engine worked so i could hopefully make a motorbike game with it. didn't have any luck figuring out the engine after two straight weeks trying i asked for help then, i was ignored. no i have further uses for the engine so i'm giving it another go.

    i've read everything but in all honesty i didn't understand half of what it was telling me to do. and when i looked at the example to thry and understand what they were doing i didn't understand any of there code. and felt really lost and incredibly stupid at the same time.

    thats why i'm here. you tal like i want someone to do the work simply so i can steal it. but it isn't like that at all. if that werre the case i would have simply asked for an engine to be made. but i didn't i wanted someone to work with me and help teach me how to use a physics engine. that way when i can understand how to use it i'll be able to implement it into what ever i want with any luck.
    Last edited by x-death; 01-08-2010 at 07:52 AM.

  15. #15
    Senior Member
    Join Date
    Jan 2008
    Location
    UK
    Posts
    269
    I understand - when I learned box2d I was also completely lost to begin with.

    Unfortunately though nobody is going to be your personal tutor and guide you step by step through the process, and if that's what you were asking for on the box2d forum, that's probably why you got ignored. The closest you are going to get to a step by step lesson is by reading online tutorials like this one here - box2d for absolute beginners

    When your done with that one, google box2d tutorials to find more. Follow a few of them - then the next time you look through the box2d user manual it will make a lot more sense.

    Finally - try to keep things simple. Don't start with trying to make a full game, just try to get a few boxes bouncing around the screen first and then increase the complexity bit by bit. I have made several games using box2d and I still don't understand half of it - I just use the bits I need.

  16. #16
    Senior Member
    Join Date
    Dec 2009
    Posts
    109
    I understand that you want a vector bases collision system, it is simple to make a hit test for every vector you want to collide with other things, then make an object using the points as a template, then allow hit tests with that object as well. I have done this before, it works perfectly, considering my limited knowledge of physics. I have gotten it to work with a variety of shaped including: squares, triangles, circles, octagons, pentagons, and human shaped objects.
    Syntax Error

  17. #17
    Senior Member
    Join Date
    Oct 2009
    Posts
    117
    x-death, I think if you break your problems down into smaller steps and ask specific questions, you'll find this much easier, and many more people will be able to help you.

    You want to make platform game with a physics engine. My question is this: do you know how to make a platform game without physics? In not, start working on that first. If you do, could show it to us what you've done so far? We can then suggest how to help you with specific problems like jumping and platform collision detection.

  18. #18
    Senior Member x-death's Avatar
    Join Date
    Aug 2009
    Posts
    175
    i don't need help making the game side of it, i've made engines before or well ok ones anyway. far from perfect needs updating badly. ok is this simple enough for you all. how would i implement a vector based collision system into a platform game?

    i don't need it to be implemented into an actual game, i mean hay you could make it so when you click a paticular button an instance of the player is created an simply falls from were its clicked. that would even be enough to help teach me how to do vector based collisions.

    and i don't want it done for me, well not without you guys going through it with me so i'm understanding whats happening. i'd prefer if it was done slowly. what i mean by that is, don't do it all at once, maybe do collisions with paticular things first so i can understand that first.

    also this way you guys will know that in the end result i will not only understand this, but i'll also not have a game made for me. so i'll have to use the knowledge i've gained to make something for myself. or well thats the idea anyway.

    so can you help? i'm kind of getting sick of asking thisquestion over and over. so this is the last time i'm going to ask. if you guys need me to ask again simply refer to one of many posts i've posted already.

  19. #19
    Senior Member
    Join Date
    Dec 2009
    Posts
    109
    To make your guy fall, you need to set a gravity variable, theta variable, and a friction variable. onEnterFrame, you add gravity to theta/ divide theta by friction, then modify your characters y position by theta.

    gravity = 5;
    friction = 2;
    thetaY = 0;
    _root.onEnterFrame = function(){
    thetaY+=gravity;
    thetaY/=friction;
    guy._y+=thetaY
    }
    To make your guy stop when he hits the ground, you would use the code:
    while(ground.hitTest(guy._x,guy._y,true)){
    thetaY = 0;
    guy._y-=0.1;
    }
    combine the codes, and you get something like this:
    gravity = 5;//sets gravity of the world to five
    friction = 2;//sets friction of the world to two
    thetaY = 0;//initializes the thetaY so we can modify it later
    _root.onEnterFrame = function(){//ideally 30 times a second
    thetaY+=gravity;//gravity(five) is added to thetaY
    thetaY/=friction;//thetaY is divided by friction (two)
    guy._y+=thetaY;//adds thetaY to the guy's y position
    while(ground.hitTest(guy._x,guy._y,true)){//while guy is touching ground (works with any shape ground)
    thetaY = 0;//make the thetaY equal zero so it does not keep increasing and push him through the ground.
    guy._y-=0.1;//move the guy's y position up one tenth of a pixel. (so as to allow him to climb slopes without looking buggy)
    }//end the while statement
    }//end the onEnterFrame
    Is this what you are looking for?
    Syntax Error

  20. #20
    Senior Member Ray Beez's Avatar
    Join Date
    Jun 2000
    Posts
    2,793
    Adam, I think he's looking to do collisions against slopes of any angle, etc... there's some serious math required.

    X-Death: Can you share what you DO know and ARE capable of?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center