Scrolling

First post i make, so hi everyone 🙂
I've been looking for a good AS3 physics engine solution with an API i didn't feel uncomfortable with, and Box2d seems to be it; simple, direct and to the point, and the results are wonderful. Thanks to those porting it to AS3, you just saved my ass on a project 🙂

My question is simple; is there a way to translate the whole world without iterating over every body in it and changing their positions? Say in an Asteroids-like game, where the player is centered and the world moves around the avatar, i'd like to apply an offset to the world as a whole and then a negative of that offset to the player avatar. In a sense what i'm trying to do is move the camera, not the world.

What's a good way to do this?

Cheers,

- Andreas SJ
This type of thing should usually be handled in your display code, not within the physics engine - I would put together some sort of camera or drawing class to wrap all your drawing calls so that you can apply tranformations before they are output to the screen.
I had this same question awhile ago, and its actually quite simple. Basically, you don't move the objects themselves, but you move their visual components.

What you have to do is parent all your sprites to a "container" movieclip(when adding sprites, do container.addChild() instead of doing addChild()), put that movieclip on the stage, and then offset it's position every frame, like this:

Code:
container.x = -playerSprite.GetCenterPosition().x + halfWidth;


In that snippet of code, container would be your container movieclip, playerSprite would be the sprite for the player(or whatever you want to be in the center), and halfWidth would be half the width of the stage.
This would depend on if you are using Sprites and stuff or using copyPixels method to draw things. If you are using Sprites/MovieClips method I think you just move the Sprite that your main class inherits from. If you are using copyPixels, you would have to do like ewjordan said and do your own transformations to draw things in the right part of the screen. One handy thing for this is to use an AABB Query from Box2D to get all the shapes that are within a certain coordinate box. I highly recommend using the copyPixels method, because it is many times faster than letting Flash handle it.
Ahh, I've never heard of the copyPixels method (I'm pretty new to this). Are there any tutorials/examples/information that would help me understand it?
I don't know of any tutorials on it really, but there might be one somewhere. To use the copyPixels method of a BitmapData is fairly simple. The problem is if you need to rotate you have to manipulate the data, and you can't just set the rotation property. It is much faster than using Sprites, but more difficult to get working. I have it working finally, but there is more optimization that could be done. I wouldn't mind writing a small tutorial when I have time if you or others are interested. I hope to make an open source raster graphics library for AS3 when I get a chance since it's basically done already. The only problem is that it's all wrapped up in my other code and there are a few more things I would like to add to it.

You can find the docs on copyPixels here.

Here is a quick example, but this is just for copyPixels, which doesn't include rotation at all.

Code:
   var bitmap:Bitmap = new Bitmap();
   var buffer:BitmapData = new BitmapData(screen_width,screen_height,true,0x00000000);
   bitmap.bitmapData = buffer;
         
   buffer.lock();
   //do all your drawing here

   buffer.copyPixels(your_bitmap,your_bitmap.rect,new Point(x,y),null,null,true);

   //allow the Bitmap object to be updated after you're done drawing
   buffer.unlock();


So what you are doing is creating a Bitmap object, which would need to be added to the stage, and making a BitmapData object the size of the flash window (assuming screen_width/screen_height exist and are set to that). Then you tell the Bitmap to display your data, but prevent it from refreshing while you are doing your drawing.

"your_bitmap" is just any BitmapData object you want to draw, and the Point(x,y) you would just put in coordinates of where to draw it at. The last value is set to true if you want to use alpha blending (transparency).
Hmm i think I've been thinking too "flat" overall. It's not like i use Box2d simulation all the time for everything. In a sense i could be adding and destroying bodies as they become needed on stage. Given a world bounding box that's a fair amount bigger than the stage area i would only need bodies at the time when game entities actually enter the stage and a buffer area around it.

This way i could just subtract the position of the entity from the game world position, add bodies as needed and simulate from there on until the entity leaves the area of activity (is there a better name for this).

It's totally right; scrolling is just a visual effect after all, it should have no bearing on the physics.
Thanks for the example, brainclog! A tutorial would be awesome if you had the time.

Original thread: https://box2d.org/forum/viewtopic.php?t=412