java2d clipping question

Hi, Im making a tile based game. In my game I hava a camera class which encapsulates a Rectangle2D that represents where the screen is in world co-ordinates. I am confused about how to use a Graphics2D’s clipping mechanisms.
should I:

a) set the Graphics2D to clip the camera then draw the scene using world space co-ordinates

or

b) set the Graphics2D to clip the screen and translate the sprites and tiles to screen space using the camera information

also

a) by setting the clip can I draw everything in the scene because it automaticly dosn’t try to render offscreen data

or

b) do I find sprites and tiles that that are contained or that intersect the camera.

thank you for your help

if (getBounds().intersects(screen)) {
                  trans.setTransform(1, 0, 0, 1, x - screen.getX() - width/2, y - screen.getY() - height/2);
                  trans.rotate(theta, width/2, height/2);
                  g2d.drawImage(image, trans, null);
}

That’s what I do. I check if my stuff is going to be on the screen, then I shift it over by the screen coordinates (screen is a Rectangle2D) and draw it.

Right now I do something simular. My scene is fairly complex so I hava a camera class that represents the screen in world space, then I find objects that intersect the camera and translate them to screen space and finaly draws them. I am now having difficulty with this system implementing screne wraping. where if part of the camera is outside the bounds of the screen, draw the opposite side of the map as the difference. I thought clipping might help. Has anyone implemented this before? Can you give me some direction on how to implement this.

One thing I’ve used on occasion is to create an intersection rect between the screen and my tile. If the width or height of the intersection is less than the original tile gfx you know that clipping has occured (or rather should be performed).

If that is so, screen.x==intersection.x tells you that clipping has occured on the left side. Vertical clipping can be found in the same way.

Then you have what you need to calculate the wrapped position for the tile. Maybe it’s not the best method but I’ve used it before and it seems to be pretty fast.

Actually, if your tiles are all the same size you only have to calculate this once for left/right-clip and once for top/bottom. After that you can just use those clip values every time you're drawing tiles around the screen edge

/J

[quote]Right now I do something simular. My scene is fairly complex so I hava a camera class that represents the screen in world space, then I find objects that intersect the camera and translate them to screen space and finaly draws them. I am now having difficulty with this system implementing screne wraping. where if part of the camera is outside the bounds of the screen, draw the opposite side of the map as the difference. I thought clipping might help. Has anyone implemented this before? Can you give me some direction on how to implement this.
[/quote]
Do you have lots of objects that can eventually be outside of screen (compared to those onscreen )?

Actually you probably want to avoid calling draw/update on sprites outside the screen if there is a lot of them :slight_smile:

[quote]Actually you probably want to avoid calling draw/update on sprites outside the screen if there is a lot of them :slight_smile:
[/quote]
It depends. I’d bet that Java2D tests if the blit/drawing will be onscreen or not before trying to put a single pixel, thus doing exactly the work you are doing.
Doing it twice would be unnecessary.
Of course, you can optimize differently and not test each sprites for presence, but still, if J2D does the test, you’re wasting CPU.

[quote] I am now having difficulty with this system implementing screne wraping. where if part of the camera is outside the bounds of the screen, draw the opposite side of the map as the difference. I thought clipping might help. Has anyone implemented this before? Can you give me some direction on how to implement this.
[/quote]
To have that sort of inifinite wrap-around thingy, you can:

  1. Check if scene objects are within bounds of the camera, translate them to screen coordinates if they are and then render them (as normal)

  2. Check if the camera bounds is outside the bounds of the scene.

  3. If true, then get the direction(s) of the sides, including the corners, that are out of bounds (ie, top, top-right, right, bottom-right, bottom etc),

  4. For each direction that is out of bounds,

  5. Wrap the scene around in that direction by using offset values. For example, if your scene extends from (0,0) to (99,99) (in world coords), the offsets to wrap around to the top-left corner would be offsetX=-100, offsetY=-100.

AND

  1. Using the offset values, check if wrapped-around scene objects are within bounds of the camera, translate them to screen coordinates if they are and then render them.

In the worst case, you would have to do intersection/containment checks for scene objects 3 times. If you have lots of scene objects, consider partitioning your scene (using quad-trees or something else) to cut down on those checks. Otherwise, intersection/containment checks with Rectangle objects are performed very quickly and aren’t a performance bottleneck.