Dynamically set world gravity using WCK

Hi everyone,

I've just started using Box2d WCK (in fact I'm using it in tandem with Air for Android) so thought it time to join a forum and get involved!

Sorry to start with a question but I'm having real difficulty getting my head round the world gravity properties. Essentially I'm trying to change the world gravity based on the phone's accelerometer. I figured I would just set the world (myWorld) instance's properties for gravity in the same manner as the WCK component i.e....

myWorld.gravityX = 9.8;
myWorld.gravityY = 0;

For testing purposes I'm doing this from AS3 code on the root timeline to an instance of the world placed on the first and only frame... no effect.

I've tried the setGravity(x,y) method I spotted after digging around and this method doesn't seem to exist. I'm pretty competent with AS3 generally but this is the first time I've used alchemy, box2D and components like this... clearly I'm missing something 🙂

Any help would be much appreciated!

Thanks!
In the wck.World create() function it does this:
Code:
baseGravity = new V2(gravityX, gravityY);

baseGravity is a V2 and that is used for gravity from that point on. I should probably fix this so you can just set gravityX and gravityY directly. For now you should be able to do:
Code:
myWorld.baseGravity.x = 5;
myWorld.baseGravity.y = 5;
Thanyou for your help! That worked perfectly!

Not to push my luck (I'm about to anyway...) but there are a couple more things I was wondering about WCK...

I read a tutorial about using the GPU on Android phones to speed up Box2D, however it involves applying a matrix to the sprite class (http://www.blackcj.com/blog/2010/08/09/box2d-with-gpu-acceleration-for-android/)

Do you happen to know if this can be worked into WCK? I assume that this technique would be applied to the 'world' MovieClip somehow?

Thanks again for your time mayobutter!
Interesting. I hadn't heard about cacheAsBitmapMatrix before. I don't see why it wouldn't work. It looks like you would just want that set to the identity matrix right? Unless you're scaling things... then I guess you'd want to figure out the correct matrix based on that.

I think this would be applied to BodyShapes, not the World, because the World is constantly changing and would require a new bitmap to be cached each step.

If you define a "ShapeBase" class at the root of your project, that will be used as the base class for all shapes (so everything should extend ShapeBase rather than BodyShape). Then you could turn it on for everything with:

Code:
public class ShapeBase {
  public function ShapeBase() {
    cacheAsBitmap = true;
    cacheAsBitmapMatrix = new Matrix(); // possibly transform.matrix? or transform.concatenatedMatrix?
  }
}


I'm not sure how well cacheAsBitmap works with rotation though. It looks like cacheAsBitmapMatrix is supposed to match the concatenated matrix of the display object, but if things are rotating, that is constantly changing. Although this would help with static objects, that never rotate.

The BodyShape class already has a "matrix" property which describes how Box2D and Flash coordinates match up. This is critical to nested BodyShapes, because each display object in the hierarchy could have it's own transform. So that matrix represents the concatenated transform.

Ok this post is getting a bit ramble-ish. I think actually transform.concatenatedMatrix should be used, in case the world is also transformed. But maybe the translation properties should be zeroed? Maybe not.
Yup, from what that post says you set the identity matrix. I'm not used to using them, but I think I see what you are getting at. I'll have a look at getting this working and let you know how I get on.

I'm trying to have 50+ 5px*5px squares move about the world by continually changing gravity, they don't actually have to rotate at this stage so perhaps I can get away with using this...

Thankyou again for the help, if I get anything interesting happening I'll post it back up on the forum 🙂
re: cacheAsBitmapMatrix

if you're going to set the alpha, rotation, scale or 3d properties (rotateX etc) of a display object you want to use cacheAsBitmapMatrix after caching the texture to the GPU with cacheAsBitmap=true

basically from what I understand cacheAsBitmap puts the graphic onto a GPU texture sheet, cacheAsBitmapMatrix then lets the GPU do those transformation operations on the hardware.

As far as I know though cacheAsBitmap caches a separate bitmap copy of each object so isn't memory optimized. ie if you have 2 triangle sprites - ie tri1=new Triangle(); tri2=new Triangle() - and set cacheAsBitmap=true on those sprites then it creates 2 separate versions of the same texture on the GPU... (FYI.. Note: you could cache the bitmapdata and dynamically assign that to your display objects's bitmapdata, but manipulating the bitmapdata would affect every object using it... this would be more appropriate for something like a tilemap where every instance of a certain tile animates the same way)

(also see my comments at the bottom of here http://forums.adobe.com/thread/729917?tstart=30)

j

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