Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Phys2D usage questions:  (Read 3349 times)
0 Members and 1 Guest are viewing this topic.
Offline Nod51

Junior Newbie





« Posted 2007-10-01 19:00:08 »

This is my farthest attempt into making a playable game. I have played around with some C++ engine like Orger3D, and have the basics of a game with a server, but I have no 3D talent, so I decided to try Java and 2D game. I have yet to finish and release a game so I am sorry if my questions are obvious or just common knowledge. I have spent at least 1 hour trying to solve the following questions. This is also my first time playing with a physics engine.

I am looking to make some top down 2d game, not sure what I want to make but I have some things I know will need to be done. I currently have the world gravity set to 0, so things just 'float' around.

1) I have 'space ship' movement easy with setDamping(0f), 'water' simulation with setDamping(1f) (with my mass of 200) and then addForce(new Vector2f(0,-10000)) every loop. I would like to know the proper way to do person movement. I tried:

(delta = time since last loop)
float distance = (delta * 100) / 1000;
body.move(getX(), getY()+(-1f*distance));

this works fine except I will get a second lag (garbage collection?) and go right over my collision wall. Should I just make sure the movement isn't over some amount (those with slower computers are slower in game)? I could do more stepping (does it work with move?) but that just adds more jerking in the end. I doubt "creating a line the length of my movement and checking it for collision, if no collision then move" would be a good idea...

2) I was wondering how I could do per pixel collision. In the end I may have a destroyable terrain (think worms).
I notice there is an addBitMap(), setBitMap() ... options but not sure how to use them or if I can even use them like that.
If I can't use bitmask setting, I was thinking of making a collision object the same shape as my image my using lines, like in the gear demo (though gears demo used a formula). I was going to check the image and find the 'outside' points and feed the points into a collision object.
Another way is just do circle/square collision, and if collision detected, do my own per pixel doing bitmask compare (examples on net). Problem is I do not think I can use the Phys2D 'world', and would have to manage the collision space myself, or is there a way to get around this?

3) I might want to have tank(s) or car(s). Water isn't bad because it does not have ridged turning but 'slides'. I would like friction on the bottom, not just against other objects (or can I use a collision object to do this?). I could simulate sliding when the energy, other then the direction I am facing, gets to some point, I apply more energy or something... If I made a side scroller it woudl be cool because I could just attach tires to body and then turn the tire.

4)  Is world.getContacts(body); function thread safe? I was going to complicate things with threads and make things more interesting.

5) If I make a space shooter I would like planets to have their 'own' gravity. I thought I read a comment on this and the solution was to attach springs to every object in range that got stronger the closer they were. I was thinking if there is no built in 'world' way to give object gravity, I would just have to check every body and if it is close to a gravity object, apply force based on distance.

Thats it for now and I hope I can still use Phys2D to do some of these. Granted I have no fixed idea what I will be making so I may make a game to fit the requirements than the other way around.
Online kevglass

JGO Kernel


Medals: 167
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #1 - Posted 2007-10-01 19:23:42 »

1) You should be updating the world, not attempting to move bodies independently. if you've applied force then the world update will cause the movement (and make other things respond). There may be GC issues to discover, but more complicated sims have been written already without the issue showing.

2) There's no per pixel collision in phys2d, infact there's no concept of a collision. The bit masks are used to define groups of bodies that can collide.

3) Friction can be applied to any body - the "floor" would be a body - so the cars/tanks would slide against it.

4) Phys2D is in no way thread safe. It's all intended to be run in a single thread by calling world.update()

5) There's no way to add gravity production to bodies. Springs would also look very strange. Your approach sounds more appropriate.

Kev

Offline Nod51

Junior Newbie





« Reply #2 - Posted 2007-10-01 21:32:50 »

Thank you for the quick replay.

1) After posting I thought of something, I swear that submit button raises my IQ to like 200 for 1 second (happens all the time, sorry)
setDamping(1f);
addForce(new Vector2f(0,-10000));
to
body.setDamping(100f);
body.addForce(new Vector2f(0,-1000000));

now it moves 'like' a person, I will lower those values till I find a good one.

2) darn, so if I ever plan to make a worms type game I will make up my own collision library I guess. So what I call collision is: world.getContacts(this.body) , where one detects is body A has 'touched' body B.

3) Since it is top down, the floor would be below my object, how can I have it apply the friction but not bounce off the 'walls' of the object. Say I have a "carboat" that drives 50x  on land and has no sliding left/right, but 30x on water and slides all over. Also does Phys2D have a concept of front back back? if someone hits it on the side it should not slide much, as opposed to hitting the front and back.

