I could really use some help I'm stuck on this. I'm trying to make a block in a simple game be the ground and another block fall onto it and then possibly bounce a little but be able to move the box left or right to fall off of that first block. Can anyone help me with this please?
For anyone interested in the answer thanks to PoprostuRonin and dermetfan's YouTube videos I was able to get the results I was looking for, you can try the below code in your project and just change the sprite textures.
private Box2DDebugRenderer debugRenderer;
private OrthographicCamera camera;
private float spriteSpeed = 500000;
private World world;
private Sprite playersprite;
private Sprite groundsprite;
private Body playerBody;
private Body groundbody;
private Vector2 movement = new Vector2();
private Array<Body> tmpBodies = new Array<Body>();
public TestState(GameStateManager gsm) {
super(gsm);
Gdx.input.setInputProcessor(this);
batch = new SpriteBatch();
debugRenderer = new Box2DDebugRenderer();
camera = new OrthographicCamera();
// Sprites
playersprite = new Sprite(new Texture("badlogic.jpg"));
groundsprite = new Sprite(new Texture("ground-tiles-01.gif"));
// World
world = new World(new Vector2(0, -9.8f), true);
// Player Sprite
// Body definition
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(0, 200); //1m
PolygonShape shape = new PolygonShape();
shape.setAsBox(10, 10);
// Fixture definition
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 2.5f; //2.5kg
fixtureDef.friction = 0; //0-1
fixtureDef.restitution = .75f; //0-1
playerBody = world.createBody(bodyDef);
playerBody.createFixture(fixtureDef);
playerBody.setUserData(playersprite);
playersprite.setSize(20, 20);
playersprite.setOrigin(playersprite.getWidth() / 2, playersprite.getHeight() / 2);
shape.dispose();
// Ground Sprite
// Body definition
BodyDef groundbodyDef = new BodyDef();
groundbodyDef.type = BodyType.StaticBody;
groundbodyDef.position.set(0, 0); //1m
PolygonShape groundshape = new PolygonShape();
groundshape.setAsBox(groundsprite.getHeight() / 2, groundsprite.getWidth() / 2);
// Fixture definition
FixtureDef groundfixtureDef = new FixtureDef();
groundfixtureDef.shape = groundshape;
groundfixtureDef.density = 100; //2.5kg
groundfixtureDef.friction = .25f; //0-1
groundfixtureDef.restitution = 0; //0-1
groundbody = world.createBody(groundbodyDef);
groundbody.createFixture(groundfixtureDef);
groundbody.setUserData(groundsprite);
groundsprite.setSize(groundsprite.getHeight(), groundsprite.getWidth());
groundsprite.setOrigin(groundsprite.getWidth() / 2, groundsprite.getHeight() / 2);
groundshape.dispose();
}
#Override
public void update(float delta) {
camera.viewportWidth = Gdx.graphics.getWidth();
camera.viewportHeight = Gdx.graphics.getHeight();
}
#Override
public void render() {
Gdx.gl.glClearColor(50 / 255f, 213 / 255f, 237 / 255f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
debugRenderer.render(world, camera.combined);
batch.setProjectionMatrix(camera.combined);
world.step(Gdx.graphics.getDeltaTime(), 6, 2);
playerBody.applyForceToCenter(movement, true);
batch.begin();
world.getBodies(tmpBodies);
for (Body body : tmpBodies){
if (body.getUserData() != null && body.getUserData() instanceof Sprite) {
Sprite sprite = (Sprite) body.getUserData();
sprite.setPosition(body.getPosition().x - sprite.getWidth() / 2, body.getPosition().y - sprite.getHeight() / 2);
sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);
sprite.draw(batch);
}
}
batch.end();
camera.position.set(playerBody.getPosition().x, playerBody.getPosition().y, 0);
camera.update();
}
#Override
public void dispose() {
world.dispose();
playersprite.getTexture().dispose();
groundsprite.getTexture().dispose();
}
#Override
public boolean keyDown(int keycode) {
switch (keycode) {
case Keys.DPAD_LEFT:
movement.x = -spriteSpeed;
break;
case Keys.DPAD_RIGHT:
movement.x = spriteSpeed;
break;
case Keys.DPAD_UP:
movement.y = spriteSpeed;
break;
case Keys.DPAD_DOWN:
movement.y = -spriteSpeed;
}
return true;
}
#Override
public boolean keyUp(int keycode) {
switch (keycode) {
case Keys.DPAD_LEFT:
case Keys.DPAD_RIGHT:
movement.x = 0;
break;
case Keys.DPAD_UP:
case Keys.DPAD_DOWN:
movement.y = 0;
}
return true;
}
You code is valid, the problem are numbers.
Mass of your body is big, 20480kg. Gravity is inverted because you ask for it:
world = new World(new Vector2(0, -1000f), true);
-1000, it is under 0 so it is inverted and big number (1000 on your "planet" and 9.8 on Earth) cause object to by less controllable.
Changes values:
float spriteSpeed = 60000;
...
world = new World(new Vector2(0, 9.8F), true);
...
fixtureDef.density = 1;
fixtureDef2.density = 1;
You will notice that now the body is controllable (it can even fly). Play with these numbers and learn some basic physics rules to have idea what are you doing.
The sprite and body position are synced, but are not aligned properly. Use this code to make the first step to make aligned texture with body.
sprite.setCenter(body.getPosition().x, body.getPosition().y);
groundsprite.setCenter(ground.getPosition().x, ground.getPosition().y);
But what about rotation
groundsprite.setOriginCenter();
sprite.setOriginCenter();
groundsprite.setRotation(ground.getAngle() * MathUtils.radDeg);
sprite.setRotation(body.getAngle() * MathUtils.radDeg);
This is simple as your object is single texture and we can simply align it at center, but as objects become more complicated code need to be changed.
Related
I've been working on this a while and feel so close! Should be easy, but I'm still new to this.
The skeleton hand data is being passed in as joints[KinectPV2.JointType_HandLeft] and can be accessed through joint.getX() and joint.getY(). I want to pass this data into the update function to replace mouseX and mouseY. I'm guessing I have to create global variables to access it within the update function or maybe I have to pass the skeleton data as parameters into the update function? How can I replace the mouse position data with the hand position?
import KinectPV2.*;
KinectPV2 kinect;
private class MyFluidData implements DwFluid2D.FluidData{
// update() is called during the fluid-simulation update step.
#Override
public void update(DwFluid2D fluid) {
float px, py, vx, vy, radius, vscale, temperature;
radius = 15;
vscale = 10;
px = width/2;
py = 50;
vx = 1 * +vscale;
vy = 1 * vscale;
radius = 40;
temperature = 1f;
fluid.addDensity(px, py, radius, 0.2f, 0.3f, 0.5f, 1.0f);
fluid.addTemperature(px, py, radius, temperature);
particles.spawn(fluid, px, py, radius, 100);
boolean mouse_input = mousePressed;
// add impulse: density + velocity, particles
if(mouse_input && mouseButton == LEFT){
radius = 15;
vscale = 15;
px = mouseX;
py = height-mouseY;
vx = (mouseX - pmouseX) * +vscale;
vy = (mouseY - pmouseY) * -vscale;
fluid.addDensity (px, py, radius, 0.25f, 0.0f, 0.1f, 1.0f);
fluid.addVelocity(px, py, radius, vx, vy);
particles.spawn(fluid, px, py, radius*2, 300);
}
// add impulse: density + temperature, particles
if(mouse_input && mouseButton == CENTER){
radius = 15;
vscale = 15;
px = mouseX;
py = height-mouseY;
temperature = 2f;
fluid.addDensity(px, py, radius, 0.25f, 0.0f, 0.1f, 1.0f);
fluid.addTemperature(px, py, radius, temperature);
particles.spawn(fluid, px, py, radius, 100);
}
// particles
if(mouse_input && mouseButton == RIGHT){
px = mouseX;
py = height - 1 - mouseY; // invert
radius = 50;
particles.spawn(fluid, px, py, radius, 300);
}
}
}
int viewport_w = 1280;
int viewport_h = 720;
int viewport_x = 230;
int viewport_y = 0;
int gui_w = 200;
int gui_x = 20;
int gui_y = 20;
int fluidgrid_scale = 3;
DwFluid2D fluid;
// render targets
PGraphics2D pg_fluid;
//texture-buffer, for adding obstacles
PGraphics2D pg_obstacles;
// custom particle system
MyParticleSystem particles;
// some state variables for the GUI/display
int BACKGROUND_COLOR = 0;
boolean UPDATE_FLUID = true;
boolean DISPLAY_FLUID_TEXTURES = false;
boolean DISPLAY_FLUID_VECTORS = false;
int DISPLAY_fluid_texture_mode = 0;
boolean DISPLAY_PARTICLES = true;
public void settings() {
size(viewport_w, viewport_h, P2D);
smooth(4);
}
public void setup() {
surface.setLocation(viewport_x, viewport_y);
// main library context
DwPixelFlow context = new DwPixelFlow(this);
context.print();
context.printGL();
// fluid simulation
fluid = new DwFluid2D(context, viewport_w, viewport_h, fluidgrid_scale);
// set some simulation parameters
fluid.param.dissipation_density = 0.999f;
fluid.param.dissipation_velocity = 0.99f;
fluid.param.dissipation_temperature = 0.80f;
fluid.param.vorticity = 0.10f;
fluid.param.timestep = 0.25f;
fluid.param.gridscale = 8f;
// interface for adding data to the fluid simulation
MyFluidData cb_fluid_data = new MyFluidData();
fluid.addCallback_FluiData(cb_fluid_data);
// pgraphics for fluid
pg_fluid = (PGraphics2D) createGraphics(viewport_w, viewport_h, P2D);
pg_fluid.smooth(4);
pg_fluid.beginDraw();
pg_fluid.background(BACKGROUND_COLOR);
pg_fluid.endDraw();
// pgraphics for obstacles
pg_obstacles = (PGraphics2D) createGraphics(viewport_w, viewport_h, P2D);
pg_obstacles.smooth(4);
pg_obstacles.beginDraw();
pg_obstacles.clear();
float radius;
radius = 200;
pg_obstacles.stroke(64);
pg_obstacles.strokeWeight(1);
pg_obstacles.fill(0);
pg_obstacles.rect(1*width/2f, 1*height/4f, radius, radius/2, 10);
pg_obstacles.stroke(64);
pg_obstacles.strokeWeight(1);
pg_obstacles.fill(0);
pg_obstacles.rect(1*width/3.5f, 1*height/2.5f, radius, radius/2, 10);
//// border-obstacle
//pg_obstacles.strokeWeight(20);
//pg_obstacles.stroke(64);
//pg_obstacles.noFill();
//pg_obstacles.rect(0, 0, pg_obstacles.width, pg_obstacles.height);
pg_obstacles.endDraw();
fluid.addObstacles(pg_obstacles);
// custom particle object
particles = new MyParticleSystem(context, 1024 * 1024);
kinect = new KinectPV2(this);
//Enables depth and Body tracking (mask image)
kinect.enableDepthMaskImg(true);
kinect.enableSkeletonDepthMap(true);
kinect.init();
background(0);
frameRate(60);
}
public void draw() {
PImage imgC = kinect.getDepthMaskImage();
image(imgC, 0, 0, 320, 240);
//get the skeletons as an Arraylist of KSkeletons
ArrayList<KSkeleton> skeletonArray = kinect.getSkeletonDepthMap();
//individual joints
for (int i = 0; i < skeletonArray.size(); i++) {
KSkeleton skeleton = (KSkeleton) skeletonArray.get(i);
//if the skeleton is being tracked compute the skleton joints
if (skeleton.isTracked()) {
KJoint[] joints = skeleton.getJoints();
color col = skeleton.getIndexColor();
fill(col);
stroke(col);
drawHandState(joints[KinectPV2.JointType_HandRight]);
drawHandState(joints[KinectPV2.JointType_HandLeft]);
}
}
// update simulation
if(UPDATE_FLUID){
fluid.addObstacles(pg_obstacles);
fluid.update();
particles.update(fluid);
}
// clear render target
pg_fluid.beginDraw();
pg_fluid.background(BACKGROUND_COLOR);
pg_fluid.endDraw();
// render fluid stuff
if(DISPLAY_FLUID_TEXTURES){
// render: density (0), temperature (1), pressure (2), velocity (3)
fluid.renderFluidTextures(pg_fluid, DISPLAY_fluid_texture_mode);
}
if(DISPLAY_FLUID_VECTORS){
// render: velocity vector field
fluid.renderFluidVectors(pg_fluid, 10);
}
if( DISPLAY_PARTICLES){
// render: particles; 0 ... points, 1 ...sprite texture, 2 ... dynamic points
particles.render(pg_fluid, BACKGROUND_COLOR);
}
// display
image(pg_fluid , 320, 0);
image(pg_obstacles, 320, 0);
// display number of particles as text
//String txt_num_particles = String.format("Particles %,d", particles.ALIVE_PARTICLES);
//fill(0, 0, 0, 220);
//noStroke();
//rect(10, height-10, 160, -30);
//fill(255,128,0);
//text(txt_num_particles, 20, height-20);
// info
//String txt_fps = String.format(getClass().getName()+ " [size %d/%d] [frame %d] [fps %6.2f]", fluid.fluid_w, fluid.fluid_h, fluid.simulation_step, frameRate);
//surface.setTitle(txt_fps);
}
//draw a ellipse depending on the hand state
void drawHandState(KJoint joint) {
noStroke();
handState(joint.getState());
//println(joint.getState());
pushMatrix();
translate(joint.getX(), joint.getY(), joint.getZ());
//println(joint.getX(), joint.getY(), joint.getZ());
ellipse(joint.getX(), joint.getY(), 70, 70);
popMatrix();
}
/*
Different hand state
KinectPV2.HandState_Open
KinectPV2.HandState_Closed
KinectPV2.HandState_Lasso
KinectPV2.HandState_NotTracked
*/
//Depending on the hand state change the color
void handState(int handState) {
switch(handState) {
case KinectPV2.HandState_Open:
fill(0, 255, 0);
break;
case KinectPV2.HandState_Closed:
fill(255, 0, 0);
break;
case KinectPV2.HandState_Lasso:
fill(0, 0, 255);
break;
case KinectPV2.HandState_NotTracked:
fill(100, 100, 100);
break;
}
}
I'm guessing I have to create global variables to access it within the update function or maybe I have to pass the skeleton data as parameters into the update function?
What happened when you tried those approaches?
Either approach sounds fine. You could store the variables in a sketch-level variable, set those variables from the kinect code, then use those variables in your drawing code. Or you could pass the variables as a parameter to the drawing code. Either should work fine. I'd probably go for the first approach because it sounds easier to me, but that's just my personal preference.
I suggest working in smaller chunks. Create a separate program that ignores the kinect for now. Create a hard-coded sketch-level variable that holds the same type of information you'd get from the kinect. Then write drawing code that uses that hard-coded variable to draw the frame. Get that working perfectly before you try adding the kinect code back in.
Then if you get stuck on a specific step, you can post a MCVE and we can go from there. Good luck.
This happen when my Body object touching with wall and I hold right key. So it is stop as long I hold right key.
When I release my right key it dropped. How to fix this problem?
Here my script:
World Definition:
this.world = new World(new Vector2(0, -9.8f), true);
Create ground body from tilemap:
//create body and fixture variables
BodyDef bdef = new BodyDef();
PolygonShape shape = new PolygonShape();
FixtureDef fdef = new FixtureDef();
Body body;
//create ground bodies/fixtures
for(MapObject object : tileMap.getLayers().get(2).getObjects().getByType(RectangleMapObject.class)){
rect = ((RectangleMapObject) object).getRectangle();
x = Static.toMeter(rect.getX() + rect.getWidth() / 2);
y = Static.toMeter(rect.getY() + rect.getHeight() / 2);
bdef.type = BodyDef.BodyType.StaticBody;
bdef.position.set(x, y);
body = world.createBody(bdef);
x = Static.toMeter(rect.getWidth() / 2);
y = Static.toMeter(rect.getHeight() / 2);
shape.setAsBox(x, y);
fdef.shape = shape;
fdef.filter.categoryBits = HookaHookaGame.GROUND_BIT;
body.createFixture(fdef);
}
Player body definition:
BodyDef bodyDef = new BodyDef();
bodyDef.position.set(Static.toMeter(128), Static.toMeter(HookaHookaGame.HEIGHT));
bodyDef.type = BodyDef.BodyType.DynamicBody;
body = world.createBody(bodyDef);
// Define mario shape
PolygonShape shape = new PolygonShape();
shape.setAsBox(Static.toMeter(32) / 2, Static.toMeter(32) / 2);
FixtureDef fixture = new FixtureDef();
fixture.shape = shape;
body.createFixture(fixture);
// Define foot shape
shape = new PolygonShape();
shape.setAsBox(Static.toMeter(32 / 4) / 2, Static.toMeter(32 / 4) / 2, new Vector2(0, Static.toMeter((-32 + 8) / 2)), 0);
fixture = new FixtureDef();
fixture.shape = shape;
// Create filter
fixture.filter.categoryBits = HookaHookaGame.MARIO_BIT;
fixture.filter.maskBits = HookaHookaGame.GROUND_BIT;
body.createFixture(fixture).setUserData(this);
My ContactListener here my beginContact() method:
int cDef;
Fixture a, b;
MySprite spriteA, spriteB;
a = contact.getFixtureA();
b = contact.getFixtureB();
cDef = a.getFilterData().categoryBits | b.getFilterData().categoryBits;
switch (cDef) {
case HookaHookaGame.GROUND_BIT | HookaHookaGame.MARIO_BIT:
System.out.println("Foot with ground");
if(a.getUserData() != null) {
spriteA = (MySprite) a.getUserData();
spriteA.onHit();
}
if(b.getUserData() != null) {
spriteB = (MySprite) b.getUserData();
spriteB.onHit();
}
break;
}
Handling user input:
private void handleInput() {
//control our player using immediate impulses
if (Gdx.input.isKeyPressed(Input.Keys.D))
player.moveRight();
else if (Gdx.input.isKeyPressed(Input.Keys.A))
player.moveLeft();
else
player.stopMove();
if (Gdx.input.isKeyPressed(Input.Keys.SPACE))
player.jump();
}
I read his tutorial and it seem he didn't do anything about it and it works fine. His source code GitHub
The problem is that your linear impulse is to strong.
You can test this with the Mario project, if you increase the impulse you will get stuck at walls, too.
To solve this either lower the impulse, lower the friction (but then you might need code that stops Mario) or instead of applying an linear impulse you could apply a torch and use a circle shape to let the player "roll".
None of these solutions are perfect, but you can try and see which one suits you most.
I have a game that generates bodies forever until the game ends. For some reason, the game will be nice and smooth at first, but then it will start to slow down and become choppy as you keep going while playing. Then, after you die and restart, the game repeats this process. I dispose all I can. Here is what pushes the player body:
//in main game class
private Vector2 movement = new Vector2();
sSpeed = 200000;
switch(button)
{
case Buttons.LEFT:
movement.y = sSpeed * 1.2f;
movement.x = sSpeed * 1.5f;
table.clear();
}
return false;
}
//in Level Generator class
public LevelGenerator(BodyDef bDef, float topEdge, float bottomEdge, float minGap, float maxGap, float w, float h, Sprite s, World world) {
this.bDef = bDef;
this.topEdge = topEdge;
this.bottomEdge = bottomEdge;
this.minGap = minGap;
this.maxGap = maxGap;
width = w;
height = h;
this.s = s;
this.world = world;
}
public void generate(float rightEdge) {
if(x + MathUtils.random(minGap, maxGap) > rightEdge) {
return;
}
x = rightEdge;
float y = MathUtils.random(topEdge - height * 2f, bottomEdge + height * 2f);
bDef = new BodyDef();
bDef.type = BodyType.DynamicBody;
PolygonShape ast = new PolygonShape();
ast.setAsBox(width, height, new Vector2(x + width, y + height), 0);
item = world.createBody(bDef);
Fixture fix = item.createFixture(ast, 0);
}
//in main game class
generator.generate(camera.position.x + camera.viewportWidth / 2 + 10);
generator = new LevelGenerator(ballD3, 120, -125, 58, 63, 12.5f, 12.5f, aSprite1 , world);
Sadly the best way to avoid this kind of memory leak is to track the objects yourself.
I've gotten around this problem by creating an array called bodies that holds all the bodies. Then you run a for loop something like this (sorry, JavaScript:)
_.each(bodies,function(body){
var pos = body.GetPosition();
if (pos.y > 500) world.destroyBody(body);
});
I am developing a game with LibGdx. I am using scene2d actors in my game.
I have 2 arrows with this body
private void creatBody() {
BodyDef bd = new BodyDef();
bd.position.set(getX(), getY());
bd.type = BodyType.DynamicBody;
FixtureDef fd = new FixtureDef();
fd.density = 15f;
fd.friction = 0.6f;
fd.restitution = 0.02f;
if (body != null)
removeBodySafely(body);
body = world.createBody(bd);
body.setTransform(body.getWorldCenter(), MathUtils.degreesToRadians
* getRotation());
GameScreen.shapeLoader.attachFixture(body, type, fd, 1);
}
public void draw(SpriteBatch batch, float parentAlpha) {
setRotation(MathUtils.radiansToDegrees * body.getAngle());
setPosition(body.getPosition().x, body.getPosition().y);
TextureRegion keyFrame = GameScreen.getAtlas("arrows").findRegion(type);
batch.draw(keyFrame, getX(), getY(), 0, 0, getWidth(), getHeight(),
1, 1, getRotation());
}
Picture of the body
but, when I drop one arrow above another they overlap instead of colliding.
From description of body editor I found that there is a red mark which is the reference point.
How do I make the arrows collide?
I found the answer
GameScreen.shapeLoader.attachFixture(body, type, fd, 90);
where 90 is the width of arrow.
So I understand the concept. The idea is that box2d more or less works in meters, so you need to do a conversion from pixels to it. Makes sense. I was following the tutorial/intro to box2d here. It mentions to do the conversion and gives you some example amounts to use. Now, that's all well and good, but I find when I'm using such techniques, the debugger box doesn't seem to render where they should. The collision does work as expected however.
In my GameScreen class, here's how I initialize the ground:
ground = new BodyDef();
// set the position half way up the ground
ground.position.set(0,16 * GameScreen.WORLD_TO_BOX);
groundBody = world.createBody(ground);
groundShape = new PolygonShape();
// make the height 16px so it doubles to 32
groundShape.setAsBox(Gdx.graphics.getWidth() * GameScreen.WORLD_TO_BOX, 16.0f * GameScreen.WORLD_TO_BOX);
groundBody.createFixture(groundShape, 0.0f);
My render method in that screen is like so:
public void render(float delta) {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
stateTime += Gdx.graphics.getDeltaTime();
batch.begin();
batch.draw(background, 0, Gdx.graphics.getHeight() - 512, 512, 512);
batch.draw(trailingBackground, 512, Gdx.graphics.getHeight() - 512);
int heightToCover = Gdx.graphics.getHeight() - 512;
int widthToCover = Gdx.graphics.getWidth();
for(int w = 0; w < widthToCover; w += 32) {
for(int h = 0; h < heightToCover; h += 32) {
batch.draw(lightBackgroundTile, w, h, 32, 32);
}
}
player.update();
player.render(stateTime, batch);
batch.end();
levels.get(currentLevel).render(camera);
// physics updates
world.step(1/60f, 6, 2);
debugRenderer.render(world, camera.combined);
}
Here's the constructor of the player class, so you can see how im setting up its collision box2d objects. I also pasted the update method which is called in the above render loop to adjust the sprites position.
public Player(int x, int y, World world) {
super();
playerTexture = new Texture(Gdx.files.internal("assets/hero.png"));
init(x, y, 128, 128, playerTexture, false, world);
bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(x * GameScreen.WORLD_TO_BOX, y * GameScreen.WORLD_TO_BOX);
body = getWorld().createBody(bodyDef);
collisionBox = new PolygonShape();
collisionBox.setAsBox(32 * GameScreen.WORLD_TO_BOX, 64 * GameScreen.WORLD_TO_BOX, new Vector2(64 * GameScreen.WORLD_TO_BOX, 64 * GameScreen.WORLD_TO_BOX), 0.0f);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = collisionBox;
fixtureDef.density = 10f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 0f;
body.createFixture(fixtureDef);
collisionBox.dispose();
addFrame(0, 0, 128, 128);
}
public void update() {
this.setX((int) ((body.getPosition().x) * GameScreen.BOX_TO_WORLD));
this.setY((int) ((body.getPosition().y) * GameScreen.BOX_TO_WORLD));
}
Now, when I remove the multiplication of those static floats in the various calculations, sizes, etc, the collision remains correct and the debugger box shows up. However passing the raw pixels to box2d feels wrong. Is there something I'm missing here as to why the debugging boxes don't show up as is?
I do like you are doing but might be a little different.
Might be a little overkill but here's my jucl port
jucl/Android - pastebin
Then I just have utility classes ie:
public class Pixel {
public static float toMeter(float pixels) {
return (float)LengthConversions.Pixel2SIf(pixels);
}
public static Vector2 toMeter(Vector2 vecPixel) {
return new Vector2(Pixel.toMeter(vecPixel.x), Pixel.toMeter(vecPixel.y));
}
}
public class Meter {
public static final float METERS_PER_PIXEL = (float) LengthConversions.SI_PIXEL;
public static float toPixel(float meter) {
return (float)LengthConversions.SI2Pixelf(meter);
}
}
In my initialize:
int graphicsWidth = Gdx.graphics.getWidth();
int graphicsHeight = Gdx.graphics.getHeight();
CAMERA_WIDTH_METERS = Pixel.toMeter(graphicsWidth);
CAMERA_HEIGHT_METERS = Pixel.toMeter(graphicsHeight);
Then in my game classes (like your Player class). In my case it was a pinball game so i have Flipper, Ball, Bumper, etc. I have a #Override render() method where I sync up the sprite with the physics body.
Here's an example file..sry it's messy but might be helpful.
Pinball Engine Class file