We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpProcessing Implementations › SOLVED:Jbox2d-custom render through ruby-proc.
Page Index Toggle Pages: 1
SOLVED:Jbox2d-custom render through ruby-proc. (Read 2680 times)
SOLVED:Jbox2d-custom render through ruby-proc.
Oct 15th, 2009, 3:22am
 
Good day experts,

My goal is simply to change the color of an Jbox2d shape, or even better to get the shapes transparent .
As the topic says already: I can’t get the custom rendering method work with ruby-processing.
When I try to override the method I get an error like: become sure that you have an argument “World” in the method.
Now I definded the method simply like this:
def custom_rendering(world)
..stuff
end

I try to rebuild this coding from antiplastik:custom rendering

Now I think the problem is because in ruby you usually do not type arguments(or I don’t know how to do that) and world is not clearly typed as an World class.
Hope somebody can help me and many thanks in advance.

br
Re: HELP:Jbox2d-custom render through ruby-processing
Reply #1 - Oct 15th, 2009, 4:13am
 
I don't know Ruby but maybe someone at the Box2D boards can help?

http://www.box2d.org/forum/search.php?st=0&sk=t&sd=d&keywords=ruby&start=20

Maybe you could post some of your code here so we can get a feel of it?

Good Luck!
Re: HELP:Jbox2d-custom render through ruby-processing
Reply #2 - Oct 15th, 2009, 4:31am
 
Thx for you reply, I'll try  it there too ! Maybe you know another way to change the color of an shape ?
BTW: GAME ZERO is a masterpiece!  Shocked How long did you work on that game ? or is it uncountable  Smiley
Re: HELP:Jbox2d-custom render through ruby-processing
Reply #3 - Oct 15th, 2009, 1:59pm
 
so far I tried out the suggestion from Philho to extend the Physics class and overide the defaultDraw method, but doesn't work in any case.
so heres the code:
Code:

class Jbox < Processing::App
#load and include java library stuff...
 def setup
   @p = Physics.new(self,width,height)
   @p.createrect(3,3,5,5)
 end
 def draw
 end
end

this works like a charm. now here's where I'm failing:
Code:

def setup
@p.setCustomRenderingMethod( self, 'custom_rendering')
end
def custom_rendering(world)
end

this is chrashing with mentioned error..

Mixin with module "Physics":
Code:

module Physics
end
class Jbox2d ...
def setup
@p = Physics.new(self,width,height)
.
.


this is enough to break the whole thing, even tough i still didn't done @p.extend(Physics). I get the error that new isn't defined, which i was not intended to override...it seems like ruby prefere to take the local definitions then the java ones.But why?!

then i tried this:
Code:

class Jbox2d ...
...
def defaultDraw(world)
...stuff.
end
..
end

does not work too, it doesn't run in that mehtod..

so these are my attempts, I'm out of ideas..can somebody help?  Smiley
Re: HELP:Jbox2d-custom render through ruby-processing
Reply #4 - Oct 15th, 2009, 2:19pm
 
I don't know Ruby, so I will give my example in Processing/Java, hoping you know how to transcribe it in Ruby.

In my BoxWrap2D sketches, I have a single variable:
Physics physics;
then I instantiate it:
 physics = new Physics(this, width, height);
 physics.setDensity(1.0);


My idea was to make a new class extending Physics:
class CustomPhysics extends Physics
{
 public void defaultDraw(World world)
 {
   background(255);
   smooth();

   for (Body body = physics.getWorld().getBodyList(); body != null; body = body.getNext()) {
     // Etc.
 }
}

then to use it:
CustomPhysics physics;
like a regular physics object.
Re: HELP:Jbox2d-custom render through ruby-processing
Reply #5 - Oct 15th, 2009, 3:23pm
 
Cheesy it seems like I'm on the right way with that
Code:

class CustomP < Java::OrgJbox2dP5::Physics
def defaultDraw(world)

 $app.background(221,21,221)

end
end

it changes the color of the background..what means the method is called !  Grin THANKS PHILHO !!

now, i just have to translate that java into ruby
Code:
for (Body body = physics.getWorld().getBodyList(); body != null; body = body.getNext()) {
  org.jbox2d.collision.Shape shape;
  for (shape = body.getShapeList(); shape != null; shape = shape.getNext()) {
    beginShape();
    if (shape.getType() == ShapeType.POLYGON_SHAPE) {
      PolygonShape poly = (PolygonShape)shape;
      int count = poly.getVertexCount();
      Vec2[] verts = poly.getVertices();
      for(int i = 0; i < count; i++) {
        Vec2 vert = physics.worldToScreen(body.getWorldPoint(verts[i]));
        vertex(vert.x, vert.y);
      }
      Vec2 firstVert = physics.worldToScreen(body.getWorldPoint(verts[0]));
      vertex(firstVert.x, firstVert.y);
      for(int i = 0; i < count; i++) {
        Vec2 vert = physics.worldToScreen(body.getWorldPoint(verts[i]));
        vertex(vert.x-2+int(random(4)), vert.y-2+int(random(4)));
      }
      firstVert = physics.worldToScreen(body.getWorldPoint(verts[0]));
      vertex(firstVert.x, firstVert.y);
    }
    endShape();
  }
}


..uff Undecided where to start

what does this mean?
Code:
  PolygonShape poly = (PolygonShape)shape; 


is this a casting?no..what is that ?!? Undecided
Re: HELP:Jbox2d-custom render through ruby-processing
Reply #6 - Oct 16th, 2009, 6:57am
 
Quote:
is this a casting?

Yes. Probably not necessary in a dynamic language... The shapes can be of several kinds (generic type), so the type is checked then the assignment is used to get a variable with the right type, so that the methods specific to this type, like getVertexCount, can be used.
In JavaScript, for example, we check instead if the function exists in the object... different approach.
Re: HELP:Jbox2d-custom render through ruby-processing
Reply #7 - Oct 18th, 2009, 7:12am
 
yesssss Coolit took me a while but now i have it..just wanna say thx to you Philho,you're really experienced and gived me good advice..thanks again.
Page Index Toggle Pages: 1