Creating objects with simple drawing methods of JOGL

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Creating objects with simple drawing methods of JOGL

Lawa
Hello ,
I'm trying to create a simple 2D game with JOGL and use a physics engine (dyn4j) with the components that I'm drawing using JOGL with the simple methods of (glRecti , glBegin(GL2.GL_POLYGON) and so on .. ) , but apparently the methods that I'm using for drawing the shapes are all void (not objects) and can't be used to add physics to it in dyn4j , so my question is there any possible way to add physics to the shapes that are being drawn with those methods?

I was thinking about creating a class with those methods inside (init , display) and then create an object from it in another class and use that object to add physics to, but that doesn't seem possible..

I appreciate any help with this matter , and I know the way im trying to do things is wrong but this is for my project which MUST use those simple methods and I can't figure out how to measure collision except of using a physics engine which doesn't seem possible in my case ..

Thanks in advance
Reply | Threaded
Open this post in threaded view
|

Re: Creating objects with simple drawing methods of JOGL

gouessej
Administrator
Reply | Threaded
Open this post in threaded view
|

Re: Creating objects with simple drawing methods of JOGL

Lawa
Yes I actually did that but in that example JOGL is being used as a renderer ONLY and it is not being used as a drawing tool , all the shapes are being created using classes from dyn4j , but in my case I want the shapes to be drawn using JOGL it self and then give them a collision detection ..
Reply | Threaded
Open this post in threaded view
|

Re: Creating objects with simple drawing methods of JOGL

gouessej
Administrator
JOGL is mostly a Java binding for the OpenGL/OpenGL-ES API. JOGL is used in this example to draw the shapes, the example overrides GLEventListener, it uses an animator, the objects of dyn4j contain the vertices but they are passed to JOGL in the main rendering method and as JOGL is a low level binding with a few helpers, you won't find object oriented high level objects to represent a shape, it's typically what you can find in a physics engine, in a framework or in an engine based on JOGL.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Creating objects with simple drawing methods of JOGL

Lawa
oh okay so what I understand is that it is not possible to draw anything using the base JOGL methods such as (glRecti) and give it physics?
Reply | Threaded
Open this post in threaded view
|

Re: Creating objects with simple drawing methods of JOGL

gouessej
Administrator
It's possible, you can write your own code to handle the vertices of the dyn4j objects with JOGL by using whatever you like, glRect, ... You must create some dyn4j objects and add them to the "world" to "give" them "physics".
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Creating objects with simple drawing methods of JOGL

Lawa
Umm I'm not sure that I understand that cause I'm very new in graphics and this would be a project so I know how things work in JOGL , can you explain further? or a tutorial or maybe an example of how can I handle vertices my self?

Thanks
Reply | Threaded
Open this post in threaded view
|

Re: Creating objects with simple drawing methods of JOGL

gouessej
Administrator
Look at this line:
https://github.com/wnbittle/dyn4j/blob/master/examples/org/dyn4j/examples/ExampleJOGL.java#L116

Instead of creating dyn4j Polygon objects, you can create dyn4j Rectangle objects and use glRect instead of GL_POLYGON.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Creating objects with simple drawing methods of JOGL

jmaasing
In reply to this post by Lawa
You should not design you application/game in such a way. The best way in my opinion is to design your application so that physics and visuals are separate from the game logic. That means you will have some game entity that has variables and logic only related to game play and that have nothing to do with visuals or physics.

The game entity is then used to create a physics body that is added to dyn4J, the same game entity is then also used to create something that is used for rendering (for example triangle mesh and shader). So you end up with separate classes dealing with different aspects of the game.

In my game I use a lot of game entities, they have a polygon definition using just plain math objects. The polygon definition is used to create Dyn4J bodies (usually polygons but also circles and so on). I put a reference in the "user data" part of Dyn4J-body to the game entity and I let the game entity have a reference to the Dyn4J body.
When for example Dyn4J detects a collision I have a collision listener, it looks at the Dyn4J body and retrieves the game entity reference from the user data to inform about collisions. When the game entity decides it should be removed from physics it has a reference to the Dyn4J body and can remove that from physics.

This works out really well and separates concerns. In this way you can for example change the rendering without changing anything in the physics and game logic. Especially if you later want to leave the deprecated OpenGL calls and move to the current generation using shaders.
Reply | Threaded
Open this post in threaded view
|

Re: Creating objects with simple drawing methods of JOGL

Lawa
This post was updated on .
In reply to this post by gouessej
I don't think you understood what I'm trying to do here ..
for example lets say I have this in display() method :
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex2i(0,0);
gl.glVertex2i(0,100);
gl.glVertex2i(100,100);
gl.glVertex2i(100,0);
gl.glEnd();

basically this is a rectangular shape (or any other shape) created using the basic methods of JOGL how do I implement physics engine to this particular shape ?
Reply | Threaded
Open this post in threaded view
|

Re: Creating objects with simple drawing methods of JOGL

Lawa
In reply to this post by jmaasing
Thanks for the suggestion, but as I explained above I need this for a project that MUST be as I explained (if its possible) using the main simple drawing methods of JOGL and use the physics engine with them, is that even possible?

Thanks for the reply :)
Reply | Threaded
Open this post in threaded view
|

Re: Creating objects with simple drawing methods of JOGL

jmaasing
In reply to this post by Lawa
Lawa wrote
I don't think you understood what I'm trying to do here ..
for example lets say I have this in display() method :
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex2i(0,0);
gl.glVertex2i(0,100);
gl.glVertex2i(100,100);
gl.glVertex2i(100,0);
gl.glEnd();

basically this is a rectangular shape (or any other shape) created using the basic methods of JOGL how do I implement physics engine to this particular shape ?
class MyGameEntity {
  int[] vertices = [0,0 0,100, 100,100, 100,0] ;
}

...

class RenderableThing {
   MyGameEntity drawThis ;

...
   glVertex2i(drawThis.vertices[0], drawThis.vertices[1]) ;
   glVertex2i(drawThis.vertices[2], drawThis.vertices[3]) ;
}

class PhysicsThing {
  MyGameEntity makeThisPhysical ;

  public void addToPhysics(World world) {
    Body b = new Body(new BodyFixture(new Rectangle(//calculate the width & height from the makeThisPhysical object))) ;
    world.add(b) ;

See how one class handles drawing, the other handles adding a physics object. Both are using the basic polygon data defined in MyGameEntity.  You know what data you send to OpenGL - use the same data as input to drive the physics engine.
Reply | Threaded
Open this post in threaded view
|

Re: Creating objects with simple drawing methods of JOGL

Lawa
Thank you very much ! That is a really brilliant idea to do it ! will surely try and do that and will post the results !
Thank you again!
Reply | Threaded
Open this post in threaded view
|

Re: Creating objects with simple drawing methods of JOGL

elect
Anyway you should be aware you are using deprecated OpenGL, unless explicitely needed, you should skip it