Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411667 Posts in 69397 Topics- by 58452 Members - Latest Member: homina

May 16, 2024, 09:13:48 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Physics and Java. Argh.
Pages: [1] 2
Print
Author Topic: Physics and Java. Argh.  (Read 8641 times)
Hayato
Level 0
**


View Profile
« on: February 04, 2010, 09:16:37 PM »

Hey all.

I recently discovered the existence of physics libraries, and wish to try to implement them in my future games. I am very familiar with java, and the game is written in java. I was looking for some physics engines. I found JBox2D and Phys2D. However, I'm having trouble with both.

Phys2D:
there are pretty much no tutorials for this thing. To figure it out, you need to sift through mountains of source code. The demos help some, but not enough for me to actually do anything with this library. I've searched online for a few days now, and I'm getting pretty desperate. I'd rather use this than JBox2D, because it was originally written for Java.

JBox2D:
It's written for c++, and it shows. Any tutorials for it pretty much refer to the c++ version, and tell you to figure out the differences. I don't have that kind of time.

If anyone could refer me to a useful tutorial or some good documentation, I would be very grateful. I really want to finish this game, and not have it die somewhere in my files like all the other ones. This physics thing has been a major roadblock, so any help is appreciated.

Thanks.
Logged

The enemy's gate is down.
Lon
Level 4
****



View Profile
« Reply #1 on: February 04, 2010, 09:41:47 PM »

I have had experience writing programs in Java, but no experiance with Phys2D libraries. 
Here is a javadoc for it, hope it helps, but I image you have already seen it.
Logged

“We all sorely complain of the shortness of time, and yet have much more than we know what to do with. Our lives are either spent in doing nothing at all, or in doing nothing to the purpose, or in doing nothing that we ought to do..." -Seneca
Glaiel-Gamer
Guest
« Reply #2 on: February 04, 2010, 11:57:04 PM »

there's a ton of tutorials for the flash port of box2D out there though, and flash is more similar to java than c++ is


also, why java?
Logged
Alex May
...is probably drunk right now.
Level 10
*


hen hao wan


View Profile WWW
« Reply #3 on: February 05, 2010, 01:33:12 AM »

That's a good point, checking out the ActionScript Box2D stuff might be a big help. Maybe if you have specific problems people on here could help.

also, why java?

No need to ask this.
Logged

Eclipse
Level 10
*****


0xDEADC0DE


View Profile WWW
« Reply #4 on: February 05, 2010, 02:27:49 AM »

also, why java?

*
Logged

<Powergloved_Andy> I once fapped to Dora the Explorer
Chromanoid
Level 10
*****



View Profile
« Reply #5 on: February 05, 2010, 02:46:09 AM »

You might try http://jbullet.advel.cz/ jbullet a very javaish port of bullet, but i don't know about 2D capabilities.

if i were you i would use jbox2d it is very easy to use. there is not much to learn... i used only the as3 box2d port. in the as3 port every class begins with b2 and function names start with a capital letter - how ugly... after looking at the sources of jbox2d i think it is a good port. the leading m_ for members etc. isn't the latest java coding style but the rest seems to be very javaish.
you can learn box2d with the sample sources easily. just look at testbed.PTest and
testbed.tests.PolyShapes. In AS3 you have a nice DebugDrawer - this seems to be not the case with jbox2d so you should write your own one (most of the code in PTest is a debug renderer for processing). I don't understand why they didn't implement a simple Swing Debug Renderer... btw the latest examples in the jbox2d repository packed most of the rendering stuff into an new class called ProcessingDebugDraw - so it should be even easier to understand the Test-Classes (new in repository: testbed.TestbedMain, testbed.AbstractExample and i.e. testbed.tests.Circles)...

Logged
Chromanoid
Level 10
*****



View Profile
« Reply #6 on: February 05, 2010, 02:50:39 AM »

also, why java?
java is great! i am fed up with discussions about that. you may look at jMonkeyEngine.com or especially at Interview with Christian, maker of Grappling Hook (he also had some problems with physics in java). some other pro java examples might be: http://www.runescape.com http://funorb.com http://www.java4k.com

ps: we all should agree that programming games in the language we can already handle is not a bad thing and that many languages work for game programming.
« Last Edit: February 05, 2010, 03:04:54 AM by Chromanoid » Logged
Alex May
...is probably drunk right now.
Level 10
*


hen hao wan


View Profile WWW
« Reply #7 on: February 05, 2010, 04:11:11 AM »

ps: we all should agree that programming games in the language we can already handle is not a bad thing and that many languages work for game programming.

Please can we put this in bold in a sticky thread at the top of the technical forum?
Logged

Hayato
Level 0
**


View Profile
« Reply #8 on: February 05, 2010, 12:25:47 PM »

I have had experience writing programs in Java, but no experiance with Phys2D libraries. 
Here is a javadoc for it, hope it helps, but I image you have already seen it.

I've seen the javadoc. It helps some, but not very much. thanks, though.

there's a ton of tutorials for the flash port of box2D out there though, and flash is more similar to java than c++ is


also, why java?

Java because it's my favorite language, and the one I'm the most familiar with.

