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 & HelpPrograms › JAVA2D vs P2D
Page Index Toggle Pages: 1
JAVA2D vs P2D (Read 2606 times)
JAVA2D vs P2D
Jun 17th, 2009, 10:54pm
 
Hi everyone, I'm kind of new to "processing", but not to programming.
I just finished my first project:
User creates small objects by clicking.
Every object reacts with all the others in every step of the "void draw" loop. That means:
If I have 2 objects, i have 4 repetitions within the loop.
If I have 10 objects, I got 100 repetitions.
If I have 20 objects, I got 400 reps and so on.

The thing is that the program starts to flicker and slow down
when I create more than 20 instances of the object.

It sure has to do with the load of computation which increases dramatically.

But...
I thought i could lighten the thing by using P2D graphics mode instead of JAVA2D and it got much worse.
The graphics I'm using are quite simple. I only use arcs (circles) to represent the objects and lines and rectangles for some other stuff.

P2D is supposed to be suitable for faster simple graphics?
What am I missing? Plz some help on that...
Re: JAVA2D vs P2D
Reply #1 - Jun 18th, 2009, 12:35am
 
Not directly answering your question; but it sounds like your code could be optimised...  though it may depend on the interaction between the objects.

If in your array of objects it doesn't matter whether object A does something to object B or vice-versa each frame, just so long as one of them acts on the other, then you can significantly reduce the number of operations.  In your current setup it seems that object A will do its thing with object B and then when it comes to object B's turn it does its thing to object A - thereby duplicating the process.

This kind of optimisation is used a lot in collision detection - it may be worth doing a search on that to see some example code Wink
Re: JAVA2D vs P2D
Reply #2 - Jun 18th, 2009, 1:20am
 
blindfish is right it does depend on the nature of the  'interaction'

The code below shows some simple optimisation to avoid comparing A<>B and B<>A and self comparison A<>A

If you run the program the number of comparisons is reduced by over half but also the number of matches are halved becase we are not comparing the same two objects twice.
Code:


int[] objects = new int[20];
int count, same;

void setup(){
 for(int i = 0 ; i < objects.length; i++){
   objects[i] = (int)random(1,10);
 }
 compare();
 compareOpt();
   
}

void compare(){
 count = 0;
 same = 0;
 for(int i = 0; i < objects.length; i++){
   for(int j = 0; j < objects.length; j++){
     count++;
     if(i != j && objects[i] == objects[j]){
       same++;
     }
   }
 }
 print("Unptimised:  comparisons made " +count);
 println("   matches found "+same);
}

void compareOpt(){
 count = 0;
 same = 0;
 for(int i = 0; i < objects.length -1; i++){
   for(int j = i+1; j < objects.length; j++){
     count++;
     if(objects[i] == objects[j]){
       same++;
     }
   }
 }
 print("Optimised:   comparisons made " +count);
 println("   matches found "+same);
}


Re: JAVA2D vs P2D
Reply #3 - Jun 19th, 2009, 12:07am
 
Thanks for taking the time to answer, but actually (and unfortunately) I do need to compare A<>B and B<>A. It is a programme for some kind of gravitational simulation. So each of the objects needs to react with all of the others (excluding itself, of course) and alter its position accordingly in every step of the loop.

It is by definition a heavy piece of code, and any "optimization" would alter the term "simulation".

So apart from changing the code is there any way to use lighter graphics? Anyone knows anything about P2D?
Re: JAVA2D vs P2D
Reply #4 - Jun 19th, 2009, 1:32am
 
Maybe a slightly off the wall suggestion but could use use OpenGL?  I've definitely seen (non-Processing) 2D projects run through openGL to take advantage of hardware-based graphics acceleration, though must admit I'm not sure what's involved...
Re: JAVA2D vs P2D
Reply #5 - Jun 19th, 2009, 1:36am
 
In any form of animation there are 2 parts - calculating the new state/position and drawing the current state. To speed up the simulation you have to either optimise the state calculation or find a better way to do the drawing.

Quote:
Every object reacts with all the others in every step of the "void draw" loop.

does this mean you are doing the state calculations in the draw() method?

There are approachs to the problem

  • calculate the state of an object
  • redraw the current state
  • repeat for each object


and

  • calculate the state of an object
  • repeat for each object
  • draw the current state


The problem with the first approach it all has to be done in the draw() method and if as in your case the state calculations are large then it is not surprising you get flikering.

