zoukankan      html  css  js  c++  java
  • Simple tutorial to phys2d. version 0.0.2

    http://code.google.com/p/cyber2d/wiki/p2dTutorial

    ————————————————————————————————————————————————————————

    Labels

    • javaSE 1.4
    • 2d
    • engine
    • collisions
    • forces

    Introduction

    Homepage: http://www.cokeandcode.com/phys2d/

    Try there demo box! It is obvious than this tutorial.

    phys2d is physical engine. It means that it take care about collisisns and movment and rotation of objects, and many more.

    Main thing you need to care about is to:

    1. set objects
    2. add forces, so objects can move
    3. handle collisions (quite simple, see below)

    ATTENTION! phys2d don't have methods to draw, so you need to draw objects yourself. Or use next code (only for rectangles!):

                    for (int i=0;i<bodies.size();i++) {
                            Body body = bodies.get(i);
                            Box box = (Box) body.getShape();
                            Vector2f[] pts = box.getPoints(body.getPosition(), body.getRotation());
                           
                            Vector2f v1 = pts[0];
                            Vector2f v2 = pts[1];
                            Vector2f v3 = pts[2];
                            Vector2f v4 = pts[3];
                           
                            g.setColor(Color.black);
                            g.drawLine((int) v1.x,(int) v1.y,(int) v2.x,(int) v2.y);
                            g.drawLine((int) v2.x,(int) v2.y,(int) v3.x,(int) v3.y);
                            g.drawLine((int) v3.x,(int) v3.y,(int) v4.x,(int) v4.y);
                            g.drawLine((int) v4.x,(int) v4.y,(int) v1.x,(int) v1.y);
                    }

    Step-by-step

    1. add phys2d.jar to your project
    2. add World object
    3. configure World
    4. create Body
    5. configure Body
    6. add Body
    7. repeat 2 last steps for more bodies

    Properties

    I'll put simple code that show example how it can be used.

    Vector

     

    So getX() equal width, and getY() - height.

    World

    Object that hold everything and execute everything inside it. Every coordinate inside it is in float.

    • gravity - direction (and power) where bodies will fall
      • hint 1 - can be changed during play
      • hint 2 - can be set to any direction (e.g. objects will not fall, but move to upper left corner)
      • hint 3 - if (0, 10) then like at Earth; if (0, 0) - like in space/top view games
      • world = new World(new Vector2f(0.0f, 10.0f), 10, new QuadSpaceStrategy(20,5));
    • add(Body) - you will use it to add other objects
    • step() - one step, that move/collides bodies

    Shapes

    Every object in World must have shape. You can create own shape or use premaded shapes:

    • Box (probably you need this one :)
    • Circle
    • Line
    • Polygon

    Body

    Bodies is objects in World, so everything will happened to them. Also it is possible to create reference to object that represents this body in game (setUserObject())

    Bodies can be static or not. Static just means that body will not move (for faster processing).

    • `new Body("player", new Box(20, 40), 100);
    • shape - geom representation of body (usually used for collisions)
    • mass - mass of body (the more mass, the more force need to move it)
    • addForce (force that will apply for body)
      • hint 1 - for moving side small force should be applied continually (e.g. mass 100, force shuold be (300,0))
      • hint 2 - for jumping one-time force should be applied (e.g. mass 100, force shuold be (0,-200000)) NOTE: force is so big, but it is right.
    • create link to non-World object
      • `body.setUserData(player);'
    • position - you can adjust position of object any time in game (or just at the beggining)

    Collisions

    When World processing step(), CollisionEvent can occure one or several times. CollisionEvent have coordinate and reference to bodies that collide.

    You can add listener to catch CollisionEvents: world.addListener(this); and then

            public void collisionOccured(CollisionEvent event)
            {
                    ceList.add(event); //for example collect all collision events, so later you can handle them            
            }

    ——————————————————————————————————
    傲轩游戏网
  • 相关阅读:
    梦想就是梦想,不要让它成为杯具
    程序员,离开了库你还能干什么?
    采用WPF框架编写的3D软渲染Demo
    what the hell with Gimbal Lock?
    FX Composer VS RenderMonkey 使用对比之 FX Composer篇
    为什么你应该使用OpenGL而不是DirectX?
    游戏中的夜视效果实现
    {转}深入浅出之正则表达式(一)
    正则表达式30分钟入门教程版本:v2.31 (2009411) 作者:deerchao 转载请注明来源
    2013年
  • 原文地址:https://www.cnblogs.com/cuizhf/p/2390359.html
Copyright © 2011-2022 走看看