[Java] [JBullet] [OpenGL ES3.2] 文本和物理碰撞

2022/1/27 9:34:29

本文主要是介绍[Java] [JBullet] [OpenGL ES3.2] 文本和物理碰撞,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

提示

这里用的是JBullet物理引擎和javax的vecmath,可以从这里下载jbullet或者github上的OpenGLES3xGame

刚体

RigidObject.java

package com.Diamond.gl12;

import java.io.InputStream;
import android.renderscript.Float4;
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.collision.shapes.BoxShape;
import android.renderscript.Float3;
import com.bulletphysics.linearmath.Transform;
import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;
import com.bulletphysics.collision.dispatch.CollisionDispatcher;
import com.bulletphysics.collision.shapes.CollisionShape;
import android.util.Log;
import android.renderscript.Matrix4f;
import com.Diamond.gl12.JBWorld;

public class RigidObject extends MyObj {
    public RigidBody rb;
    
    public RigidObject(InputStream is) {
        super(is,new Float4(1,1,1,1));
    }
    public RigidObject(int vao,int vc) {
        super(vao,vc);
    }
    
    public RigidObject bind(JBWorld world,float mass,CollisionShape shape) {
        rb = JBUtil.createRigidBody(mass,getModelMatrixArray(),shape);
        world.addRigidBody(rb);
        return this;
    }
    public RigidObject bindStaticObject(JBWorld world,CollisionShape shape) {
        Transform t = new Transform();
        t.setIdentity();
        t.origin.set(new Vector3f(0,0,0));
        rb = JBUtil.createRigidBody(0,t,shape,new Vector3f(0,0,0));
        world.addRigidBody(rb);
        return this;
    }
    
    public Vector3f getHalf() {
        Float3 scale = getScale();
        return new Vector3f(scale.x / 2,scale.y / 2,scale.z / 2);
    }
    
    public RigidObject update() {
        Transform t = rb.getMotionState().getWorldTransform(new Transform());
        
        float[] matrix = new float[16];
        t.getOpenGLMatrix(matrix);
        setModelMatrix(new Matrix4f(matrix));
        
        setPosition(JBUtil.toFloat3(t.origin));
        
        Quat4f quat = new Quat4f();
        t.getRotation(quat);
        //我也不知道对不对,反正看起来像那么回事
        setRotate(new Float3(quat.x * 360,quat.y * 360,quat.z * 360));
        return this;
    }
}

JBWorld.java

package com.Diamond.gl12;

import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
import com.bulletphysics.collision.dispatch.CollisionConfiguration;
import com.bulletphysics.collision.dispatch.DefaultCollisionConfiguration;
import com.bulletphysics.collision.dispatch.CollisionDispatcher;
import com.bulletphysics.collision.broadphase.AxisSweep3;
import javax.vecmath.Vector3f;
import com.bulletphysics.dynamics.constraintsolver.SequentialImpulseConstraintSolver;
import com.bulletphysics.dynamics.RigidBody;

class JBWorld {
    private DiscreteDynamicsWorld mWorld;
    private boolean mIsRun;

    public static DiscreteDynamicsWorld createWorld() {
        CollisionConfiguration cc = new DefaultCollisionConfiguration();
        CollisionDispatcher cd = new CollisionDispatcher(cc);
        AxisSweep3 as = new AxisSweep3(new Vector3f(-100, -100, -100), new Vector3f(100, 100, 100), 1024);
        SequentialImpulseConstraintSolver sics = new SequentialImpulseConstraintSolver();
        
        return new DiscreteDynamicsWorld(cd, as, sics, cc);
    }
    public static DiscreteDynamicsWorld createWorld(CollisionConfiguration cc, CollisionDispatcher cd, AxisSweep3 as, SequentialImpulseConstraintSolver sics) {
        return new DiscreteDynamicsWorld(cd, as, sics, cc);
    }



    public JBWorld() {
        mWorld = createWorld();
        mWorld.setGravity(new Vector3f(0, -10, 0));
        mIsRun = false;
    }

    public JBWorld(CollisionConfiguration cc, CollisionDispatcher cd, AxisSweep3 as, SequentialImpulseConstraintSolver sics) {
        mWorld = createWorld(cc, cd, as, sics);
        mWorld.setGravity(new Vector3f(0, -10, 0));
        mIsRun = false;
    }

    public JBWorld setWorld(DiscreteDynamicsWorld world) {
        mWorld = world;
        return this;
    }
    public DiscreteDynamicsWorld getWorld() {
        return mWorld;
    }

    public JBWorld addRigidBody(RigidBody rb) {
        mWorld.addRigidBody(rb);
        return this;
    }

