2013-05-11 3 views
0

중력없이 모든 충돌 및 가속 효과를 유지하는 방법. 나는 땅에 떨어지지 않는 몸을 추가하고 충돌이 추가되지 않으면 그 자세를 유지하고 싶습니다.및 마우스 마우스 조인트와 중력

public class PhysicsMouseJointExample extends SimpleBaseGameActivity implements IAccelerationListener, IOnSceneTouchListener, IOnAreaTouchListener { 
    // =========================================================== 
    // Constants 
    // =========================================================== 

    private static final int CAMERA_WIDTH = 720; 
    private static final int CAMERA_HEIGHT = 480; 

    private static final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f); 

    // =========================================================== 
    // Fields 
    // =========================================================== 

    private BitmapTextureAtlas mBitmapTextureAtlas; 

    private TiledTextureRegion mBoxFaceTextureRegion; 
    private TiledTextureRegion mCircleFaceTextureRegion; 

    private Scene mScene; 

    private PhysicsWorld mPhysicsWorld; 
    private int mFaceCount = 0; 

    private MouseJoint mMouseJointActive; 
    private Body mGroundBody; 

    // =========================================================== 
    // Constructors 
    // =========================================================== 

    // =========================================================== 
    // Getter & Setter 
    // =========================================================== 

    // =========================================================== 
    // Methods for/from SuperClass/Interfaces 
    // =========================================================== 

    @Override 
    public EngineOptions onCreateEngineOptions() { 
     Toast.makeText(this, "Touch the screen to add objects.", Toast.LENGTH_LONG).show(); 

     final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT); 

     return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera); 
    } 

    @Override 
    public void onCreateResources() { 
     BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); 

     this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 64, 64, TextureOptions.BILINEAR); 
     this.mBoxFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_box_tiled.png", 0, 0, 2, 1); // 64x32 
     this.mCircleFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_circle_tiled.png", 0, 32, 2, 1); // 64x32 
     this.mBitmapTextureAtlas.load(); 
    } 

    @Override 
    public Scene onCreateScene() { 
     this.mEngine.registerUpdateHandler(new FPSLogger()); 

     this.mScene = new Scene(); 
     this.mScene.setBackground(new Background(0, 0, 0)); 
     this.mScene.setOnSceneTouchListener(this); 
     this.mScene.setOnAreaTouchListener(this); 

     this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false); 
     this.mGroundBody = this.mPhysicsWorld.createBody(new BodyDef()); 

     final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager(); 
     final Rectangle ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2, vertexBufferObjectManager); 
     final Rectangle roof = new Rectangle(0, 0, CAMERA_WIDTH, 2, vertexBufferObjectManager); 
     final Rectangle left = new Rectangle(0, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager); 
     final Rectangle right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager); 

     final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f); 
     PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef); 
     PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef); 
     PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef); 
     PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef); 

     this.mScene.attachChild(ground); 
     this.mScene.attachChild(roof); 
     this.mScene.attachChild(left); 
     this.mScene.attachChild(right); 

     this.mScene.registerUpdateHandler(this.mPhysicsWorld); 

     return this.mScene; 
    } 

    @Override 
    public void onGameCreated() { 
     this.mEngine.enableVibrator(this); 
    } 

    @Override 
    public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) { 
     if(this.mPhysicsWorld != null) { 
      switch(pSceneTouchEvent.getAction()) { 
       case TouchEvent.ACTION_DOWN: 
        this.addFace(pSceneTouchEvent.getX(), pSceneTouchEvent.getY()); 
        return true; 
       case TouchEvent.ACTION_MOVE: 
        if(this.mMouseJointActive != null) { 
         final Vector2 vec = Vector2Pool.obtain(pSceneTouchEvent.getX()/PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, pSceneTouchEvent.getY()/PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT); 
         this.mMouseJointActive.setTarget(vec); 
         Vector2Pool.recycle(vec); 
        } 
        return true; 
       case TouchEvent.ACTION_UP: 
        if(this.mMouseJointActive != null) { 
         this.mPhysicsWorld.destroyJoint(this.mMouseJointActive); 
         this.mMouseJointActive = null; 
        } 
        return true; 
      } 
      return false; 
     } 
     return false; 
    } 

    @Override 
    public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final ITouchArea pTouchArea, final float pTouchAreaLocalX, final float pTouchAreaLocalY) { 
     if(pSceneTouchEvent.isActionDown()) { 
      final IAreaShape face = (IAreaShape) pTouchArea; 
      /* 
      * If we have a active MouseJoint, we are just moving it around 
      * instead of creating a second one. 
      */ 
      if(this.mMouseJointActive == null) { 
       this.mEngine.vibrate(100); 
       this.mMouseJointActive = this.createMouseJoint(face, pTouchAreaLocalX, pTouchAreaLocalY); 
      } 
      return true; 
     } 
     return false; 
    } 

    @Override 
    public void onAccelerationAccuracyChanged(final AccelerationData pAccelerationData) { 

    } 

    @Override 
    public void onAccelerationChanged(final AccelerationData pAccelerationData) { 
     final Vector2 gravity = Vector2Pool.obtain(pAccelerationData.getX(), pAccelerationData.getY()); 
     this.mPhysicsWorld.setGravity(gravity); 
     Vector2Pool.recycle(gravity); 
    } 

    @Override 
    public void onResumeGame() { 
     super.onResumeGame(); 

     this.enableAccelerationSensor(this); 
    } 

    @Override 
    public void onPauseGame() { 
     super.onPauseGame(); 

     this.disableAccelerationSensor(); 
    } 

    // =========================================================== 
    // Methods 
    // =========================================================== 

    public MouseJoint createMouseJoint(final IAreaShape pFace, final float pTouchAreaLocalX, final float pTouchAreaLocalY) { 
     final Body body = (Body) pFace.getUserData(); 
     final MouseJointDef mouseJointDef = new MouseJointDef(); 

     final Vector2 localPoint = Vector2Pool.obtain((pTouchAreaLocalX - pFace.getWidth() * 0.5f)/PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, (pTouchAreaLocalY - pFace.getHeight() * 0.5f)/PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT); 
     this.mGroundBody.setTransform(localPoint, 0); 

     mouseJointDef.bodyA = this.mGroundBody; 
     mouseJointDef.bodyB = body; 
     mouseJointDef.dampingRatio = 0.95f; 
     mouseJointDef.frequencyHz = 30; 
     mouseJointDef.maxForce = (200.0f * body.getMass()); 
     mouseJointDef.collideConnected = true; 

     mouseJointDef.target.set(body.getWorldPoint(localPoint)); 
     Vector2Pool.recycle(localPoint); 

     return (MouseJoint) this.mPhysicsWorld.createJoint(mouseJointDef); 
    } 

    private void addFace(final float pX, final float pY) { 
     this.mFaceCount++; 
     Debug.d("Faces: " + this.mFaceCount); 

     final AnimatedSprite face; 
     final Body body; 

     if(this.mFaceCount % 2 == 0) { 
      face = new AnimatedSprite(pX, pY, this.mBoxFaceTextureRegion, this.getVertexBufferObjectManager()); 
      body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF); 
     } else { 
      face = new AnimatedSprite(pX, pY, this.mCircleFaceTextureRegion, this.getVertexBufferObjectManager()); 
      body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF); 
     } 
     face.setUserData(body); 
     face.animate(200); 

     this.mScene.registerTouchArea(face); 
     this.mScene.attachChild(face); 

     this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true)); 
    } 

    // =========================================================== 
    // Inner and Anonymous Classes 
    // =========================================================== 
} 

