Box2D, as3, help please.

6 posts

Flag Post

I am using box2D flash library in my game. In the world there are some static, kinematic and dynamic bodies, all stored into Global.scrolB2 array, created in same world. The main character of game is box2d body too, moving left and right with linear velocity and jumping up with impulse. I made world scrolling:

function MoveWorld(l){
			trace(l);
			if (Math.abs(l)>0.3){
			t = Global.scrolB2;
			var pos:b2Vec2;
			for (i = 0; i<t.length; i++)
			{
				pos = t[i].GetPosition();
				pos.Set(pos.x+l/Global.PRate, pos.y);
				t[i].SetPosition(pos);
			}
			t = Global.scrol;
			for (i = 0; i<t.length; i++)
			{
					t[i].x += l;
			}
			pos = Body.GetPosition();
			pos.Set(pos.x+l/Global.PRate, pos.y);
			Body.SetPosition(pos);
			}
		}

The idea is that, on each frame, the script counts the distance between the very first main body position and changed position. After it script applies MoveWorld function. There are some problems, which i cant fix in it, can you help me? Errors:
1)With this function character sometimes gets stuck when its moving around, jumping launches the character up in air and left.
Jump and movement:


var vel = Body.GetLinearVelocity();
vel.x = -2;
Body.SetLinearVelocity(vel);


b2dpos = Body.GetPosition();
tempvec = Body.GetPosition();
var tempi = -1 * Body.GetMass() *
Math.sqrt(2*10*5000/(Global.PRate*Global.PRate));
tempvec.Set(0, tempi);
Body.ApplyImpulse(tempvec, b2dpos);

2)The Box2D bodies and images(which are stored in Global.scrol) are moving with different speed(Global.PRate is convertion rate from pixels to meters, which are used with Box2D). This is probably rounding problem(after l/Global.PRate part, PRate is 40, 0<=l<=2);

 
Flag Post

Moving the whole world sounds like a lot of trouble. If every object teleports several pixels to the left every frame, the physics engine will have a hard time coping with that. Routinely using SetPosition is not recommended in box2d, because it bypasses the physics engine. Better to leave the world where it is, and centre the view/camera on your protagonist instead.

 
Flag Post
Originally posted by CuriousGaming:

Moving the whole world sounds like a lot of trouble. If every object teleports several pixels to the left every frame, the physics engine will have a hard time coping with that. Routinely using SetPosition is not recommended in box2d, because it bypasses the physics engine. Better to leave the world where it is, and centre the view/camera on your protagonist instead.

I had only seen if how to do it in box2d for C languages. Can you tell me if how would i do it in flash(in C it was something about the camera object, but i know nothing about it in flash)?

 
Flag Post

Unless you’re using Box2D debug draw feature, you have code to redraw the world every frame, and/or update a bunch of sprites to follow Box2D’s world model.

In any case, there is a parent display object. If you’re using debug drawing, it’s the object you pass to b2DebugDraw.SetSprite(). If you’re redrawing the world using the graphics API, it’s the object whose graphics property you’re using. If you’re using a bunch of sprites, it’s the object whose addChild() method you call when setting everything up.

Assuming this object is stored as the variable “worldContainer,” this is what your “MoveWorld” function should look like:

function moveWorld(l:Number):void {
	worldContainer.x -= l;
}

That’s it! All you have to do is update the x property of your container object. And if you want to move the camera up or down, all you’d have to do is update the y property.

 
Flag Post

Thank you both for your help, it was very easy to forget about the box2d world sprite property after setting the world. Now everything is working good(though graphic object scroll 2x faster than box2d ones by some reason, but thats solvable, i guess)

 
Flag Post
Originally posted by a3lex33:

graphic object scroll 2x faster than box2d ones by some reason

I’m not sure which “graphic objects” you’re talking about here, but I’d guess you’re moving them individually and moving their parent. (Hint: just move their parent.)