Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Physics of 2d Space

  1. #1

    Physics of 2d Space

    Hello!

    As a fan of 2d space games, the type where you fly around and visit planets or blast aliens from another galaxy, I am currently working on my own project. However, actual game control is a problem:

    I am using a coordinate system for space (x,y), and I want the ship to be able to rotate with the left and right arrow key, thrust with the foward key, and anti-thrust (break) with the back key.

    I was wondering, what formulars do I need to use in order to simulate this? And what options can I build inside to make control more realistic?

    I would appreciate any help or suggestions.

    splatty

  2. #2
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Physics of 2d Space

    Well you can never have enough 2D space games. Heck, I've been writting one for a while now. First thing I'd recommend is 'boning up' on your physics. Understanding the basic fundimentals behind motion will save you a world of hurting later on(trust me!).

    In general, movement in space is almost the same as movment here on earth, save the amount of pull of gravity. There is some gravitational pull in space(depending on the size and distance of objects around you), but it is so fractional it's practiacly zero-Gs. Basically if you can lob a cannon ball in a game like Scorched Earth or Worms, using code then you can make a ship float around in space... just add some kind of inertia...

    Inertia is basically the momentum of an object. An object in space that is not disturbed will continue to move on inertia until something interupts it's movement. ie. An alien laser or the ships own rocket engines... Notice how when you see an astronaught/cosmonaught floating around in the ISS he doesn't really move until he/she pushes him/herself off a part of the equipment? Thats because of one of Newton's Laws of Motion.


    Here is a site that has all kinds of Physics information and even a few equations: www.physicsclassroom.com

    It, however is a bit detailed...


    Hope this helps a bit. I'll see if I can find something a bit more staight-forward. If not I'll just cut and paste a few bits from my own code. Not as helpful for understanding in though.

    Best of luck!
    Jason McMillen
    Pascal Game Development
    Co-Founder





  3. #3

    Physics of 2d Space

    Thanks for the Link :-)

    I am currently digged through the vector calculations on that page for some calculations, and started a very basic system for flight (for test purposes), which I will use before I use "real-universe" physics.

    This is what I have worked out:

    Code:
    Player.x := Player.x + (cos(angle) * speed/5);
    Player.y := Player.y + -(sin(angle) * speed/5);
    But then again, I've thought about using a velocity.x and a velocity.y to go the "entire hog"... but I am not quite sure where to start. Well, I guess I have to keep on trying - eventually something will work out :-)

    splatty

  4. #4
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Physics of 2d Space

    I found my old cheat-sheet from when I started my current project about a year or so ago...

    <u>Basic 2D Math Functions</u>

    Find Velocity:
    [pascal]VelX := power * cos(Pi * AngleInRadians);
    VelY := power * sin(Pi * AngleInRadians);[/pascal]

    Find Distance Between Two Points:
    [pascal]Distance := sqrt(sqr(x2 - x1) + sqr(y2 - y1));[/pascal]

    Find Rotation:
    [pascal]NewX := cos(AngleInRadians) * OldX - sin(AngleInRadians) * OldY;
    NewY := sin(AngleInRadians) * OldX + cos(AngleInRadians) * OldY;[/pascal]

    Find Angle From One Point To Another:
    [pascal]Angle := ArcCos((x2 - x1) / Distance);[/pascal]


    You should be able to do quite a bit with these. You should however consider optimizing them a bit. Functions like sqrt and ArcCos cost quite a bit of cpu speed, but at least you can substitute something like [pascal]z := sqr(x);[/pascal] with [pascal]z := x * x;[/pascal]


    I hope this is generally useful.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  5. #5
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Physics of 2d Space

    Also for optimisation create an array with all the Cos/Sin etc of each angle. Then instead of calling a function to calculate the value just access it directly from the array.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  6. #6

    Physics of 2d Space

    what about making a flying enemy??
    It should follow a certain path in the sky (it can be aplied to space and also to earth). How could I do this? also this path should change in response to certain events such as palyer movement or scroll, etc..

    thanks in advance!

  7. #7
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Physics of 2d Space

    Quote Originally Posted by patroclus02
    what about making a flying enemy??
    It should follow a certain path in the sky (it can be aplied to space and also to earth). How could I do this? also this path should change in response to certain events such as palyer movement or scroll, etc..
    You're entering a whole new ball-game now. Something like this isn't just a simple equation. Its more or less a algorithm or more, depenging on how complex this action gets. You can do this in many ways, such as; Have a set of "way-points" and either have it find it's way by steering from some AI or have little flight commands along a timeline... Depending on the type of game and type of AI you are using there are all sorts of different ways to do this. But whatever you do decide on you should establish your system of movement well before you move on to complexities. It'll save you alot of trouble later on. Ask any veteran here, they'll tell you the same.

    Perhaps someone here could show you a system that they have used or came up with in some scenario or help you with one of your own, but they'd have to narrow down the type of perspective(side-view, birds-eye-view) or game physics envolved(2D/3D, but in this case 2D).
    Jason McMillen
    Pascal Game Development
    Co-Founder





  8. #8

    Physics of 2d Space

    well, my flying enemies are quite simple.
    It is for a 2d platforms game style, and I'm talking about enemies that just fly around with no colisions with walls or anything. I just want it to fly in a way you don't see any straigth movement (I mean, not in lines, but in circular motion when its direction changes).

    I did a very simple test telling the enemy to go go in direction of main character, and only change direction if it passed the main character by more than 100 pixels, for example, following straight lines in X and Y, but with acceleration in both axes. this way it looks like cirular motion when it changes direction!! Too easy, but too good also,

  9. #9
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Physics of 2d Space

    Hmm... I think you are getting into AI here. That is a much different topic from the actual game physics. Getting to make the ships move in a "smarter" way rests soley on the game AI though you still want to let the physics do the work of moving the ship. AI has a limited effect on the game physics through some control variables such as speed, angle, etc.

    Example 1: Steering the ship.

    :arrow: You would change the angle of the ship by a set turn_rate for that ship either left or right.

    Example 2: Increasing/Decreasing speed of the ship.

    :arrow: Here you would change the speed of the ship either buy percentage, set amount or by incremental amounts that allow you to gradually change it.


    It is usually best to keep these to things seperate in your code. Have a method or function for each if possible. This way, once you have created your physics routeens you can just leave them alone and you won't even have to worry about them. However it looks like you want to know more about AI. Perhaps if you post some of your questions on our AI forums someone can help you with those. I can list serveral methods for you to try and there are a few that others can suggest aswell.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  10. #10

    Physics of 2d Space

    Quote Originally Posted by splattergnome
    Thanks for the ]Player.x &#58;= Player.x + &#40;cos&#40;angle&#41; * speed/5&#41;;
    Player.y &#58;= Player.y + -&#40;sin&#40;angle&#41; * speed/5&#41;;[/code]
    can somone please post some example code which uses this ? for example just plot a pixel so that when u turn left it turn 1 degree . I've been trying it for so long but i just suck at maths and never managed to get it working right.

    thanks

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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