Android Open Source - hackfmi-ragdoll-physics Physics Service






From Project

Back to project page hackfmi-ragdoll-physics.

License

The source code is released under:

GNU General Public License

If you think the Android project hackfmi-ragdoll-physics listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.midtownmadness.bubblecombat.physics;
// w w w.j a  v a2 s  .com
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import org.jbox2d.callbacks.ContactImpulse;
import org.jbox2d.callbacks.ContactListener;
import org.jbox2d.collision.Manifold;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;
import org.jbox2d.dynamics.contacts.Contact;

import com.midtownmadness.bubblecombat.game.GameObject;
import com.midtownmadness.bubblecombat.multiplay.MultiplayEvent;

public class PhysicsService implements ContactListener {

  private static final Vec2 GRAVITY = new Vec2(0, 0);

  private static final int VELOCITY_ITERATIONS = 4;

  private static final int POSITION_ITERATIONS = 4;

  private World world;
  private BlockingQueue<PhysicsRequest> requestQueue = new ArrayBlockingQueue<PhysicsRequest>(
      128);

  private CollisionListener collisionListener;

  public void doPhysicsStep() {
    while (!requestQueue.isEmpty()) {
      PhysicsRequest request = requestQueue.poll();
      request.applyRequest(world);
    }
    world.step(0.016666666f / 3, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
    world.step(0.016666666f / 3, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
    world.step(0.016666666f / 3, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
    world.clearForces();
  }

  public void startService() {
    world = new World(GRAVITY);
    world.setContinuousPhysics(true);
    world.setContactListener(this);
  }

  public void createBody(BodyCreationRequest request) {
    requestQueue.add(request);
  }

  public void createBody(BodyDef bodyDef, FixtureDef fixtureDef) {
    BodyCreationRequest entry = new BodyCreationRequest(bodyDef, fixtureDef);
    requestQueue.offer(entry);
  }

  public void applyMovement(Body body, Vec2 movement) {
    requestQueue.add(new MovementRequest(body, movement, null));
  }

  @Override
  public void beginContact(Contact contact) {
    BodyUserData buda1 = (BodyUserData) contact.getFixtureA().getBody()
        .getUserData();
    BodyUserData buda2 = (BodyUserData) contact.getFixtureB().getBody()
        .getUserData();

    GameObject gameObj1 = buda1.gameObject;
    GameObject gameObj2 = buda2.gameObject;

    dispatchCollision(gameObj1, gameObj2, contact);
  }

  public void setCollisionListener(CollisionListener listener) {
    collisionListener = listener;
  }

  private void dispatchCollision(GameObject gameObj1, GameObject gameObj2,
      Contact contact) {
    if (collisionListener != null)
      collisionListener.collision(gameObj1, gameObj2, contact);
  }

  @Override
  public void endContact(Contact contact) {

  }

  @Override
  public void postSolve(Contact arg0, ContactImpulse arg1) {
    // Nothing
  }

  @Override
  public void preSolve(Contact arg0, Manifold arg1) {
    // Nothing
  }

  public void applyState(Body playerBody, MultiplayEvent e) {
    requestQueue.add(new MovementStateRequest(playerBody, new Vec2(e.dx,
        e.dy), new Vec2(e.x, e.y), new Vec2(e.vx, e.vy), null));
  }
}




Java Source Code List

com.midtownmadness.bubblecombar.listeners.GameRoomListener.java
com.midtownmadness.bubblecombar.model.GameModel.java
com.midtownmadness.bubblecombar.model.GamesAdapter.java
com.midtownmadness.bubblecombar.model.MockAdapter.java
com.midtownmadness.bubblecombat.BaseActivity.java
com.midtownmadness.bubblecombat.BluetoothGamesAdapter.java
com.midtownmadness.bubblecombat.GameActivity.java
com.midtownmadness.bubblecombat.GameView.java
com.midtownmadness.bubblecombat.GlobalContext.java
com.midtownmadness.bubblecombat.MenuActivity.java
com.midtownmadness.bubblecombat.MockMultiplayerGame.java
com.midtownmadness.bubblecombat.Settings.java
com.midtownmadness.bubblecombat.game.DrawThread.java
com.midtownmadness.bubblecombat.game.GameObject.java
com.midtownmadness.bubblecombat.game.GameWallObject.java
com.midtownmadness.bubblecombat.game.LevelObject.java
com.midtownmadness.bubblecombat.game.PlayerObject.java
com.midtownmadness.bubblecombat.multiplay.BaseStrategy.java
com.midtownmadness.bubblecombat.multiplay.BluetoothConnectException.java
com.midtownmadness.bubblecombat.multiplay.BluetoothMessage.java
com.midtownmadness.bubblecombat.multiplay.Callback.java
com.midtownmadness.bubblecombat.multiplay.ClientStrategy.java
com.midtownmadness.bubblecombat.multiplay.HostStrategy.java
com.midtownmadness.bubblecombat.multiplay.LoggingListener.java
com.midtownmadness.bubblecombat.multiplay.LooperThread.java
com.midtownmadness.bubblecombat.multiplay.MessageType.java
com.midtownmadness.bubblecombat.multiplay.MultiplayEventListener.java
com.midtownmadness.bubblecombat.multiplay.MultiplayEvent.java
com.midtownmadness.bubblecombat.multiplay.MultiplayManager.java
com.midtownmadness.bubblecombat.multiplay.MultiplayStrategy.java
com.midtownmadness.bubblecombat.multiplay.MultiplayUtil.java
com.midtownmadness.bubblecombat.multiplay.MultiplayerGame.java
com.midtownmadness.bubblecombat.multiplay.commobjects.GoMessageObject.java
com.midtownmadness.bubblecombat.physics.BodyCreationRequest.java
com.midtownmadness.bubblecombat.physics.BodyUserData.java
com.midtownmadness.bubblecombat.physics.CentralHurdle.java
com.midtownmadness.bubblecombat.physics.CollisionListener.java
com.midtownmadness.bubblecombat.physics.DefaultLevelBuilder.java
com.midtownmadness.bubblecombat.physics.LevelBuilder.java
com.midtownmadness.bubblecombat.physics.MovementRequest.java
com.midtownmadness.bubblecombat.physics.MovementStateRequest.java
com.midtownmadness.bubblecombat.physics.PhysicsRequest.java
com.midtownmadness.bubblecombat.physics.PhysicsService.java
com.midtownmadness.bubblecombat.views.MenuGameView.java
com.midtownmadness.bubblecombat.views.MenuView.java