You might try http://jbullet.advel.cz/ jbullet a very javaish port of bullet, but i don't know about 2D capabilities.

if i were you i would use jbox2d it is very easy to use. there is not much to learn... i used only the as3 box2d port. in the as3 port every class begins with b2 and function names start with a capital letter - how ugly... after looking at the sources of jbox2d i think it is a good port. the leading m_ for members etc. isn't the latest java coding style but the rest seems to be very javaish.
you can learn box2d with the sample sources easily. just look at testbed.PTest and
testbed.tests.PolyShapes. In AS3 you have a nice DebugDrawer - this seems to be not the case with jbox2d so you should write your own one (most of the code in PTest is a debug renderer for processing). I don't understand why they didn't implement a simple Swing Debug Renderer... btw the latest examples in the jbox2d repository packed most of the rendering stuff into an new class called ProcessingDebugDraw - so it should be even easier to understand the Test-Classes (new in repository: testbed.TestbedMain, testbed.AbstractExample and i.e. testbed.tests.Circles)...

I'll spend some more time with the box2d demos then. If anything, it has to be better than the phys2d ones...
Logged

The enemy's gate is down.
Hayato
Level 0
**


View Profile
« Reply #9 on: February 05, 2010, 02:45:23 PM »

After seeing all the Box2D, i kinda want to use it and just code in c++. However, that would involve also learning some graphics library for it, as well as expanding my knowledge of c++...

Why can't this be easier?

On a somewhat related note, does anyone know how to add Box2D to Dev C++? I tried to compile one of their examples, but I got all sorts of errors. I know in the Java version, you needed to add the jar file to the build path. Do i need to do something similar here?
Logged

The enemy's gate is down.
Tycho Brahe
Level 10
*****

λx.x


View Profile
« Reply #10 on: February 05, 2010, 03:12:13 PM »

to use box2d you need to compile the source files given into a library then link that, and include the box2d source in your game. I'm not sure how you do this in dev-c++ but I've done it in visual studio and its probably more or less the same.
« Last Edit: February 05, 2010, 03:15:48 PM by 14113 » Logged
Hayato
Level 0
**


View Profile
« Reply #11 on: February 05, 2010, 04:00:32 PM »

...how do I do that?
Logged

The enemy's gate is down.
Mikademus
Level 10
*****


The Magical Owl


View Profile
« Reply #12 on: February 05, 2010, 04:18:01 PM »

After seeing all the Box2D, i kinda want to use it and just code in c++. However, that would involve also learning some graphics library for it, as well as expanding my knowledge of c++...

Why can't this be easier?

Perhaps you can see it as something positive? The vast majority of all game-related code is for C++, and regardless of one's language preferences, it is ultimately a severe handicap not to have access to it. C++ is an extremely satisfying language, if with a... eh... slight initial threshold, so you might in fact be in for a pleasant ride!

NinjaHand Point Right Hand Point LeftGentleman
Logged

\\\"There\\\'s a tendency among the press to attribute the creation of a game to a single person,\\\" says Warren Spector, creator of Thief and Deus Ex. --IGN<br />My compilation of game engines for indies
Alex May
...is probably drunk right now.
Level 10
*


hen hao wan


View Profile WWW
« Reply #13 on: February 05, 2010, 04:21:15 PM »

I would say keep at it with the java and jbox2d. If it works then the only thing that needs to happen is for you to understand how it works. Keep plugging away until you got it working!
Logged

Hayato
Level 0
**


View Profile
« Reply #14 on: February 05, 2010, 04:23:12 PM »

I would say keep at it with the java and jbox2d. If it works then the only thing that needs to happen is for you to understand how it works. Keep plugging away until you got it working!


Alright! I won''t give up on java! Mainly because I realize that there is still way too much that I don't know about c++, and porting the parts of my game over that actually work would take even more time, but hey, a reason is a reason, right?
Logged

The enemy's gate is down.
Alex May
...is probably drunk right now.
Level 10
*


hen hao wan


View Profile WWW
« Reply #15 on: February 05, 2010, 04:28:23 PM »

Yeah I don't think porting from Java to C++ with no C++ knowledge is a productive idea, definitely Smiley
Logged

Hayato
Level 0
**


View Profile
« Reply #16 on: February 05, 2010, 04:35:00 PM »

Does anyone here have any experience with JBox2D? The hello world from the c++ version doesn't convert over very well, due to changes in syntax and method names/calls/etc. OIf someone could write up a hello world in pure java, that would be great. It doesn't have to draw, just show how to create a world, a ground, a few bodies, apply forces, etc.
Logged

The enemy's gate is down.
skyy
Level 2
**


[ SkyWhy ]


View Profile
« Reply #17 on: February 05, 2010, 04:52:19 PM »

But on one thing I have to STRONGLY disagree: Runescape is NOT a game. It's... seventh level of hell  Durr...?

I had to point this out when one of my lecturers was going on about games and Java. So Runescape, no, no, no. It's satanic. Other than that, rock on everybody. Over and out. Big Laff
Logged

Chromanoid
Level 10
*****