나는 중력을 변경하려고하지만 아무 것도 추가하지 않는 것 같습니다.

this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, -10), false); 

답변

0

없음 중력 없습니다 :

enter code here`this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, 0), false); 

이 반 중력 또는 ~ 1G까지입니다 :

this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, -10), false); 

이입니다 ~ 1G 오른쪽으로 :

this.mPhysicsWorld = new PhysicsWorld(new Vector2(10, 0), false); 
+0

하는 경우 나는 전화를 거꾸로 돌리거나 왼쪽으로 향하게한다. 내 스프라이트는 그 자리에 있어야한다. 위의 값으로 변경하면 스프라이트가 계속 떨어집니다. – lucignolo

+0

그러면 첫 번째 만 원합니다. PhysicsWorld에 전달되는 Vector2는 왼쪽 또는 오른쪽, 위 또는 아래의 "중력"힘을 지정하는 것입니다. 첫 번째 값은 왼쪽/오른쪽입니다. 음수 값을 지정하면 스프라이트가 화면의 왼쪽으로 떨어지고 양수 값을 입력하면 화면의 오른쪽으로 떨어집니다. 두 번째 값은 음수 값의 경우 화면의 위쪽 또는 위쪽이고 양수 값의 경우 화면 아래쪽입니다. 따라서 새로운 Vector (0, 0)을 전달하면 전혀 중력이 없다고 말하는 것입니다. 스프 라이트는 충돌로 인해 이동하거나 사용자가 주위로 뛰어 다니도록 허용하는 경우에만 이동합니다. –

+0

나는 this.mPhysicsWorld = new PhysicsWorld (새 Vector2 (0, 0), false)와 같이 코드를 변경하려고합니다. 그러나 스프라이트는 여전히 떨어집니다. 전화를 기울이거나 회전시킬 때 그 스프 라이트가 움직이지 않게하고 싶습니다 – lucignolo

관련 문제