I recently encountered a problem when using different sizes (therefore masses).
I am developing a breakout-like game. I need different sized balls that move in the same velocity as if they are equally sized. In other words, they should be dynamic bodies but “ignore mass” when applying an impulse on them. Is there a way to accomplish this?
That is the Ball class:
[spoiler]
public class Ball extends Entity {
private Texture texture;
private Body body;
public Body getBody() { return body; }
private float radius;
public float getRadius() { return radius; }
private Directions direction = Directions.STOP;
public void setDirection(Directions left) { direction = left; }
private boolean started = false;
public void setStarted(boolean started) { this.started = started; }
public boolean hasStarted() { return started; }
public Ball(float x, float y) {
super(x,y);
radius = 10/pixel_to_meter;
this.createBody();
}
public Ball(float x, float y, float radius) {
super(x,y);
this.radius = radius;
this.createBody();
}
@Override
public void createBody() {
texture = new Texture(Gdx.files.internal("data/sprites/ballGrey.png"));
body = BodyFactory.createCircleBody(
new Vector2(x, y), radius, BodyType.DynamicBody, FixtureType.CUSTOM);
BodyFactory.createFixture(
body, BodyFactory.createCircleShape(radius), new float[] {1f,0f,1f}, false, (short)1);
body.setFixedRotation(false);
body.setUserData(this);
}
@Override
public void render(SpriteBatch spriteBatch) {
spriteBatch.begin();
spriteBatch.draw(texture, (body.getPosition().x-radius)*pixel_to_meter, (body.getPosition().y-radius)*pixel_to_meter);
spriteBatch.end();
}
public void move() {
if(!started) {
switch (direction) {
case LEFT:
body.setLinearVelocity(-Paddle.VELOCITY,0);
break;
case RIGHT:
body.setLinearVelocity(Paddle.VELOCITY,0);
break;
case STOP:
body.setLinearVelocity(0,0);
break;
}
}
}
public void start() {
body.setLinearVelocity(0,0);
Random generator = new Random();
if(generator.nextBoolean()) {
body.applyLinearImpulse(new Vector2(-0.06f,0.06f), body.getPosition());
} else {
body.applyLinearImpulse(new Vector2(0.06f,0.06f), body.getPosition());
}
System.out.println(body.getLinearVelocity().x + ", " + body.getLinearVelocity().y);
}
public void destroy() {
World world = BlockGame.getWorld();
world.destroyBody(body);
this.started = false;
}
public void lockMovement() {
body.setLinearVelocity(0,0);
}
}
[/spoiler]
The BodyFactory class is just a wrapper for simplifying the process of creating bodies (If needed i will edit it here too :)).
PS: Is there a way to spoiler the code? I tried it with the [.spoiler] tags but that didn’t do it…