bg scroll

Hi,

how to scroll BG..??
.i am using
body.ApplyForce(new b2Vec2(20,0), body.GetWorldCenter()) ...to control player
and m_sprite.x -= scrollX to make whole BG move...

but since both are working simultaneuosly..its looking jerky...i want ApplyForce effect to BG
Easiest way is to create a body in the world to represent the camera, and turn off collisions, gravity etc. Then instead of subtracting scrollX/Y, you get that bodies position each frame and use that. Then you can control the body the usual way.
Hi thanks ..but still i am confused
package TestBed{

import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Dynamics.Joints.*;
import Box2D.Dynamics.Contacts.*;
import Box2D.Common.*;
import Box2D.Common.Math.*;
import Main;
import General.Input;
import flash.utils.getTimer;
import flash.display.*;
import flash.events.*


public class Test {
public var scrollX=5
public function Test() {

m_sprite = Main.m_sprite;
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-100.0, -100.0);
worldAABB.upperBound.Set(1000.0, 1000.0);
var gravity:b2Vec2 = new b2Vec2(0.0, 10.0);
var doSleep:Boolean = true;
m_world = new b2World(worldAABB, gravity, doSleep);
var dbgDraw:b2DebugDraw = new b2DebugDraw();
dbgDraw.m_sprite = m_sprite;
dbgDraw.m_drawScale = 30.0;
dbgDraw.m_fillAlpha = 0.5;
dbgDraw.m_lineThickness = 1.0;
dbgDraw.m_drawFlags = b2DebugDraw.e_shapeBit;
m_world.SetDebugDraw(dbgDraw);
var wallSd:b2PolygonDef = new b2PolygonDef();
var wallBd:b2BodyDef = new b2BodyDef();
var wallB:b2Body;
wallBd.position.Set(26,13);

wallSd.SetAsBox(3, .1);

wallB = m_world.CreateBody(wallBd);
wallB.CreateShape(wallSd);
wallB.SetMassFromShapes();


//////////ship creation
var shipShapeDef:b2PolygonDef = new b2PolygonDef();
shipShapeDef.vertexCount=3;

shipShapeDef.vertices[0].Set(6.0,2.5);
shipShapeDef.vertices[1].Set(0.0,5.0);
shipShapeDef.vertices[2].Set(0.0,0.0);
shipShapeDef.friction = 0.5;
shipShapeDef.restitution = 0.3;

var shipBodyDef:b2BodyDef = new b2BodyDef();
shipBodyDef.userData = new Ship();
shipBodyDef.userData.width = 30
shipBodyDef.userData.height=30
shipBodyDef.position.Set(7, 7);

var shipBody = m_world.CreateBody(shipBodyDef);
var myShape:b2Shape=shipBody.CreateShape(shipShapeDef);









////////////
}
public function Update():void {
if (Input.isKeyPressed(39)){

/// what to write here to make BG move ( here BG is having wallB as a object)

}
if (Input.isKeyPressed(37)){



//m_sprite.x += scrollX

}

}
Something like:
Code:
package TestBed{
//...
public class Test {
public var scrollX=5;
public var camera:b2Body = null;
public function Test() {
//...snip...
wallB = m_world.CreateBody(wallBd);
wallB.CreateShape(wallSd);
wallB.SetMassFromShapes();
camera = wallB;
//..snip...
}
public function Update():void {
if (Input.isKeyPressed(39)){
camera.ApplyForce(new b2Vec2(1,0))
}
if (Input.isKeyPressed(37)){
camera.ApplyForce(new b2Vec2(-1,0))
}
m_sprite.x += camera.GetPosition.X;
m_sprite.y += camera.GetPosition.X;

}

You'd need to apply some damping as well.

However, I'm concerned that you are a bit confused over what you are trying to do. You want to scroll the world around while having a player spaceship fly around, yes? Well then you shouldn't really be moving the camera independantly from the spaceship.
Instead you want the spaceship to move around, everything else to stay still, and to move m_sprite so that the space ship is always in view.
HI
well i think some error lies here..m_sprite.x += camera.GetPosition.X;//not accessiable.....i dont knw about this..




well i want something like this effect...http://www.bakis.se/temp/downhill/
were player is at center and BG is moving...
Oh, man, terribly sorry, that was a stupid mistake on my behalf, I must have been half asleep. How's:
Code:
m_sprite.x = camera.GetPosition().x;
m_sprite.y = camera.GetPosition().y;
instead?

To get the player to be the center, you should substitute camera with the body you want in the center, and then subtract half the width/height of the screen.
E.g.
Code:
m_sprite.x = player.GetPosition().x-screenWidth/2;
m_sprite.y = player.GetPosition().y-screenHeight/2;
This is probably what you had in mind to begin with, if so, I apologize for misinterpreting you.
After some bugging by pm, I'll produce a working example:
Attachment:
TestRagdoll.txt

Replace TestRagdoll.as with this (with the extension changed, obviously), and you'll see what I mean.

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