当前位置: 首页 > news >正文

Android基于LiquidFun引擎实现软体碰撞效果

一、实现效果

Android使用LiquidFun物理引擎实现果冻碰撞效果

二、Android代码

    // 加载liquidfun动态库static {System.loadLibrary("liquidfun");System.loadLibrary("liquidfun_jni");}class ParticleData {long id;ParticleSystem particleSystem;float particleRadius;int textureId;ArrayList<ArrayList<Integer>> row;public ParticleData(long id, ParticleSystem ps, float particleRadius, ArrayList<ArrayList<Integer>> row, int textureId) {this.id = id;this.particleSystem = ps;this.textureId = textureId;this.particleRadius = particleRadius;this.row = row;}public long getId() {return this.id;}public ParticleSystem getParticleSystem() {return this.particleSystem;}public int getTextureId() { return this.textureId;}public float getParticleRadius() { return this.particleRadius;}public ArrayList<ArrayList<Integer>> getRow() { return this.row;}}class BodyData {long id;Body body;FloatBuffer vertexBuffer;FloatBuffer uvBuffer;int vertexLen;int drawMode;int textureId;public BodyData(long id, Body body, float[] buffer, float[] uv, int drawMode, int textureId) {this.id = id;this.body = body;this.vertexBuffer = makeFloatBuffer(buffer);this.uvBuffer = makeFloatBuffer(uv);this.vertexLen = buffer.length / 2;this.drawMode = drawMode;this.textureId = textureId;}public long getId() {return this.id;}public Body getBody() {return this.body;}public FloatBuffer getVertexBuffer() {return this.vertexBuffer;}public FloatBuffer getUvBuffer() { return this.uvBuffer;}public int getDrawMode() { return this.drawMode;}public int getVertexLen() { return this.vertexLen;}public int getTextureId() { return this.textureId;}}public MainRenderer(MainGlView view) {this.view = view;world = new World(0, -10);//this.addBox(1, 1, 0, 10, 0, BodyType.dynamicBody, 0);}private void addBodyData(Body body, float[] buffer, float[] uv, int drawMode, int textureId) {long id = nextBodyDataId++;BodyData data = new BodyData(id, body, buffer, uv, drawMode, textureId);this.mapBodyData.put(id, data);}private void addParticleData(ParticleSystem ps, float particleRadius, ArrayList<ArrayList<Integer>> row, int textureId) {long id = nextBodyDataId++;ParticleData data = new ParticleData(id, ps, particleRadius, row, textureId);this.mapParticleData.put(id, data);}public void addCircle(GL10 gl,float r, float x, float y, float angle, BodyType type, float density, int resId) {// Box2d用BodyDef bodyDef = new BodyDef();bodyDef.setType(type);bodyDef.setPosition(x, y);bodyDef.setAngle(angle);Body body = world.createBody(bodyDef);CircleShape shape = new CircleShape();shape.setRadius(r);body.createFixture(shape, density);// OpenGL用float vertices[] = new float[32*2];float uv[] = new float[32*2];for(int i = 0; i < 32; ++i){float a = ((float)Math.PI * 2.0f * i)/32;vertices[i*2]   = r * (float)Math.sin(a);vertices[i*2+1] = r * (float)Math.cos(a);uv[i*2]   = ((float)Math.sin(a) + 1.0f)/2f;uv[i*2+1] = (-1 * (float)Math.cos(a) + 1.0f)/2f;}int textureId=makeTexture(gl, resId);this.addBodyData(body, vertices, uv, GL10.GL_TRIANGLE_FAN, textureId);}public void addBox(GL10 gl,float hx, float hy, float x, float y, float angle, BodyType type, float density, int resId) {// Box2d用BodyDef bodyDef = new BodyDef();bodyDef.setType(type);bodyDef.setPosition(x, y);Body body = world.createBody(bodyDef);PolygonShape shape = new PolygonShape();shape.setAsBox(hx, hy, 0, 0, angle);body.createFixture(shape, density);// OpenGL用float vertices[] = {- hx, + hy,- hx, - hy,+ hx, + hy,+ hx, - hy,};FloatBuffer buffer = this.makeFloatBuffer(vertices);float[] uv={0.0f,0.0f,//左上0.0f,1.0f,//左下1.0f,0.0f,//右上1.0f,1.0f,//右下};FloatBuffer uvBuffer = this.makeFloatBuffer(uv);int textureId=makeTexture(gl, resId);this.addBodyData(body, vertices, uv, GL10.GL_TRIANGLE_STRIP, textureId);}public void addSoftBody(GL10 gl,float hx, float hy, float cx, float cy, float particleRadius, int resId) {ParticleSystemDef psd = new ParticleSystemDef();psd.setRadius(particleRadius);ParticleSystem ps = world.createParticleSystem(psd);PolygonShape shape = new PolygonShape();shape.setAsBox(hx, hy, 0, 0, 0);ParticleGroupDef pgd = new ParticleGroupDef();pgd.setFlags(ParticleFlag.elasticParticle);pgd.setGroupFlags(ParticleGroupFlag.solidParticleGroup);pgd.setShape(shape);pgd.setPosition(cx, cy);ParticleGroup pg = ps.createParticleGroup(pgd);float py = 0;ArrayList<ArrayList<Integer>> row = new ArrayList<ArrayList<Integer>>();ArrayList<Integer> line = new ArrayList<Integer>();for (int i = pg.getBufferIndex(); i < pg.getParticleCount() - pg.getBufferIndex(); ++i) {float y = ps.getParticlePositionY(i);if (i==0) {py = y;}if ((float)Math.abs(py - y) > 0.01f) {row.add(line);line = new ArrayList<Integer>();}line.add(i);py = y;}row.add(line);int textureId=makeTexture(gl, resId);this.addParticleData(ps, particleRadius, row, textureId);}

