How do you draw elements with JBox2D? I'm fine using the DebugDraw, I just want to find a quick way to do this in Java since I haven't worked much with graphics.
Do I need to use a Canvas? Or a JFrame? And how does the world know when I call
world.drawDebugData()
where to draw it to?
How could I devise a class that just drew points where I want them, and integrate this with JBox2D?
...
while(true)
world.step(timeStep, velocityIterations, positionIterations);
Vec2 position = body.getPosition();
float angle = body.getAngle();
System.out.printf("%4.2f %4.2f %4.2f\n", position.x, position.y, angle);
}
I imagine I could put this code somewhere inside this while loop, but I'm not exactly sure where. Is there a function that's called every time a World steps? Can I put a draw function in there? I'm even ok using the DebugDraw if I could figure it out...
Could you please help me understand what kind of class would take all the objects in the World object and draw them continually? (and where to draw to?)
You can easily create new testbed tests, this is the best way to test out your new physics ideas and not worry about drawing and GUI stuff. It even supports adding extra real-time options. Check it out here:
https://code.google.com/p/jbox2d/wiki/Testbed
It's difficult to know where to organize everything if you're not used to programming games and physics engines.
Dyn4j is another great physics library that has great examples explaining where to put everything and even how the user can interact with the world. A lot about the dyn4j library is identical to box2d/jbox2d, so if you just try out their examples real quick, it will lend a hand to figuring out how to render graphics in box2d.
Related
I'm creating a 2d game using the Java version of Box2d.
I know that the engine is generally paired with a separate library to handle animation, but I'm looking for something simple, with a more rustic feel, so I'm considering handling the animation from scratch.
The animation I am trying to create is something for the collision of two objects.
I'm using openGl to handle the graphics, and I pretty much have everything set up with the fps and rendering, it's just a matter of tweaking the frames to make it look smooth.
I tried bliting images of some things that I drew myself and it looked atrocious. I think the main problem is that the animation frame images are not linked to the physics forces and so they don't blend well.
What I want to try next is a sort 'spark effect' using a small number of pixels that sort of resemble 'dust' exploding off of the colliding images. If you've ever played dxball, you may recall a similar effect when the ball collided with the paddle at high speed.
As you can see in the above image, the principles are fairly simple. I figure by creating a set of small bodies with fixtures, I can use the box2d engine to produce this effect. I already have code that grabs the point of collision for all bodies, and am familiar enough with parabolic functions that I pretty sure I can create the force direction.
I'm just not sure if there is a better way of doing this, or how to apply a polynomial function to a body like in the image.
Does anybody know how to achieve this functionality with box2d? Any built in methods or documentation that would be helpful?
As a point of reference, the following snippet of code demonstrates the drawing of a small, red box on all of the collision points.
for(Vec2 point : listener.worldmanifold.points){
glPushMatrix();
newpos = point.mul(30);
glTranslatef(newpos.x, newpos.y, 0);
glColor3f(1,0,0);
glRectf(-2, -2, 2, 2);
glPopMatrix();
}
I wanted to make a circle that has a ripple-effect on the edges, kind of like in the game agar.io. I am kind of lost on how to implement it. Obviously I can't just g.fillOval() because that would draw a solid circle with no movement on the edges.
I'm not asking anyone to write any code for me (but if you really want to, I don't mind :D), but if you could point me in the right direction with some methods I should use. I am using Slick-2D library for java, if that helps.
I also tried analyzing the javascript source from the agar.io website to try to understand how they implemented it in javascript, but I was unsuccessful because the code was obfuscated; all the methods and variables were just single letters.
The only way I can imagine doing this currently is to have each circle be composed of a number of points, and let each point have it's own physics, and it can be affected by other points. If anyone who has insight into this problem, I would greatly appreciate some help. Thank you!
I'm not sure you can do this with Slick2D. It is quite high level and gives a lot built-in classes. What you want to do is really specific. As Slick development has stopped you will not get new features. You should probably look at lwjgl which is the base of Slick. It is more low-level but can be more precise with the form you need.
You can look at this project to have some drawing cool stuff. And for another example of manipulating circle you have this one
I will be working with an application that will use 3D graphics in Java and I am pretty sure we'll need an arc object in 3D. I've seen some proposed methods on how to do that, one of which entailed creating an Arc2D object and then rotating it in 3D. Not quite sure how to do that because I couldn't find how to rotate a plane in Java 3D, but I imagine I need to use the javax.media.j3d.J3DGraphics2D object.
Regardless, I am wondering if it is worth my time to create an Arc3D object. It will make everyone's life much easier, it makes more sense to simply have an arc in 3D than create an entire drawing plane and then rotating it, and overall it would be a contribution to Java programmers.
My questions are, from your experience, how long does it take to create a new class that works in an existing API? and where would I start? I tried looking at the Graphics, Graphics2D, and Arc2D documentations and couldn't find where the actual drawing takes place so I can see how to do the same in 3D.
Any help?
The 'AWT Shape Extruder' API supports extruding of an Arc2D object in Java 3D. Download is available here: http://www.interactivemesh.org/testspace/awtshapeextruder.html
I am new to the world of libgdx and the world of game programming in general. I want to create a game, but not any game. I have created some basic game like breaks, and pong. But I still cant go any further, I google for good articles, but I always have problems with collision, especially between entities! I want to create a game with slopes like sonic.
Why not use Box2D (libGDX extension) ? It's perfect for platformers.
Do you know how to create rectangles. I assume that you know about rectangles.
if you want to check collision of two rectangles you can do as follows:
Rectangle a = new Rectangle(), b = new Rectangle();
in constructor set rectangles
a.setRectangle(yourX, yourY, yourWidth, yourHeight);
b.setRectangle(yourX, yourY, yourWidth, yourHeight);
in render check collision like this:
if(a.overlaps(b))
{
//do your work
}
U can use OverlapTester class given in SuperJumper Project by LibGdx
Create Your bounds using rectangle class in Libgdx and test them using Intersector class.
This class has many function to test overlapping of rectangles, circles etc..
I recommend you to use box2d if you know the basics.
if you know how to use a rectangle, sprite batch, camera etc. Then you should proceed to Box2d if you don't just take some good tutorial and try to make the application without any extension.this will make your concept clear and you will easily able to grasp the logic behind the game.
Well I am going to bed right now, and in next 2 days I have to code a simple program with animation that will simulate an inverted broom on a cart (a pole balancing genetic algorithm problem) the cart has to be pushed constantly from both sides to prevent the broom from falling down
You can see it in this video
http://www.youtube.com/watch?v=Ums3eGIVgks
or this picture image http://lis.epfl.ch/research/projects/EvolutionOfAnalogNetworks/ArtificialNeuralNetworks/images/mechanik_small.png
Well, I need to simulate physical behavior of this, but I have very little time, so I need something that I can understand and start using fast (the assignment is more into physics and genetic programming, so the simulation has to be just to show how it works).
Thank you
I'd use HTML5 Canvas and pure javascript. It's super easy, you don't need to compile. All you need is notepad, Chrome/Firefox/IE9, and a little time. There are tons of examples out there:
http://3.paulhamill.com/node/36
If you want an application, create a java application which uses a JPanel in a JFrame. In the JPanel override the paintComponent(Graphics g) method, and look at the graphics class which allows you to draw simple shapes like lines, rectangles, and ellipses.