• 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            
            }

    ——————————————————————————————————
    傲轩游戏网
  • 相关阅读:
    记一个微信公众号个性化菜单的坑
    初学c# -- 学习笔记(六) winfrom组件圆角
    Swift中添加标记(MARK)和警告(TODO FIXME)
    iOS Xib布局某些控件显示或隐藏<约束的修改>
    [!] Unable to satisfy the following requirements:
    React Native使用NetInfo对当前系统网络的判断
    iOS自定义下拉列表
    [!] The version of CocoaPods used to generate the lockfile (1.4.0.beta.1) is higher than the version of the current executable (1.3.0.beta.1). Incompatibility issues may arise.
    iOS实现TextField光标居中
    iOS优秀第三方框架
  • 原文地址:https://www.cnblogs.com/cuizhf/p/2390359.html
Copyright © 2020-2023  润新知