博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Simple tutorial to phys2d. version 0.0.2
阅读量:7229 次
发布时间:2019-06-29

本文共 2931 字,大约阅读时间需要 9 分钟。

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

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

Labels

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

Introduction

Homepage:

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

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                     }

 

 

转载地址:http://vebfm.baihongyu.com/

你可能感兴趣的文章
对象实例化、字符串的使用方法
查看>>
keepalived基于LVS实现高可用,实现web服务的高可用
查看>>
80端口被Microsoft-HTTPAPI/2.0占用的解决办法
查看>>
无法抗拒Minecraft给予超高的自由度和探索-微访谈
查看>>
数据结构之串
查看>>
我的友情链接
查看>>
lvs+keepalived+nginx+tomcat高可用高性能集群部署
查看>>
实验:搭建主DNS服务器
查看>>
org.gjt.mm.mysql.Driver与com.mysql.jdbc.Driver区别
查看>>
部署exchange2010三合一:之五:功能测试
查看>>
nginx编译安装参数
查看>>
代码托管
查看>>
第一次给ThinkPHP5核心框架提pull request的完整过程
查看>>
U-Mail邮件系统何以誉为信息整合中转枢纽
查看>>
强大的vim配置文件,让编程更随意
查看>>
崛起于Springboot2.X之配置文件详解(10)
查看>>
定时执行程序-Quartz简单实例
查看>>
【CF 应用开发大赛】MyfCMS系统
查看>>
windows下kangle虚拟主机-架设java空间的教程及心得
查看>>
Discuz! X2.5:文件目录结构
查看>>