A possible solution is to use the second approach which can be done in Processing with
Code:

void setup(){
  // blah blah blah

  registerPre(this);
}

void pre(){
  // do all the state calculations here
}

void draw(){
  // draw the objects here
}


With regard to JAVA2D vs P2D.
In the source code comments for PGraphics2D and PGraphicsJava2D it states P2D is better than JAVA2D if you are working at the pixel level but is less accurate than JAVA2D for drawing shapes. Looking at the code for drawing a rectangle in these two classes P2D is doing a lot more work than JAVA2D

In this case perhaps it you should stick with JAVA2D .
Smiley

Re: JAVA2D vs P2D
Reply #6 - Jun 19th, 2009, 1:40am
 
Just posted and noted blindfish's responce and I agree OpenGL is a possible solution I have a sketch that animates a 3D model at upto 60fps and that does a lot of calculations between frames.

You might like to see it
http://www.lagers.org.uk/g4p/applets/g4p_ogro/index.html
Re: JAVA2D vs P2D
Reply #7 - Jun 19th, 2009, 5:07am
 
Actually I made the suggestion because I had used OpenGL on a 2D project and found I was able to render it at larger dimensions and with better performance than using the 2D rendering methods; but if you asked me why I don't think I'd be able to give a conclusive answer.

My assumption is that as long as the draw commands sent to OpenGL are understood by the graphics hardware then it will handle the rendering, thereby freeing up the processor to concentrate on the calculations...  though any optimisation of these will obviously help.
Re: JAVA2D vs P2D
Reply #8 - Jun 19th, 2009, 1:10pm
 
20 objects is piss easy to draw at high frame rates. Even 100 objects. The problem must be somewhere other than the drawing stage.

Having said that, I'm making an assumption that the "drawing" stage only draws each of the objects once. If you're looping over 20x20 objects is causing 400 object-draw operations, you have made a blunder that can hopefully be easily corrected by doing all of the position (etc) calculations first and drawing all of the objects later, only once each.

-spxl
Re: JAVA2D vs P2D
Reply #9 - Jun 23rd, 2009, 3:48pm
 
Wow, many things going on here...
I've been away for a few days.
Anyway, the conclusion is that Processing is kind of weak to keep
up with such a piece of code which is bound to fail at a certain
number of instances, whether it is 20 or 200.
By the way the same algorithm runs smoothly on c++ using the
allegro library up until 200 or more instances.
But.. java is easier and doesn't break my nerves to declare a class
the right way. So...
Also the for 20 objects i have 20 object.draw(); in the draw loop.
I will try using the
void pre(){...}
function for the calculations and see if that works.
Or maybe openGl? Still experimenting...
Thanks for the support, guys.
I will also try to post the whole code from an external link
sometime soon. It's a fun, meaningless game of attraction and
repulsion between "particles".

On a different subject...
Is there a way to print text on the screen without including fonts
in the application folder or having to browse for a font file?
Re: JAVA2D vs P2D
Reply #10 - Jun 23rd, 2009, 9:52pm
 
kostino wrote on Jun 23rd, 2009, 3:48pm:
Is there a way to print text on the screen without including fonts
in the application folder or having to browse for a font file

As long as you don't want/need to distribute the sketch or fear the font will be absent from the other computers, you can use createFont instead of loadFont.
Re: JAVA2D vs P2D
Reply #11 - Jun 24th, 2009, 4:02am
 
Hey, I'm just curious but, if it's a game why do you care so much about strict simulation? Gravity has really short arms, so you can pre-compute the gravitational effect of an object up to a certain radius where the effect is negligible, then in every time step add it to a vector field and step all of the objects along their local vectors. If you do it like this, you only have to loop through all of your objects twice, once to create the field and once to update the object positions. Kinda like fluid simulation actually..

In any case, I have found that OpenGL can really speed up drawing, at the expense of making your game very unfriendly to browsers.
Re: JAVA2D vs P2D
Reply #12 - Jun 25th, 2009, 12:24am
 
Quote:
... game of attraction and repulsion between "particles".

Why don't you have a look at the traer physics library http://processing.org/reference/libraries/#simulation_math I have been messing arround with this library using OpenGL and textures and I am getting great frames rates (30-60fps) for 400 particles and 800 textured triangles.
Page Index Toggle Pages: 1