com.badlogic.gdx.physics.box2d.Body.getMass()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(7.6k)|赞(0)|评价(0)|浏览(92)

本文整理了Java中com.badlogic.gdx.physics.box2d.Body.getMass()方法的一些代码示例,展示了Body.getMass()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Body.getMass()方法的具体详情如下:
包路径:com.badlogic.gdx.physics.box2d.Body
类名称:Body
方法名:getMass

Body.getMass介绍

[英]Get the total mass of the body.
[中]得到物体的总质量。

代码示例

代码示例来源:origin: libgdx/libgdx

@Override
public boolean touchDown (int x, int y, int pointer, int newParam) {
  // translate the mouse coordinates to world coordinates
  testPoint.set(x, y, 0);
  camera.unproject(testPoint);
  // ask the world which bodies are within the given
  // bounding box around the mouse pointer
  hitBody = null;
  world.QueryAABB(callback, testPoint.x - 0.1f, testPoint.y - 0.1f, testPoint.x + 0.1f, testPoint.y + 0.1f);
  // if we hit something we create a new mouse joint
  // and attach it to the hit body.
  if (hitBody != null) {
    MouseJointDef def = new MouseJointDef();
    def.bodyA = groundBody;
    def.bodyB = hitBody;
    def.collideConnected = true;
    def.target.set(testPoint.x, testPoint.y);
    def.maxForce = 1000.0f * hitBody.getMass();
    mouseJoint = (MouseJoint)world.createJoint(def);
    hitBody.setAwake(true);
  } else {
    for (Body box : boxes)
      world.destroyBody(box);
    boxes.clear();
    createBoxes();
  }
  return false;
}

代码示例来源:origin: libgdx/libgdx

@Override
public boolean touchDown (int x, int y, int pointer, int button) {
  // translate the mouse coordinates to world coordinates
  camera.unproject(testPoint.set(x, y, 0));
  // ask the world which bodies are within the given
  // bounding box around the mouse pointer
  hitBody = null;
  world.QueryAABB(callback, testPoint.x - 0.0001f, testPoint.y - 0.0001f, testPoint.x + 0.0001f, testPoint.y + 0.0001f);
  if (hitBody == groundBody) hitBody = null;
  // ignore kinematic bodies, they don't work with the mouse joint
  if (hitBody != null && hitBody.getType() == BodyType.KinematicBody) return false;
  // if we hit something we create a new mouse joint
  // and attach it to the hit body.
  if (hitBody != null) {
    MouseJointDef def = new MouseJointDef();
    def.bodyA = groundBody;
    def.bodyB = hitBody;
    def.collideConnected = true;
    def.target.set(testPoint.x, testPoint.y);
    def.maxForce = 1000.0f * hitBody.getMass();
    mouseJoint = (MouseJoint)world.createJoint(def);
    hitBody.setAwake(true);
  }
  return false;
}

代码示例来源:origin: libgdx/libgdx

float mass = body.getMass();

代码示例来源:origin: dsaltares/libgdx-cookbook

@Override
public boolean touchDown (int x, int y, int pointer, int button) {
  // Screen coordinates to world coordinates
  viewport.getCamera().unproject(point.set(x, y, 0));
  
  if (button == Input.Buttons.LEFT) {
    float force = 100f * player1.getBody().getMass();
    Vector2 target = new Vector2(point.x, point.y);
    Vector2 direction = target.sub(player1.getBody().getPosition());
    player1.getBody().applyForceToCenter(direction.scl(force), true);
  }
  return false;
}

代码示例来源:origin: dsaltares/libgdx-cookbook

@Override
public boolean touchDown (int x, int y, int pointer, int button) {
  // Mouse coordinates to world coordinates
  viewport.getCamera().unproject(point.set(x, y, 0));
  
  if(button == Input.Buttons.RIGHT) {
    hitBody = null;
    
    // Check if a interactive body has been right-clicked
    world.QueryAABB(callback, point.x - 0.0001f, point.y - 0.0001f, point.x + 0.0001f, point.y + 0.0001f);
    
    if (hitBody == groundBody) hitBody = null;
    
    // if we hit an interactive body we create a new mouse joint
    // and attach it to the hit body.
    if (hitBody != null) {
      MouseJointDef def = new MouseJointDef();
      def.bodyA = groundBody;
      def.bodyB = hitBody;
      def.collideConnected = true;
      def.target.set(point.x, point.y);
      def.maxForce = 1000.0f * hitBody.getMass();
      mouseJoint = (MouseJoint)world.createJoint(def);
      hitBody.setAwake(true);
    }
  }
  return false;
}

代码示例来源:origin: lycying/c2d-engine

@Override
public boolean touchDown (int x, int y, int pointer, int newParam) {
  if(world == null)return false;
  // translate the mouse coordinates to world coordinates
  testPoint.set(Engine.screenToWorld(x, y).scl(1/Box2dObject.RADIO));
  // ask the world which bodies are within the given
  // bounding box around the mouse pointer
  hitBody = null;
  world.QueryAABB(callback, testPoint.x - 0.1f, testPoint.y - 0.1f, testPoint.x + 0.1f, testPoint.y + 0.1f);
  // if we hit something we create a new mouse joint
  // and attach it to the hit body.
  if (hitBody != null) {
    MouseJointDef def = new MouseJointDef();
    def.bodyA = fixBody;
    def.bodyB = hitBody;
    def.collideConnected = true;
    def.target.set(testPoint.x, testPoint.y);
    def.maxForce = 1000.0f * hitBody.getMass();
    mouseJoint = (MouseJoint)world.createJoint(def);
    hitBody.setAwake(true);
    
    return true;
  } 
  return false;
}

