@Override public void render () { if (m_platform.getType() == BodyType.KinematicBody) { Vector2 p = m_platform.getTransform().getPosition(); Vector2 v = m_platform.getLinearVelocity(); if ((p.x < -10 && v.x < 0) || (p.x > 10 && v.x > 0)) { v.x = -v.x; m_platform.setLinearVelocity(v); } } super.render(); // if (renderer.batch != null) { // renderer.batch.begin(); // // renderer.batch.drawText(renderer.font, "Keys: (d) dynamic, (s) static, (k) kinematic", 0, Gdx.app.getGraphics() // // .getHeight(), Color.WHITE); // renderer.batch.end(); // } } }
protected void renderBody (Body body) { Transform transform = body.getTransform(); for (Fixture fixture : body.getFixtureList()) { if (drawBodies) { drawShape(fixture, transform, getColorByBody(body)); if (drawVelocities) { Vector2 position = body.getPosition(); drawSegment(position, body.getLinearVelocity().add(position), VELOCITY_COLOR); } } if (drawAABBs) { drawAABB(fixture, transform); } } }
renderer.render(world, cam.combined); Vector2 vel = player.getLinearVelocity(); Vector2 pos = player.getPosition(); boolean grounded = isPlayerGrounded(Gdx.graphics.getDeltaTime()); if (grounded) { player.setLinearVelocity(vel.x, 0); System.out.println("jump before: " + player.getLinearVelocity()); player.setTransform(pos.x, pos.y + 0.01f, 0); player.applyLinearImpulse(0, 40, pos.x, pos.y, true); System.out.println("jump, " + player.getLinearVelocity());
public Vector2 getLinearVelocity() { return body.getLinearVelocity(); }
public State getState(){ //Test to Box2D for velocity on the X and Y-Axis //if mario is going positive in Y-Axis he is jumping... or if he just jumped and is falling remain in jump state if(marioIsDead) return State.DEAD; else if(runGrowAnimation) return State.GROWING; else if((b2body.getLinearVelocity().y > 0 && currentState == State.JUMPING) || (b2body.getLinearVelocity().y < 0 && previousState == State.JUMPING)) return State.JUMPING; //if negative in Y-Axis mario is falling else if(b2body.getLinearVelocity().y < 0) return State.FALLING; //if mario is positive or negative in the X axis he is running else if(b2body.getLinearVelocity().x != 0) return State.RUNNING; //if none of these return then he must be standing else return State.STANDING; }
private void handleImpactFeedback () { // process impact feedback while (impacts > 0) { impacts--; // feed back the result to the car simulator carDesc.velocity_wc.set(body.getLinearVelocity()); carDesc.angularvelocity = -body.getAngularVelocity(); } } }
public void update(float dt){ stateTime += dt; setRegion(fireAnimation.getKeyFrame(stateTime, true)); setPosition(b2body.getPosition().x - getWidth() / 2, b2body.getPosition().y - getHeight() / 2); if((stateTime > 3 || setToDestroy) && !destroyed) { world.destroyBody(b2body); destroyed = true; } if(b2body.getLinearVelocity().y > 2f) b2body.setLinearVelocity(b2body.getLinearVelocity().x, 2f); if((fireRight && b2body.getLinearVelocity().x < 0) || (!fireRight && b2body.getLinearVelocity().x > 0)) setToDestroy(); }
public void handleInput(float dt){ //control our player using immediate impulses if(player.currentState != Mario.State.DEAD) { if (Gdx.input.isKeyJustPressed(Input.Keys.UP)) player.jump(); if (Gdx.input.isKeyPressed(Input.Keys.RIGHT) && player.b2body.getLinearVelocity().x <= 2) player.b2body.applyLinearImpulse(new Vector2(0.1f, 0), player.b2body.getWorldCenter(), true); if (Gdx.input.isKeyPressed(Input.Keys.LEFT) && player.b2body.getLinearVelocity().x >= -2) player.b2body.applyLinearImpulse(new Vector2(-0.1f, 0), player.b2body.getWorldCenter(), true); if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) player.fire(); } }
@Override public void update(float dt) { super.update(dt); setPosition(body.getPosition().x - getWidth() / 2, body.getPosition().y - getHeight() / 2); velocity.y = body.getLinearVelocity().y; body.setLinearVelocity(velocity); } }
if((b2body.getLinearVelocity().x < 0 || !runningRight) && !region.isFlipX()){ region.flip(true, false); runningRight = false; else if((b2body.getLinearVelocity().x > 0 || runningRight) && region.isFlipX()){ region.flip(true, false); runningRight = true;
protected void renderBody (Body body) { Transform transform = body.getTransform(); for (Fixture fixture : body.getFixtureList()) { if (drawBodies) { drawShape(fixture, transform, getColorByBody(body)); if (drawVelocities) { Vector2 position = body.getPosition(); drawSegment(position, body.getLinearVelocity().add(position), VELOCITY_COLOR); } } if (drawAABBs) { drawAABB(fixture, transform); } } }
case WALKING_LEFT: state.setCurrentState("walking_left"); if (body.getLinearVelocity().x > -enemy.getSpeed()) { body.applyLinearImpulse(new Vector2(-enemy.getSpeed() * body.getMass(), 0), body.getWorldCenter(), true); case WALKING_RIGHT: state.setCurrentState("walking_right"); if (body.getLinearVelocity().x < enemy.getSpeed()) { body.applyLinearImpulse(new Vector2(enemy.getSpeed() * body.getMass(), 0), body.getWorldCenter(), true); case WALKING_UP: state.setCurrentState("walking_up"); if (body.getLinearVelocity().y < enemy.getSpeed()) { body.applyLinearImpulse(new Vector2(0, enemy.getSpeed() * body.getMass()), body.getWorldCenter(), true); default: state.setCurrentState("walking_down"); if (body.getLinearVelocity().y > -enemy.getSpeed()) { body.applyLinearImpulse(new Vector2(0, -enemy.getSpeed() * body.getMass()), body.getWorldCenter(), true);
case WALKING_LEFT: state.setCurrentState("walking_left"); if (body.getLinearVelocity().x > -enemy.getSpeed()) { body.applyLinearImpulse(new Vector2(-enemy.getSpeed() * body.getMass(), 0), body.getWorldCenter(), true); case WALKING_RIGHT: state.setCurrentState("walking_right"); if (body.getLinearVelocity().x < enemy.getSpeed()) { body.applyLinearImpulse(new Vector2(enemy.getSpeed() * body.getMass(), 0), body.getWorldCenter(), true); case WALKING_UP: state.setCurrentState("walking_up"); if (body.getLinearVelocity().y < enemy.getSpeed()) { body.applyLinearImpulse(new Vector2(0, enemy.getSpeed() * body.getMass()), body.getWorldCenter(), true); default: state.setCurrentState("walking_down"); if (body.getLinearVelocity().y > -enemy.getSpeed()) { body.applyLinearImpulse(new Vector2(0, -enemy.getSpeed() * body.getMass()), body.getWorldCenter(), true);
Vector2 linearVelocity = body.getLinearVelocity();
toVector.nor(); if (body.getLinearVelocity().len2() < enemy.getSpeed() * enemy.getSpeed()) { body.applyLinearImpulse(toVector.scl(enemy.getSpeed()), body.getWorldCenter(), true);