4) Thanks, I can easly thread sounds and GUI stuff...

5) now I just need to make a "get list of objects within circle range" function.

To me it seems Phys2d is more geared toward a side scroller than a overhead game, but not impossible. Perhaps a clone of http://xmoto.tuxfamily.org/ in Java, multi player, and car(s) might be fun...
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online kevglass

JGO Kernel


Medals: 167
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #3 - Posted 2007-10-02 08:00:19 »

Quote
To me it seems Phys2d is more geared toward a side scroller than a overhead game, but not impossible. Perhaps a clone of http://xmoto.tuxfamily.org/ in Java, multi player, and car(s) might be fun...

Thats dead right. A top down game implies 3 dimensions really, given the floor is in at a different z to the players/units. Phys2D is strictly a 2D engine. For interest, the original source of Phys2D has been released as a C++ engine (Box2D) which is in the process of being ported by another group. It may be more able to deal with your use cases - though I haven't seen any info on the project as yet.

Kev

Offline Nod51

Junior Newbie





« Reply #4 - Posted 2007-10-03 01:00:15 »

Good point on the top down being really 3d, never really thought of it in that way.

Well now that I know what Phys2d was meant to do, I can use it for a future side scrolling shooter, but use a 3d library or roll my own (so many examples on net) for a Zelda/FF/Diablo/NWN type game.

I thought that Phys2D was an implementation of Box2D and not the other way around.  I saw http://www.box2d.org/forum/viewtopic.php?f=5&t=36 earlier, but just thought it was someone with time on their hands reinventing Phys2D in a different way. I will go look at what Box2D has over Phys2D

I wanted to thank you for making a great physics engine. I find it easy to use as long as I don't try to use it for something it was not made to do. Right now I am still playing around with lwjgl, logl, and java2d rendering. If I decided on a game that can use Phys2D I will look forward to using it.
Online kevglass

JGO Kernel


Medals: 167
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #5 - Posted 2007-10-03 08:43:38 »

Thats what I meant btw, the original source of Phys2D - rather than the original src. To be clear, Phys2D is based on the original Box2D demo by Erin.

Kev

Offline ewjordan

Junior Member





« Reply #6 - Posted 2007-10-06 13:30:47 »

Hi there - I'm one of the people working on the new Java port of (the new) Box2D.  quixote_arg is working on it, too, and for now it's just the two of us.

Just to clarify, Phys2d is a port/reworking of the older Box2d engine, which is now being referred to as Box2D Lite.  The Lite engine was basically a proof of concept/educational project (it was missing a lot of features), so Erin Catto finally decided to throw together a real full featured and actively maintained engine in C++.  My only introduction to the old Box2d was Phys2d, and I was very impressed, so I figured that it was worth some time to getting the new one working in Java.

I'm not exactly sure of the feature differences between the two - I know the new one has polygons and circles, as well as all types of joints, and does friction, world gravity, and that type of stuff.  I'm not sure how much of this was in the old one before the reworking into Phys2d, I've heard people say that the Lite version had no polygons.  One of the things I'm _really_ looking forward to is that Erin has promised to add continuous collision detection to the engine - this means no more tunneling, assuming it works.  But I'm pretty sure this one doesn't have anything that would make doing an overhead driving thing work any easier, though, and I don't think there are inter-body gravity forces.  These are certainly things you could code up yourself, though, but you could probably do it just as easy with Phys2d, which is a lot more stable, especially right now.

As far as speed, the new Box2d has been highly optimized in the C++ version, and should run a good deal faster than the Lite one; however, we're still completely unsure how these gains will translate to Java, since a lot of the C++ optimizations have to do with memory handling.  Once we're all set, it might be worth comparing it to Phys2d to see if the algorithms are actually faster or if most of the improvement gets lost in the porting.

In any case, our port is now about 95% working, but we've still got to iron out some bugs, update it to sync up with the newest C++ code (Erin works FAST), and optimize it a bit for Java.  I'm guessing we'll do a first "release" once we've updated it, but before the optimizations.  The project lives at http://sourceforge.net/projects/jbox2d and the source is all there now, but beware, it's changing quickly.  I'll send a message over here when it's done so you guys can check it out.
Online kevglass

JGO Kernel


Medals: 167
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #7 - Posted 2007-10-06 13:51:32 »

Great stuff, really looking forward to the java version of the new Box2D! Continuous collision - really!? Awesome stuff! Smiley

