Android Open Source - gameengine Chain Shape






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./* w w w . j  ava 2 s  .  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;

public class ChainShape extends Shape {
    /*
     * JNI #include <Box2D/Box2D.h>
     */

    public ChainShape() {
        addr = newChainShape();
    }

    private native long newChainShape(); /*
                                          * return (jlong)(new b2ChainShape());
                                          */

    ChainShape(long addr) {
        this.addr = addr;
    }

    @Override
    public Type getType() {
        return Type.Chain;
    }

    /**
     * Create a loop. This automatically adjusts connectivity.
     * 
     * @param vertices
     *            an array of vertices, these are copied
     */
    public void createLoop(Vector2[] vertices) {
        float[] verts = new float[vertices.length * 2];
        for (int i = 0, j = 0; i < vertices.length * 2; i += 2, j++) {
            verts[i] = vertices[j].x;
            verts[i + 1] = vertices[j].y;
        }
        jniCreateLoop(addr, verts, verts.length / 2);
    }

    private native void jniCreateLoop(long addr, float[] verts, int numVertices); /*
                                                                                   * b2ChainShape
                                                                                   * *
                                                                                   * chain
                                                                                   * =
                                                                                   * (
                                                                                   * b2ChainShape
                                                                                   * *
                                                                                   * )
                                                                                   * addr
                                                                                   * ;
                                                                                   * b2Vec2
                                                                                   * *
                                                                                   * verticesOut
                                                                                   * =
                                                                                   * new
                                                                                   * b2Vec2
                                                                                   * [
                                                                                   * numVertices
                                                                                   * ]
                                                                                   * ;
                                                                                   * for
                                                                                   * (
                                                                                   * int
                                                                                   * i
                                                                                   * =
                                                                                   * 0
                                                                                   * ;
                                                                                   * i
                                                                                   * <
                                                                                   * numVertices
                                                                                   * ;
                                                                                   * i
                                                                                   * ++
                                                                                   * )
                                                                                   * verticesOut
                                                                                   * [
                                                                                   * i
                                                                                   * ]
                                                                                   * =
                                                                                   * b2Vec2
                                                                                   * (
                                                                                   * verts
                                                                                   * [
                                                                                   * i
                                                                                   * <<
                                                                                   * 1
                                                                                   * ]
                                                                                   * ,
                                                                                   * verts
                                                                                   * [
                                                                                   * (
                                                                                   * i
                                                                                   * <<
                                                                                   * 1
                                                                                   * )
                                                                                   * +
                                                                                   * 1
                                                                                   * ]
                                                                                   * )
                                                                                   * ;
                                                                                   * chain
                                                                                   * -
                                                                                   * >
                                                                                   * CreateLoop
                                                                                   * (
                                                                                   * verticesOut
                                                                                   * ,
                                                                                   * numVertices
                                                                                   * )
                                                                                   * ;
                                                                                   * delete
                                                                                   * verticesOut
                                                                                   * ;
                                                                                   */

    /**
     * Create a chain with isolated end vertices.
     * 
     * @param vertices
     *            an array of vertices, these are copied
     */
    public void createChain(Vector2[] vertices) {
        float[] verts = new float[vertices.length * 2];
        for (int i = 0, j = 0; i < vertices.length * 2; i += 2, j++) {
            verts[i] = vertices[j].x;
            verts[i + 1] = vertices[j].y;
        }
        jniCreateChain(addr, verts, verts.length / 2);
    }

    private native void jniCreateChain(long addr, float[] verts, int numVertices); /*
                                                                                    * b2ChainShape
                                                                                    * *
                                                                                    * chain
                                                                                    * =
                                                                                    * (
                                                                                    * b2ChainShape
                                                                                    * *
                                                                                    * )
                                                                                    * addr
                                                                                    * ;
                                                                                    * b2Vec2
                                                                                    * *
                                                                                    * verticesOut
                                                                                    * =
                                                                                    * new
                                                                                    * b2Vec2
                                                                                    * [
                                                                                    * numVertices
                                                                                    * ]
                                                                                    * ;
                                                                                    * for
                                                                                    * (
                                                                                    * int
                                                                                    * i
                                                                                    * =
                                                                                    * 0
                                                                                    * ;
                                                                                    * i
                                                                                    * <
                                                                                    * numVertices
                                                                                    * ;
                                                                                    * i
                                                                                    * ++
                                                                                    * )
                                                                                    * verticesOut
                                                                                    * [
                                                                                    * i
                                                                                    * ]
                                                                                    * =
                                                                                    * b2Vec2
                                                                                    * (
                                                                                    * verts
                                                                                    * [
                                                                                    * i
                                                                                    * <<
                                                                                    * 1
                                                                                    * ]
                                                                                    * ,
                                                                                    * verts
                                                                                    * [
                                                                                    * (
                                                                                    * i
                                                                                    * <<
                                                                                    * 1
                                                                                    * )
                                                                                    * +
                                                                                    * 1
                                                                                    * ]
                                                                                    * )
                                                                                    * ;
                                                                                    * chain
                                                                                    * -
                                                                                    * >
                                                                                    * CreateChain
                                                                                    * (
                                                                                    * verticesOut
                                                                                    * ,
                                                                                    * numVertices
                                                                                    * )
                                                                                    * ;
                                                                                    * delete
                                                                                    * verticesOut
                                                                                    * ;
                                                                                    */

    /**
     * Establish connectivity to a vertex that precedes the first vertex. Don't
     * call this for loops.
     */
    public void setPrevVertex(Vector2 prevVertex) {
        setPrevVertex(prevVertex.x, prevVertex.y);
    }

    /**
     * Establish connectivity to a vertex that precedes the first vertex. Don't
     * call this for loops.
     */
    public void setPrevVertex(float prevVertexX, float prevVertexY) {
        jniSetPrevVertex(addr, prevVertexX, prevVertexY);
    }

    private native void jniSetPrevVertex(long addr, float x, float y); /*
                                                                        * b2ChainShape
                                                                        * *
                                                                        * chain
                                                                        * = (
                                                                        * b2ChainShape
                                                                        * *
                                                                        * )addr;
                                                                        * chain
                                                                        * ->
                                                                        * SetPrevVertex
                                                                        * (
                                                                        * b2Vec2
                                                                        * (x,
                                                                        * y));
                                                                        */

    /**
     * Establish connectivity to a vertex that follows the last vertex. Don't
     * call this for loops.
     */
    public void setNextVertex(Vector2 nextVertex) {
        setNextVertex(nextVertex.x, nextVertex.y);
    }

    /**
     * Establish connectivity to a vertex that follows the last vertex. Don't
     * call this for loops.
     */
    public void setNextVertex(float nextVertexX, float nextVertexY) {
        jniSetNextVertex(addr, nextVertexX, nextVertexY);
    }

    private native void jniSetNextVertex(long addr, float x, float y); /*
                                                                        * b2ChainShape
                                                                        * *
                                                                        * chain
                                                                        * = (
                                                                        * b2ChainShape
                                                                        * *
                                                                        * )addr;
                                                                        * chain
                                                                        * ->
                                                                        * SetNextVertex
                                                                        * (
                                                                        * b2Vec2
                                                                        * (x,
                                                                        * y));
                                                                        */

    /** @return the number of vertices */
    public int getVertexCount() {
        return jniGetVertexCount(addr);
    }

    private native int jniGetVertexCount(long addr); /*
                                                      * b2ChainShape* chain =
                                                      * (b2ChainShape*)addr;
                                                      * return
                                                      * chain->GetVertexCount();
                                                      */

    private static float[] verts = new float[2];

    /**
     * Returns the vertex at the given position.
     * 
     * @param index
     *            the index of the vertex 0 <= index < getVertexCount( )
     * @param vertex
     *            vertex
     */
    public void getVertex(int index, Vector2 vertex) {
        jniGetVertex(addr, index, verts);
        vertex.x = verts[0];
        vertex.y = verts[1];
    }

    private native void jniGetVertex(long addr, int index, float[] verts); /*
                                                                            * b2ChainShape
                                                                            * *
                                                                            * chain
                                                                            * =
                                                                            * (
                                                                            * b2ChainShape
                                                                            * *
                                                                            * )addr
                                                                            * ;
                                                                            * const
                                                                            * b2Vec2
                                                                            * v
                                                                            * =
                                                                            * chain
                                                                            * ->
                                                                            * GetVertex
                                                                            * (
                                                                            * index
                                                                            * );
                                                                            * verts
                                                                            * [
                                                                            * 0]
                                                                            * =
                                                                            * v
                                                                            * .x
                                                                            * ;
                                                                            * verts
                                                                            * [
                                                                            * 1]
                                                                            * =
                                                                            * v
                                                                            * .y
                                                                            * ;
                                                                            */

    // /// Implement b2Shape. Vertices are cloned using b2Alloc.
    // b2Shape* Clone(b2BlockAllocator* allocator) const;
    //
    // /// @see b2Shape::GetChildCount
    // int32 GetChildCount() const;
    //
    // /// Get a child edge.
    // void GetChildEdge(b2EdgeShape* edge, int32 index) const;
    //
    // /// This always return false.
    // /// @see b2Shape::TestPoint
    // bool TestPoint(const b2Transform& transform, const b2Vec2& p) const;
    //
    // /// Implement b2Shape.
    // bool RayCast(b2RayCastOutput* output, const b2RayCastInput& input,
    // const b2Transform& transform, int32 childIndex) const;
    //
    // /// @see b2Shape::ComputeAABB
    // void ComputeAABB(b2AABB* aabb, const b2Transform& transform, int32
    // childIndex) const;
    //
    // /// Chains have zero mass.
    // /// @see b2Shape::ComputeMass
    // void ComputeMass(b2MassData* massData, float32 density) const;
    //
}




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