Android Open Source - gameengine Fixture






From Project

Back to project page gameengine.

License

The source code is released under:

Apache License

If you think the Android project gameengine 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

/*******************************************************************************
 * Copyright 2011 See AUTHORS file.//from w ww .  j av  a 2s .  c o  m
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

package com.badlogic.gdx.physics.box2d;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Shape.Type;
import com.badlogic.gdx.utils.GdxRuntimeException;

public class Fixture {
    /*
     * JNI #include <Box2D/Box2D.h>
     */
    /** body **/
    private Body body;

    /** the address of the fixture **/
    protected long addr;

    /** the shape, initialized lazy **/
    protected Shape shape;

    /** user specified data **/
    protected Object userData;

    /**
     * Constructs a new fixture
     * 
     * @param addr
     *            the address of the fixture
     */
    protected Fixture(Body body, long addr) {
        this.body = body;
        this.addr = addr;
    }

    protected void reset(Body body, long addr) {
        this.body = body;
        this.addr = addr;
        this.shape = null;
        this.userData = null;
    }

    /**
     * Get the type of the child shape. You can use this to down cast to the
     * concrete shape.
     * 
     * @return the shape type.
     */
    public Type getType() {
        int type = jniGetType(addr);
        switch (type) {
        case 0:
            return Type.Circle;
        case 1:
            return Type.Edge;
        case 2:
            return Type.Polygon;
        case 3:
            return Type.Chain;
        default:
            throw new GdxRuntimeException("Unknown shape type!");
        }
    }

    private native int jniGetType(long addr); /*
                                               * b2Fixture* fixture =
                                               * (b2Fixture*)addr; b2Shape::Type
                                               * type = fixture->GetType();
                                               * switch( type ) { case
                                               * b2Shape::e_circle: return 0;
                                               * case b2Shape::e_edge: return 1;
                                               * case b2Shape::e_polygon: return
                                               * 2; case b2Shape::e_chain:
                                               * return 3; default: return -1; }
                                               */

    /** Returns the shape of this fixture */
    public Shape getShape() {
        if (shape == null) {
            long shapeAddr = jniGetShape(addr);
            if (shapeAddr == 0)
                throw new GdxRuntimeException("Null shape address!");
            int type = Shape.jniGetType(shapeAddr);

            switch (type) {
            case 0:
                shape = new CircleShape(shapeAddr);
                break;
            case 1:
                shape = new EdgeShape(shapeAddr);
                break;
            case 2:
                shape = new PolygonShape(shapeAddr);
                break;
            case 3:
                shape = new ChainShape(shapeAddr);
                break;
            default:
                throw new GdxRuntimeException("Unknown shape type!");
            }
        }

        return shape;
    }

    private native long jniGetShape(long addr); /*
                                                 * b2Fixture* fixture =
                                                 * (b2Fixture*)addr; return
                                                 * (jlong)fixture->GetShape();
                                                 */

    /** Set if this fixture is a sensor. */
    public void setSensor(boolean sensor) {
        jniSetSensor(addr, sensor);
    }

    private native void jniSetSensor(long addr, boolean sensor); /*
                                                                  * b2Fixture*
                                                                  * fixture =
                                                                  * (b2Fixture
                                                                  * *)addr;
                                                                  * fixture
                                                                  * ->SetSensor
                                                                  * (sensor);
                                                                  */

    /**
     * Is this fixture a sensor (non-solid)?
     * 
     * @return the true if the shape is a sensor.
     */
    public boolean isSensor() {
        return jniIsSensor(addr);
    }

    private native boolean jniIsSensor(long addr); /*
                                                    * b2Fixture* fixture =
                                                    * (b2Fixture*)addr; return
                                                    * fixture->IsSensor();
                                                    */

    /**
     * Set the contact filtering data. This will not update contacts until the
     * next time step when either parent body is active and awake. This
     * automatically calls Refilter.
     */
    public void setFilterData(Filter filter) {
        jniSetFilterData(addr, filter.categoryBits, filter.maskBits, filter.groupIndex);
    }

    private native void jniSetFilterData(long addr, short categoryBits, short maskBits, short groupIndex); /*
                                                                                                            * b2Fixture
                                                                                                            * *
                                                                                                            * fixture
                                                                                                            * =
                                                                                                            * (
                                                                                                            * b2Fixture
                                                                                                            * *
                                                                                                            * )
                                                                                                            * addr
                                                                                                            * ;
                                                                                                            * b2Filter
                                                                                                            * filter
                                                                                                            * ;
                                                                                                            * filter
                                                                                                            * .
                                                                                                            * categoryBits
                                                                                                            * =
                                                                                                            * categoryBits
                                                                                                            * ;
                                                                                                            * filter
                                                                                                            * .
                                                                                                            * maskBits
                                                                                                            * =
                                                                                                            * maskBits
                                                                                                            * ;
                                                                                                            * filter
                                                                                                            * .
                                                                                                            * groupIndex
                                                                                                            * =
                                                                                                            * groupIndex
                                                                                                            * ;
                                                                                                            * fixture
                                                                                                            * -
                                                                                                            * >
                                                                                                            * SetFilterData
                                                                                                            * (
                                                                                                            * filter
                                                                                                            * )
                                                                                                            * ;
                                                                                                            */

    /** Get the contact filtering data. */
    private final short[] tmp = new short[3];
    private final Filter filter = new Filter();

    public Filter getFilterData() {
        jniGetFilterData(addr, tmp);
        filter.maskBits = tmp[0];
        filter.categoryBits = tmp[1];
        filter.groupIndex = tmp[2];
        return filter;
    }

    private native void jniGetFilterData(long addr, short[] filter); /*
                                                                      * b2Fixture*
                                                                      * fixture
                                                                      * =
                                                                      * (b2Fixture
                                                                      * *)addr;
                                                                      * unsigned
                                                                      * short*
                                                                      * filterOut
                                                                      * =
                                                                      * (unsigned
                                                                      * short
                                                                      * *)filter
                                                                      * ;
                                                                      * b2Filter
                                                                      * f =
                                                                      * fixture
                                                                      * ->
                                                                      * GetFilterData
                                                                      * ();
                                                                      * filterOut
                                                                      * [0] =
                                                                      * f.maskBits
                                                                      * ;
                                                                      * filterOut
                                                                      * [1] = f.
                                                                      * categoryBits
                                                                      * ;
                                                                      * filterOut
                                                                      * [2] =
                                                                      * f.groupIndex
                                                                      * ;
                                                                      */

    /**
     * Call this if you want to establish collision that was previously disabled
     * by b2ContactFilter::ShouldCollide.
     */
    public void refilter() {
        jniRefilter(addr);
    }

    private native void jniRefilter(long addr); /*
                                                 * b2Fixture* fixture =
                                                 * (b2Fixture*)addr;
                                                 * fixture->Refilter();
                                                 */

    /**
     * Get the parent body of this fixture. This is NULL if the fixture is not
     * attached.
     */
    public Body getBody() {
        return body;
    }

    /**
     * Test a point for containment in this fixture.
     * 
     * @param p
     *            a point in world coordinates.
     */
    public boolean testPoint(Vector2 p) {
        return jniTestPoint(addr, p.x, p.y);
    }

    /**
     * Test a point for containment in this fixture.
     * 
     * @param x
     *            the x-coordinate
     * @param y
     *            the y-coordinate
     */
    public boolean testPoint(float x, float y) {
        return jniTestPoint(addr, x, y);
    }

    private native boolean jniTestPoint(long addr, float x, float y); /*
                                                                       * b2Fixture*
                                                                       * fixture
                                                                       * =
                                                                       * (b2Fixture
                                                                       * *)addr;
                                                                       * return
                                                                       * fixture
                                                                       * -
                                                                       * >TestPoint
                                                                       * (
                                                                       * b2Vec2(
                                                                       * x, y )
                                                                       * );
                                                                       */

    // const b2Body* GetBody() const;
    //
    // /// Get the next fixture in the parent body's fixture list.
    // /// @return the next shape.
    // b2Fixture* GetNext();
    // const b2Fixture* GetNext() const;
    //
    // /// Get the user data that was assigned in the fixture definition. Use
    // this to
    // /// store your application specific data.
    // void* GetUserData() const;
    //
    // /// Set the user data. Use this to store your application specific data.
    // void SetUserData(void* data);
    //
    // /// Cast a ray against this shape.
    // /// @param output the ray-cast results.
    // /// @param input the ray-cast input parameters.
    // bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input) const;
    //
    // /// Get the mass data for this fixture. The mass data is based on the
    // density and
    // /// the shape. The rotational inertia is about the shape's origin. This
    // operation
    // /// may be expensive.
    // void GetMassData(b2MassData* massData) const;

    /**
     * Set the density of this fixture. This will _not_ automatically adjust the
     * mass of the body. You must call b2Body::ResetMassData to update the
     * body's mass.
     */
    public void setDensity(float density) {
        jniSetDensity(addr, density);
    }

    private native void jniSetDensity(long addr, float density); /*
                                                                  * b2Fixture*
                                                                  * fixture =
                                                                  * (b2Fixture
                                                                  * *)addr;
                                                                  * fixture
                                                                  * ->SetDensity
                                                                  * (density);
                                                                  */

    /** Get the density of this fixture. */
    public float getDensity() {
        return jniGetDensity(addr);
    }

    private native float jniGetDensity(long addr); /*
                                                    * b2Fixture* fixture =
                                                    * (b2Fixture*)addr; return
                                                    * fixture->GetDensity();
                                                    */

    /** Get the coefficient of friction. */
    public float getFriction() {
        return jniGetFriction(addr);
    }

    private native float jniGetFriction(long addr); /*
                                                     * b2Fixture* fixture =
                                                     * (b2Fixture*)addr; return
                                                     * fixture->GetFriction();
                                                     */

    /** Set the coefficient of friction. */
    public void setFriction(float friction) {
        jniSetFriction(addr, friction);
    }

    private native void jniSetFriction(long addr, float friction); /*
                                                                    * b2Fixture*
                                                                    * fixture =
                                                                    * (
                                                                    * b2Fixture*
                                                                    * )addr;
                                                                    * fixture
                                                                    * ->SetFriction
                                                                    * (
                                                                    * friction);
                                                                    */

    /** Get the coefficient of restitution. */
    public float getRestitution() {
        return jniGetRestitution(addr);
    }

    private native float jniGetRestitution(long addr); /*
                                                        * b2Fixture* fixture =
                                                        * (b2Fixture*)addr;
                                                        * return
                                                        * fixture->GetRestitution
                                                        * ();
                                                        */

    /** Set the coefficient of restitution. */
    public void setRestitution(float restitution) {
        jniSetRestitution(addr, restitution);
    }

    private native void jniSetRestitution(long addr, float restitution); /*
                                                                          * b2Fixture
                                                                          * *
                                                                          * fixture
                                                                          * = (
                                                                          * b2Fixture
                                                                          * *
                                                                          * )addr
                                                                          * ;
                                                                          * fixture
                                                                          * ->
                                                                          * SetRestitution
                                                                          * (
                                                                          * restitution
                                                                          * );
                                                                          */

    // /// Get the fixture's AABB. This AABB may be enlarge and/or stale.
    // /// If you need a more accurate AABB, compute it using the shape and
    // /// the body transform.
    // const b2AABB& GetAABB() const;

    /** Sets custom user data. */
    public void setUserData(Object userData) {
        this.userData = userData;
    }

    /** @return custom user data */
    public Object getUserData() {
        return userData;
    }
}




