Hi guys,
First i wanna say hello, because this is my first post on this forum. Before i created this topic i was trying figure out everything on my own. Of course I read whole manual. Problem is that this my first game with bo2D so i lack the knowledge 🙁
on the image below u can check what im trying achieve:

Green platform reach maximum height when there is no other boxes on it. the more boxes on the top the lower position have platform and vice-versa. Most important - green platform ca move only on y-axis.
right now i'm using DynamicBody for the platform but as soon as any box hit it - platform falls down really fast (maybe i should use KineticBody instead?). i'm sure im doing some noob error but i don't know where. beow u can see my code:
I would be very grateful for any help.
First i wanna say hello, because this is my first post on this forum. Before i created this topic i was trying figure out everything on my own. Of course I read whole manual. Problem is that this my first game with bo2D so i lack the knowledge 🙁
on the image below u can check what im trying achieve:

Green platform reach maximum height when there is no other boxes on it. the more boxes on the top the lower position have platform and vice-versa. Most important - green platform ca move only on y-axis.
right now i'm using DynamicBody for the platform but as soon as any box hit it - platform falls down really fast (maybe i should use KineticBody instead?). i'm sure im doing some noob error but i don't know where. beow u can see my code:
Code:
public class GameWorld
{
private OrthographicCamera cam;
private Vector2 startOfRay;
private Vector2 endOfRay;
private Body platform;
private Box2DDebugRenderer renderer;
private HovercarRayCastClosestCallback callback;
private float targetHeight = 40;
private float springConst = 50;
public GameWorld()
{
this.box2dWorld = new World(new Vector2(0.0f, -100.0f), true); //gravity -100
this.cam = new OrthographicCamera(Settings.WORLD_WIDTH, Settings.WORLD_HEIGHT);
this.renderer = new Box2DDebugRenderer();
cam.position.set(Settings.WORLD_WIDTH / 2, Settings.WORLD_HEIGHT / 2, 0);
cam.update();
floor = createEdge(BodyType.StaticBody, 0, 1, Settings.WORLD_WIDTH, 1, 0);
weightScale = createPlatform ();
callback = new HovercarRayCastClosestCallback();
}
public void update(float delta)
{
cam.update();
cam.apply(Gdx.gl10);
Vector2 startOfRay = platform.getPosition();
Vector2 endOfRay = platform.getWorldPoint(new Vector2(0, -20));
box2dWorld.rayCast(callback, startOfRay, endOfRay);
weightScale.applyForce(new Vector2(0, 100.0f * weightScale.getMass()), weightScale.getPosition());
if(callback.mHit)
{
float distanceAboveGround = (startOfRay.sub(callback.mPoint)).len();
if(distanceAboveGround < targetHeight){
float distanceAwayFromTargetHeight = targetHeight - distanceAboveGround;
platform.applyForce(new Vector2(0, springConst * distanceAwayFromTargetHeight),
weightScale.getWorldCenter());
}
}
box2dWorld.step(delta, 3, 2);
renderer.render(box2dWorld, cam.combined);
}
private Body createplatform ()
{
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.fixedRotation = true;
bodyDef.position.set(306, 40);
Body body = box2dWorld.createBody(bodyDef);
PolygonShape poly = new PolygonShape();
poly.setAsBox(24.0f, 1.0f);
body.createFixture(poly, 1);
poly.dispose();
return body;
}
private Body createEdge(BodyType type, float x1, float y1, float x2, float y2, float density)
{
BodyDef def = new BodyDef();
def.type = type;
Body box = box2dWorld.createBody(def);
EdgeShape edge = new EdgeShape();
edge.set(new Vector2(x1, y1), new Vector2(x2, y2));
box.createFixture(edge, density);
edge.dispose();
return box;
}
}
{
private OrthographicCamera cam;
private Vector2 startOfRay;
private Vector2 endOfRay;
private Body platform;
private Box2DDebugRenderer renderer;
private HovercarRayCastClosestCallback callback;
private float targetHeight = 40;
private float springConst = 50;
public GameWorld()
{
this.box2dWorld = new World(new Vector2(0.0f, -100.0f), true); //gravity -100
this.cam = new OrthographicCamera(Settings.WORLD_WIDTH, Settings.WORLD_HEIGHT);
this.renderer = new Box2DDebugRenderer();
cam.position.set(Settings.WORLD_WIDTH / 2, Settings.WORLD_HEIGHT / 2, 0);
cam.update();
floor = createEdge(BodyType.StaticBody, 0, 1, Settings.WORLD_WIDTH, 1, 0);
weightScale = createPlatform ();
callback = new HovercarRayCastClosestCallback();
}
public void update(float delta)
{
cam.update();
cam.apply(Gdx.gl10);
Vector2 startOfRay = platform.getPosition();
Vector2 endOfRay = platform.getWorldPoint(new Vector2(0, -20));
box2dWorld.rayCast(callback, startOfRay, endOfRay);
weightScale.applyForce(new Vector2(0, 100.0f * weightScale.getMass()), weightScale.getPosition());
if(callback.mHit)
{
float distanceAboveGround = (startOfRay.sub(callback.mPoint)).len();
if(distanceAboveGround < targetHeight){
float distanceAwayFromTargetHeight = targetHeight - distanceAboveGround;
platform.applyForce(new Vector2(0, springConst * distanceAwayFromTargetHeight),
weightScale.getWorldCenter());
}
}
box2dWorld.step(delta, 3, 2);
renderer.render(box2dWorld, cam.combined);
}
private Body createplatform ()
{
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.fixedRotation = true;
bodyDef.position.set(306, 40);
Body body = box2dWorld.createBody(bodyDef);
PolygonShape poly = new PolygonShape();
poly.setAsBox(24.0f, 1.0f);
body.createFixture(poly, 1);
poly.dispose();
return body;
}
private Body createEdge(BodyType type, float x1, float y1, float x2, float y2, float density)
{
BodyDef def = new BodyDef();
def.type = type;
Body box = box2dWorld.createBody(def);
EdgeShape edge = new EdgeShape();
edge.set(new Vector2(x1, y1), new Vector2(x2, y2));
box.createFixture(edge, density);
edge.dispose();
return box;
}
}
I would be very grateful for any help.