    /*public JBWorld start() {
        new Thread(){

            @Override
            public void run() {
                while (true) {
                    try {
                        mWorld.stepSimulation(1.0f / 60.0f);
                        Thread.sleep((long)(1.0f / 60.0f * 1000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
        return this;
    }*/

    public JBWorld stepSimulation(float fps) {
        mWorld.stepSimulation(1.0f / fps);
        return this;
    }
    public JBWorld run(float fps) {
        if(mIsRun) {
            stepSimulation(fps);
        }
        return this;
    }
    public boolean isRun() {
        return mIsRun;
    }
    public JBWorld start() {
        mIsRun = true;
        return this;
    }
    public JBWorld stop() {
        mIsRun = false;
        return this;
    }
}

JBUtil.java

package com.Diamond.gl12;

import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.linearmath.Transform;
import javax.vecmath.Vector3f;
import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
import com.bulletphysics.linearmath.MotionState;
import com.bulletphysics.linearmath.DefaultMotionState;
import android.renderscript.Float3;
import javax.vecmath.Matrix4f;

public class JBUtil {
    public static RigidBody createRigidBody(float mass,MotionState motionState,CollisionShape shape,Vector3f inertia) {
        RigidBodyConstructionInfo rbci = new RigidBodyConstructionInfo(mass,motionState,shape,inertia);
        return new RigidBody(rbci);
    }
    public static RigidBody createRigidBody(float mass,Transform transform,CollisionShape shape,Vector3f inertia) {
        DefaultMotionState dms = new DefaultMotionState(transform);
        return createRigidBody(mass,dms,shape,inertia);
    }
    public static RigidBody createRigidBody(float mass,float[] modelMatrix,CollisionShape shape,Vector3f inertia) {
        Transform t = new Transform();
        t.setIdentity();
        t.setFromOpenGLMatrix(modelMatrix);
        return createRigidBody(mass,t,shape,inertia);
    }
    public static RigidBody createRigidBody(float mass,float[] modelMatrix,CollisionShape shape) {
        Vector3f inertia = new Vector3f(0,0,0);
        shape.calculateLocalInertia(mass,inertia);
        return createRigidBody(mass,modelMatrix,shape,inertia);
    }
    
    
    
    public static Vector3f toVector3f(Float3 vec) {
        return new Vector3f(vec.x,vec.y,vec.z);
    }
    public static Float3 toFloat3(Vector3f vec) {
        return new Float3(vec.x,vec.y,vec.z);
    }
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
p.s.gif大小竟然超了

文本

Text.java

package com.Diamond.gl12;

import android.opengl.GLES32;
import android.renderscript.Float4;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

public class Text extends Texture {
    private String mTextString;
    private Float4 mColor;
    private float mTextSize;
    
    public static final int TEXTURE_TYPE = GLES32.GL_TEXTURE_2D;
    
    public Text() {
        super(TEXTURE_TYPE);
        mTextString = new String("");
        mColor = new Float4(1,1,1,1);
        mTextSize = 10;
    }
    public Text(String str) {
        super(TEXTURE_TYPE);
        mTextString = str;
        mColor = new Float4(1,1,1,1);
        mTextSize = 25;
        bind(default_parameters,loadText(str,mColor,mTextSize,100,100));
    }
    
    
    
    public static Bitmap loadText(String string,Float4 color,float textSize,int width,int height) {
        return loadText(string,color,textSize,Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888));
    }
    public static Bitmap loadText(String string,Float4 color,float textSize,Bitmap bitmap) {
        Canvas canvas = new Canvas(bitmap);
        Paint paint = new Paint();
        paint.setARGB((int)color.w * 255,(int)color.x * 255,(int)color.y * 252,(int)color.z * 255);
        paint.setTextSize(textSize);
        paint.setTypeface(null);
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        canvas.drawText(string,0,textSize,paint);
        return bitmap;
    }
    
    
    
    public Text rebind() {
        int id = genTexture();
        setTextureID(id);
        bind(default_parameters,loadText(mTextString,mColor,mTextSize,100,100));
        return this;
    }
    public Text rebind(int[] parameters,Bitmap bitmap) {
        int id = genTexture();
        setTextureID(id);
        bind(parameters,bitmap);
        return this;
    }
    
    public Text setTextSize(float textSize) {
        mTextSize = Math.abs(textSize);
        rebind();
        return this;
    }
    public float getTextSize() {
        return mTextSize;
    }
    
    public Text setText(String str) {
        mTextString = str;
        rebind();
        return this;
    }
    public String getText() {
        return mTextString;
    }
    
    public Text setTextColor(Float4 color) {
        mColor = color;
        rebind();
        return this;
    }
    public Float4 getTextColor() {
        return mColor;
    }
}

在这里插入图片描述
p.s.左下角那个是裁剪测试



这篇关于[Java] [JBullet] [OpenGL ES3.2] 文本和物理碰撞的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程