Java Source Code List

com.badlogic.gdx.math.MathUtils.java
com.badlogic.gdx.math.Matrix3.java
com.badlogic.gdx.math.Matrix4.java
com.badlogic.gdx.math.Quaternion.java
com.badlogic.gdx.math.Vector2.java
com.badlogic.gdx.math.Vector3.java
com.badlogic.gdx.physics.box2d.BodyDef.java
com.badlogic.gdx.physics.box2d.Body.java
com.badlogic.gdx.physics.box2d.ChainShape.java
com.badlogic.gdx.physics.box2d.CircleShape.java
com.badlogic.gdx.physics.box2d.ContactFilter.java
com.badlogic.gdx.physics.box2d.ContactImpulse.java
com.badlogic.gdx.physics.box2d.ContactListener.java
com.badlogic.gdx.physics.box2d.Contact.java
com.badlogic.gdx.physics.box2d.DestructionListener.java
com.badlogic.gdx.physics.box2d.EdgeShape.java
com.badlogic.gdx.physics.box2d.Filter.java
com.badlogic.gdx.physics.box2d.FixtureDef.java
com.badlogic.gdx.physics.box2d.Fixture.java
com.badlogic.gdx.physics.box2d.JointDef.java
com.badlogic.gdx.physics.box2d.JointEdge.java
com.badlogic.gdx.physics.box2d.Joint.java
com.badlogic.gdx.physics.box2d.Manifold.java
com.badlogic.gdx.physics.box2d.MassData.java
com.badlogic.gdx.physics.box2d.PolygonShape.java
com.badlogic.gdx.physics.box2d.QueryCallback.java
com.badlogic.gdx.physics.box2d.RayCastCallback.java
com.badlogic.gdx.physics.box2d.Shape.java
com.badlogic.gdx.physics.box2d.Transform.java
com.badlogic.gdx.physics.box2d.WorldManifold.java
com.badlogic.gdx.physics.box2d.World.java
com.badlogic.gdx.physics.box2d.joints.DistanceJointDef.java
com.badlogic.gdx.physics.box2d.joints.DistanceJoint.java
com.badlogic.gdx.physics.box2d.joints.FrictionJointDef.java
com.badlogic.gdx.physics.box2d.joints.FrictionJoint.java
com.badlogic.gdx.physics.box2d.joints.GearJointDef.java
com.badlogic.gdx.physics.box2d.joints.GearJoint.java
com.badlogic.gdx.physics.box2d.joints.MouseJointDef.java
com.badlogic.gdx.physics.box2d.joints.MouseJoint.java
com.badlogic.gdx.physics.box2d.joints.PrismaticJointDef.java
com.badlogic.gdx.physics.box2d.joints.PrismaticJoint.java
com.badlogic.gdx.physics.box2d.joints.PulleyJointDef.java
com.badlogic.gdx.physics.box2d.joints.PulleyJoint.java
com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef.java
com.badlogic.gdx.physics.box2d.joints.RevoluteJoint.java
com.badlogic.gdx.physics.box2d.joints.RopeJointDef.java
com.badlogic.gdx.physics.box2d.joints.RopeJoint.java
com.badlogic.gdx.physics.box2d.joints.WeldJointDef.java
com.badlogic.gdx.physics.box2d.joints.WeldJoint.java
com.badlogic.gdx.physics.box2d.joints.WheelJointDef.java
com.badlogic.gdx.physics.box2d.joints.WheelJoint.java
com.badlogic.gdx.utils.Array.java
com.badlogic.gdx.utils.ComparableTimSort.java
com.badlogic.gdx.utils.Disposable.java
com.badlogic.gdx.utils.GdxRuntimeException.java
com.badlogic.gdx.utils.LongArray.java
com.badlogic.gdx.utils.LongMap.java
com.badlogic.gdx.utils.NumberUtils.java
com.badlogic.gdx.utils.Pool.java
com.badlogic.gdx.utils.Sort.java
com.badlogic.gdx.utils.StringBuilder.java
com.badlogic.gdx.utils.TimSort.java
com.garrapeta.MathUtils.java
com.garrapeta.gameengine.Actor.java
com.garrapeta.gameengine.AsyncGameMessage.java
com.garrapeta.gameengine.BitmapManager.java
com.garrapeta.gameengine.Box2DActor.java
com.garrapeta.gameengine.Box2DWorld.java
com.garrapeta.gameengine.GameMessage.java
com.garrapeta.gameengine.GameView.java
com.garrapeta.gameengine.GameWorld.java
com.garrapeta.gameengine.ShapeDrawer.java
com.garrapeta.gameengine.SyncGameMessage.java
com.garrapeta.gameengine.Viewport.java
com.garrapeta.gameengine.actor.Box2DAtomicActor.java
com.garrapeta.gameengine.actor.Box2DCircleActor.java
com.garrapeta.gameengine.actor.Box2DEdgeActor.java
com.garrapeta.gameengine.actor.Box2DLoopActor.java
com.garrapeta.gameengine.actor.Box2DOpenChainActor.java
com.garrapeta.gameengine.actor.Box2DPolygonActor.java
com.garrapeta.gameengine.actor.IAtomicActor.java
com.garrapeta.gameengine.actor.SimpleActor.java
com.garrapeta.gameengine.module.LevelActionsModule.java
com.garrapeta.gameengine.module.LoadedLevelActionsModule.java
com.garrapeta.gameengine.module.SoundModule.java
com.garrapeta.gameengine.module.VibrationModule.java
com.garrapeta.gameengine.utils.IOUtils.java
com.garrapeta.gameengine.utils.LogX.java
com.garrapeta.gameengine.utils.PhysicsUtils.java
com.garrapeta.gameengine.utils.Pool.java