View Profile
« Reply #18 on: February 05, 2010, 04:59:37 PM »

because i was bored here some simple sample code using the current jbox2d (2.0.1-b250):
Code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.Timer;
import org.jbox2d.collision.AABB;
import org.jbox2d.collision.shapes.PolygonDef;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.World;

public class SimpleBox2DTestFrame extends javax.swing.JFrame {

    MyJBox2DTestPanel box2DTestPanel;

    /** Creates new form SimpleBox2DTestFrame */
    public SimpleBox2DTestFrame() {
        initComponents();
        this.getContentPane().add(BorderLayout.CENTER, box2DTestPanel = new MyJBox2DTestPanel());
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {

        addBoxButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        addBoxButton.setText("add Body");
        addBoxButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addBoxButtonActionPerformed(evt);
            }
        });
        getContentPane().add(addBoxButton, java.awt.BorderLayout.PAGE_START);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-720)/2, (screenSize.height-575)/2, 720, 575);
    }

    private void addBoxButtonActionPerformed(java.awt.event.ActionEvent evt) {
        box2DTestPanel.addBox((int) (Math.random() * 500.0 + 100), 128);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new SimpleBox2DTestFrame().setVisible(true);
            }
        });
    }
    private javax.swing.JButton addBoxButton;


    //interesting stuff here:
    class MyJBox2DTestPanel extends JPanel implements ActionListener {

        int w, h;
        World world;
        AABB worldAABB;
        Timer refreshTimer;
        ArrayList<Body> physicalBodies = new ArrayList<Body>();
        int frameX, frameY;

        public void createWorld() {
            worldAABB = new AABB(); //everything outside of this goes to sleep
            worldAABB.lowerBound.set(0.0f, 0.0f);
            worldAABB.upperBound.set(1000.0f, 1000.0f);
            Vec2 gravity = new Vec2(0.0f, 100.0f);
            boolean doSleep = true;
            world = new World(worldAABB, gravity, doSleep);
            world.setWarmStarting(true);
           
            // Create border of boxes
            PolygonDef wallShapeDef = new PolygonDef();
            BodyDef wallBd = new BodyDef();

            // Left
            wallBd.position.set(5, 250);
            wallShapeDef.friction = 1.0f;
            wallShapeDef.setAsBox(5, 250);
            world.createBody(wallBd).createShape(wallShapeDef);

            // Right
            wallBd.position.set(695, 250);
            world.createBody(wallBd).createShape(wallShapeDef);

            // Top
            wallBd.position.set(350, 5);
            wallShapeDef.setAsBox(340, 5);
            world.createBody(wallBd).createShape(wallShapeDef);

            // Bottom
            wallBd.position.set(350, 495);
            world.createBody(wallBd).createShape(wallShapeDef);

            addBox(320, 128);

        }

        public MyJBox2DTestPanel() {
            setBackground(Color.white);
            //Initialize JBox2D
            createWorld();
            //set refresh Timer
            refreshTimer = new Timer((int) (1000.0 / 30.0), this);
            refreshTimer.start();
        }

        public void resetTrans(Graphics2D g2) {
            AffineTransform at = new AffineTransform();
            at.setToIdentity();
            g2.setTransform(at);
        }

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            Dimension d = getSize();
            w = d.width;
            h = d.height;

            resetTrans(g2);
            //draw wall
            g2.setColor(Color.red);
            g2.drawRect(10, 10, 680, 480);
            //draw bodies
            for (Body body : physicalBodies) {
                resetTrans(g2);
                g2.translate(body.getWorldCenter().x, body.getWorldCenter().y);
                g2.rotate(body.getAngle());
                g2.drawRect(-10, -10, 20, 20);
            }


        }

        public void addBox(int x, int y) {
            PolygonDef shapeDef = new PolygonDef();
            BodyDef bodyDef = new BodyDef();
            bodyDef.position.set(x, y); //worldposition
            bodyDef.angle=(float)(Math.PI*Math.random());
            shapeDef.density = 25.0f; //density defined -> dynamic object
            shapeDef.friction = 0.1f;
            shapeDef.restitution = 0.8f; //bouncy
            shapeDef.setAsBox(10, 10);
            Body physicalBody = world.createBody(bodyDef);

            physicalBody.createShape(shapeDef);
            physicalBody.setMassFromShapes();
            physicalBody.setBullet(false);
            physicalBodies.add(physicalBody);
        }

        public void actionPerformed(ActionEvent e) {
            world.step(1.0f / 30.0f, 10);
            this.repaint();
        }
    }
}
Logged
Hayato
Level 0
**


View Profile
« Reply #19 on: February 05, 2010, 05:08:06 PM »

I think I may be using a different version. I copied and pasted that code into eclipse, and I got a lot of errors.

Even this:

 worldAABB = new AABB(); //everything outside of this goes to sleep
            worldAABB.lowerBound.set(0.0f, 0.0f);
            worldAABB.upperBound.set(1000.0f, 1000.0f);

has a few. Where do I download the version that you are using?
Logged

The enemy's gate is down.
Pages: [1] 2
Print
Jump to:  

Theme orange-lt created by panic