Can't wait!

Kev

Offline quixote_arg

Junior Member


Projects: 1


Jengibre


« Reply #8 - Posted 2007-10-06 16:33:48 »

Yes, I want to see it too! Hurry up you guys! Tongue
Offline micxiao

Senior Newbie





« Reply #9 - Posted 2007-10-19 08:17:35 »

im trying to use phys2D on a 3D rendered game... hope it works

edit: seems to work fine, but since im rendering the 3D images, i can't see the physics circles and boxes. makes it abit hard to sync the 3D stuffs and the "2D bounding box/circle"
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Bonbon-Chan

JGO Coder


Medals: 12



« Reply #10 - Posted 2007-10-19 09:21:30 »

When i debug my Phys2D stuff with LWJGL, i draw the 3D. Then i desable the depth buffer and i draw the physic object from the world data  (showWorld) :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
  protected void showBody(Body body)
  {
    if (body.getShape() instanceof Box)
    {
//         drawBoxBody(g,body,(Box) body.getShape());
   }
    if (body.getShape() instanceof Circle)
    {
   drawCircleBody(body,(Circle) body.getShape());
    }
    if (body.getShape() instanceof Line)
    {
      drawLineBody(body,(Line) body.getShape());
    }
    if (body.getShape() instanceof Polygon)
    {
//   drawPolygonBody(g,body,(Polygon) body.getShape());
   }
  }
 
  protected void drawCircleBody(Body body, Circle circle)
  {
    GL11.glColor3f(1.0f,1.0f,1.0f);

    float x = body.getPosition().getX();
    float y = body.getPosition().getY();
    float r = circle.getRadius();
    float rot = body.getRotation();
    float xo = (float) (Math.cos(rot) * r);
    float yo = (float) (Math.sin(rot) * r);
     
    //g.drawOval((int) (x-r),(int) (y-r),(int) (r*2),(int) (r*2));
   //g.drawLine((int) x,(int) y,(int) (x+xo),(int) (y+yo));
   
    GL11.glBegin(GL11.GL_LINES);
          GL11.glVertex3f(x+r,y,0.0f);
          for(int i=1;i<8;i++)
          {
            GL11.glVertex3f(x+r*(float)Math.cos(i*Math.PI/4),y+r*(float)Math.sin(i*Math.PI/4),0.0f);
            GL11.glVertex3f(x+r*(float)Math.cos(i*Math.PI/4),y+r*(float)Math.sin(i*Math.PI/4),0.0f);
          }
          GL11.glVertex3f(x+r,y,0.0f);
   
          GL11.glVertex3f(x,y,0.0f);
          GL11.glVertex3f(x+xo,y+yo,0.0f);
    GL11.glEnd();
   
    GL11.glColor3f(1.0f,1.0f,0.0f);
   
   /* float fx = body.getVelocity().getX()/10.0f;
    float fy = body.getVelocity().getY()/10.0f;
   
    System.out.println(fx+"-"+fy);
   
    GL11.glBegin(GL11.GL_LINES);
          GL11.glVertex3f(x,y,0.0f);
          GL11.glVertex3f(x+fx,y+fy,0.0f);
    GL11.glEnd();*/

  }
 
  protected void drawLineBody(Body body, Line line)
  {
    GL11.glColor3f(1.0f,1.0f,1.0f);

    Vector2f[] verts = line.getVertices(body.getPosition(), body.getRotation());
   
    GL11.glBegin(GL11.GL_LINES);
          GL11.glVertex3f(verts[0].getX(),verts[0].getY(),0.0f);
          GL11.glVertex3f(verts[1].getX(),verts[1].getY(),0.0f);
    GL11.glEnd();
  }
 
  protected void showJoint(Joint j)
  {
    if (j instanceof FixedJoint)
    {
   FixedJoint joint = (FixedJoint) j;
         
        GL11.glColor3f(1.0f,0.0f,0.0f);
   float x1 = joint.getBody1().getPosition().getX();
   float x2 = joint.getBody2().getPosition().getX();
   float y1 = joint.getBody1().getPosition().getY();
   float y2 = joint.getBody2().getPosition().getY();
     
        GL11.glBegin(GL11.GL_LINES);
          GL11.glVertex3f(x1,y1,0.0f);
          GL11.glVertex3f(x2,y2,0.0f);
        GL11.glEnd();
     }
/*      if (j instanceof BasicJoint) {
         BasicJoint joint = (BasicJoint) j;
         
         Body b1 = joint.getBody1();
         Body b2 = joint.getBody2();
   
         Matrix2f R1 = new Matrix2f(b1.getRotation());
         Matrix2f R2 = new Matrix2f(b2.getRotation());
   
         ROVector2f x1 = b1.getPosition();
         Vector2f p1 = MathUtil.mul(R1,joint.getLocalAnchor1());
         p1.add(x1);
   
         ROVector2f x2 = b2.getPosition();
         Vector2f p2 = MathUtil.mul(R2,joint.getLocalAnchor2());
         p2.add(x2);
   
         g.setColor(Color.red);
         g.drawLine((int) x1.getX(), (int) x1.getY(), (int) p1.x, (int) p1.y);
         g.drawLine((int) p1.x, (int) p1.y, (int) x2.getX(), (int) x2.getY());
         g.drawLine((int) x2.getX(), (int) x2.getY(), (int) p2.x, (int) p2.y);
         g.drawLine((int) p2.x, (int) p2.y, (int) x1.getX(), (int) x1.getY());
      }
      if (j instanceof SpringJoint) {
         SpringJoint joint = (SpringJoint) j;
         
         Body b1 = joint.getBody1();
         Body b2 = joint.getBody2();
   
         Matrix2f R1 = new Matrix2f(b1.getRotation());
         Matrix2f R2 = new Matrix2f(b2.getRotation());
   
         ROVector2f x1 = b1.getPosition();
         Vector2f p1 = MathUtil.mul(R1,joint.getLocalAnchor1());
         p1.add(x1);
   
         ROVector2f x2 = b2.getPosition();
         Vector2f p2 = MathUtil.mul(R2,joint.getLocalAnchor2());
         p2.add(x2);
         
         g.setColor(Color.red);
         g.drawLine((int) x1.getX(), (int) x1.getY(), (int) p1.x, (int) p1.y);
         g.drawLine((int) p1.x, (int) p1.y, (int) p2.getX(), (int) p2.getY());
         g.drawLine((int) p2.getX(), (int) p2.getY(), (int) x2.getX(), (int) x2.getY());
      }*/

  }
 
  protected void drawContact(Contact contact)
  {
    int x = (int) contact.getPosition().getX();
    int y = (int) contact.getPosition().getY();
   
    GL11.glColor3f(0.0f,0.4f,1.0f);
    GL11.glBegin(GL11.GL_QUADS);
      GL11.glVertex3f(x+0.5f,y+0.5f,0.0f);
      GL11.glVertex3f(x-0.5f,y+0.5f,0.0f);
      GL11.glVertex3f(x-0.5f,y-0.5f,0.0f);
      GL11.glVertex3f(x+0.5f,y-0.5f,0.0f);
    GL11.glEnd();
   
    //g.setColor(Color.blue);
   //g.fillOval(x-3,y-3,6,6);
   
     int dx = (int) (contact.getNormal().getX() * 10);
     int dy = (int) (contact.getNormal().getY() * 10);
    // g.setColor(Color.darkGray);
   // g.drawLine(x,y,x+dx,y+dy);
 }
         
  protected void showWorld()
  {
    GL11.glDisable(GL11.GL_BLEND);
    GL11.glDisable(GL11.GL_TEXTURE_2D);
     
    BodyList bodies = world.getBodies();
     
    for (int i=0;i<bodies.size();i++)
    {
      Body body = bodies.get(i);
      showBody(body);
    }
     
    JointList joints = world.getJoints();
    for (int i=0;i<joints.size();i++)
    {
   Joint joint = joints.get(i);
   showJoint(joint);
    }
     
    ArbiterList arbs = world.getArbiters();
    for (int i=0;i<arbs.size();i++)
    {
      Arbiter arb = arbs.get(i);
 
      Contact[] contacts = arb.getContacts();
      int numContacts = arb.getNumContacts();
         
      for (int j=0;j<numContacts;j++)
      {
   drawContact(contacts[j]);
      }
    }
  }


Well don't take it like that but you get the idea.
Offline micxiao

Senior Newbie





« Reply #11 - Posted 2007-10-22 08:33:14 »

hi guys can anyone help me with something?

how do i reset the physics? for example, an object in mid-fall will start to fall from rest once more.

coz i wan to make a reset button, but the thing is the physics calculation does not get reset! it's funny but not working as intended. i didn't see anything of use in the documentation so any help here would be greatly appreciated

edit: hmm i created a new instances of body and re-added them to the world... seems to work as intended now~
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (37 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (60 views)
2014-09-21 02:42:18

BurntPizza (30 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (52 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!