代码示例来源:origin: narfman0/GDXWorld

@Override public boolean touchUp(int x, int y, int arg2, int arg3) {
  Vector3 coordinates = new Vector3(x,y,0);
  camera.unproject(coordinates);
  if(lastTouchPolygon != null){
    Vector2 impulse = new Vector2(coordinates.x, coordinates.y).
        sub(lastTouchCoordinates).scl(lastTouchPolygon.getMass());
    Log.log("LiveMode.touchUp", "applying impulse: " + 
        impulse + " on body: " + lastTouchPolygon.getPosition());
    lastTouchPolygon.applyLinearImpulse(impulse, 
        lastTouchPolygonLocalCoordinates.add(lastTouchPolygon.getPosition()),true);
    lastTouchCoordinates = null;
    lastTouchPolygon = null;
    lastTouchPolygonLocalCoordinates = null;
  }
  return false;
}

代码示例来源:origin: dsaltares/libgdx-cookbook

def.collideConnected = true;
def.target.set(point.x, point.y);
def.maxForce = 1000.0f * hitBody.getMass();

代码示例来源:origin: yichen0831/Bomberman_libGdx

state.setCurrentState("walking_left");
if (body.getLinearVelocity().x > -enemy.getSpeed()) {
  body.applyLinearImpulse(new Vector2(-enemy.getSpeed() * body.getMass(), 0), body.getWorldCenter(), true);
state.setCurrentState("walking_right");
if (body.getLinearVelocity().x < enemy.getSpeed()) {
  body.applyLinearImpulse(new Vector2(enemy.getSpeed() * body.getMass(), 0), body.getWorldCenter(), true);
state.setCurrentState("walking_up");
if (body.getLinearVelocity().y < enemy.getSpeed()) {
  body.applyLinearImpulse(new Vector2(0, enemy.getSpeed() * body.getMass()), body.getWorldCenter(), true);
state.setCurrentState("walking_down");
if (body.getLinearVelocity().y > -enemy.getSpeed()) {
  body.applyLinearImpulse(new Vector2(0, -enemy.getSpeed() * body.getMass()), body.getWorldCenter(), true);

代码示例来源:origin: yichen0831/Bomberman_libGdx

state.setCurrentState("walking_left");
if (body.getLinearVelocity().x > -enemy.getSpeed()) {
  body.applyLinearImpulse(new Vector2(-enemy.getSpeed() * body.getMass(), 0), body.getWorldCenter(), true);
state.setCurrentState("walking_right");
if (body.getLinearVelocity().x < enemy.getSpeed()) {
  body.applyLinearImpulse(new Vector2(enemy.getSpeed() * body.getMass(), 0), body.getWorldCenter(), true);
state.setCurrentState("walking_up");
if (body.getLinearVelocity().y < enemy.getSpeed()) {
  body.applyLinearImpulse(new Vector2(0, enemy.getSpeed() * body.getMass()), body.getWorldCenter(), true);
state.setCurrentState("walking_down");
if (body.getLinearVelocity().y > -enemy.getSpeed()) {
  body.applyLinearImpulse(new Vector2(0, -enemy.getSpeed() * body.getMass()), body.getWorldCenter(), true);

代码示例来源:origin: yichen0831/Bomberman_libGdx

if (player.invincible || !hitBombVertical(body, fromV.set(body.getPosition()), toV.set(body.getPosition().x, body.getPosition().y + 0.5f))) {
  if (Math.abs(linearVelocity.y) < maxSpeed) {
    body.applyLinearImpulse(new Vector2(0, player.acceleration * body.getMass()), body.getWorldCenter(), true);
if (player.invincible || !hitBombVertical(body, fromV.set(body.getPosition()), toV.set(body.getPosition().x, body.getPosition().y - 0.5f))) {
  if (Math.abs(linearVelocity.y) < maxSpeed) {
    body.applyLinearImpulse(new Vector2(0, -player.acceleration * body.getMass()), body.getWorldCenter(), true);
if (player.invincible || !hitBombHorizontal(body, fromV.set(body.getPosition()), toV.set(body.getPosition().x - 0.5f, body.getPosition().y))) {
  if (Math.abs(linearVelocity.x) < maxSpeed) {
    body.applyLinearImpulse(new Vector2(-player.acceleration * body.getMass(), 0), body.getWorldCenter(), true);
if (player.invincible || !hitBombHorizontal(body, fromV.set(body.getPosition()), toV.set(body.getPosition().x + 0.5f, body.getPosition().y))) {
  if (Math.abs(linearVelocity.x) < maxSpeed) {
    body.applyLinearImpulse(new Vector2(player.acceleration * body.getMass(), 0), body.getWorldCenter(), true);

相关文章