当前位置: 首页 > 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://www.lqws.cn/news/76447.html

相关文章:

  • android binder(二)应用层编程实例
  • 循序渐进 Android Binder(一):IPC 基本概念和 AIDL 跨进程通信的简单实例
  • 基于 Android 和 JBox2D 的简单小游戏
  • 实验一:PyTorch基本操作实验
  • 力扣热题100之对称二叉树
  • LeetCode 热题 100 394. 字符串解码
  • C#项目07-二维数组的随机创建
  • CppCon 2014 学习:Exception-Safe Coding
  • Python----目标检测(《YOLOv3:AnIncrementalImprovement》和YOLO-V3的原理与网络结构)
  • Python----目标检测(训练YOLOV8网络)
  • FreeBSD 14.3 候选版本附带 Docker 镜像和关键修复
  • 嵌入式鸿蒙开发环境搭建操作方法与实现
  • web架构3------(nginx的return跳转,gzip压缩,目录浏览,访问控制和location符号优先级)
  • 分布式锁剖析
  • 2025/6月最新Cursor(0.50.5版本)一键自动更换邮箱无限续杯教程
  • 05.MySQL表的约束
  • 牛客小白月赛117
  • Linux 权限管理入门:从基础到实践
  • OpenCV4.4.0下载及初步配置(Win11)
  • PCA(K-L变换)人脸识别(python实现)
  • 从【0-1的HTML】第1篇:HTML简介
  • C++ - 标准库之 <sstream> ostringstream(ostringstream 概述、基本使用、清空内容、进阶使用)
  • 房屋租赁系统 Java+Vue.js+SpringBoot,包括房屋信息、看房申请、租赁合同、房屋报修、收租信息、维修数据、租客管理、公告管理模块
  • 系统调用与程序接口的关系
  • [leetcode] 二分算法
  • 线程间和进程间是如何进行通信
  • day17 leetcode-hot100-34(链表13)
  • Oracle的Hint
  • 【笔记】Windows系统部署suna基于 MSYS2的Poetry 虚拟环境backedn后端包编译失败处理
  • (九)学生写作画像可视化