三、完整源码下载:LiquidFunTest源码: https://url83.ctfile.com/d/45573183-68059777-a5f411?p=7526 (访问密码: 7526)
 


文章转载自:

http://uEJ8K039.msfqt.cn
http://96yDTiMS.msfqt.cn
http://WbycQrHz.msfqt.cn
http://ZSvhg12p.msfqt.cn
http://lIL9DMvO.msfqt.cn
http://8LC2LSPd.msfqt.cn
http://dWraaxan.msfqt.cn
http://kjjQ67bo.msfqt.cn
http://P1rUF6Xa.msfqt.cn
http://ETDzxMoL.msfqt.cn
http://8LNqL4E4.msfqt.cn
http://9fJJcBWj.msfqt.cn
http://ylhhNscI.msfqt.cn
http://U6DyFJLp.msfqt.cn
http://fUyI8ytY.msfqt.cn
http://JTphlVRS.msfqt.cn
http://Ld2Kthws.msfqt.cn
http://Cyead66u.msfqt.cn
http://vpFEpRcS.msfqt.cn
http://scTCf5Sj.msfqt.cn
http://qIFvj9fG.msfqt.cn
http://EyiZfPFF.msfqt.cn
http://pqBJpSCY.msfqt.cn
http://cXV9xnSz.msfqt.cn
http://kbQ5an1J.msfqt.cn
http://iSDX7VHe.msfqt.cn
http://ILaLKbpV.msfqt.cn
http://QuYECuZJ.msfqt.cn
http://GafK9kXP.msfqt.cn
http://Fu7tHIlJ.msfqt.cn
http://www.dtcms.com/a/226682.html

相关文章:

  • leetcode hot100刷题日记——35.子集
  • day17 leetcode-hot100-34(链表13)
  • 每日算法刷题计划Day20 6.2:leetcode二分答案3道题,用时1h20min
  • 从 LeetCode 到日志匹配:一行 Swift 实现规则识别
  • 力扣LeetBook数组和字符串--数组简介
  • 【软件测试】web自动化:Pycharm+Selenium+Firefox(一)
  • NodeJS全栈WEB3面试题——P8项目实战类问题(偏全栈)
  • 电脑wifi显示已禁用怎么点都无法启用
  • Java线程生命周期详解
  • GStreamer开发笔记(六):gstreamer基本概念:组件、箱柜、管道、衬垫、链接组件
  • 【存储基础】存储设备和服务器的关系和区别
  • 4、ubuntu系统 | 文本和目录操作函数
  • 设备驱动与文件系统:03 生磁盘的使用
  • Python 训练营打卡 Day 33-神经网络
  • SpringMVC的注解
  • Java设计模式之备忘录模式详解
  • 【小沐杂货铺】基于Three.JS构建IFC模型浏览器(WebGL、CAD、Revit、IFC)
  • 使用source ~/.bashrc修改环境变量之后,关闭服务器,在重启,环境变量还有吗?
  • Spark-TTS: AI语音合成的“变声大师“
  • 一步一步配置 Ubuntu Server 的 NodeJS 服务器详细实录——4. 配置服务器终端环境 zsh , oh my zsh, vim
  • isp中的 ISO代表什么意思
  • 实验一:PyTorch基本操作实验
  • 前端八股之CSS
  • 电脑的ip地址会自动变怎么办?原因解析和解决方法
  • Java生态中的NLP框架
  • 探索大语言模型(LLM):RSE流程详解——从文档中精准识别高相关片段
  • 【03】完整开发腾讯云播放器SDK的UniApp官方UTS插件——优雅草上架插件市场-卓伊凡
  • cnn训练并用grad-cam可视化
  • DAY 40 超大力王爱学Python
  • Linux运维笔记:服务器安全加固