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.pswp.cn/bicheng/83381.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Redis持久化机制详解:RDB与AOF的深度剖析

一、为什么需要持久化&#xff1f; Redis作为内存数据库&#xff0c;数据存储在易失性内存中。持久化机制解决两大核心问题&#xff1a; 数据安全&#xff1a;防止服务器宕机导致数据丢失灾难恢复&#xff1a;支持数据备份与快速重建 二、RDB&#xff1a;内存快照持久化 ▶ …

Netty学习example示例

文章目录 simpleServer端NettyServerNettyServerHandler Client端NettyClientNettyClientHandler tcp&#xff08;粘包和拆包&#xff09;Server端NettyTcpServerNettyTcpServerHandler Client端NettyTcpClientNettyTcpClientHandler protocolcodecCustomMessageDecoderCustomM…

ThreadLocal ,底层原理,强引用,弱引用,内存泄漏

目录 ThreadLocal的基本概念 底层实现原理 强引用与弱引用 内存泄漏问题 内存泄漏的解决方案 示例代码 ThreadLocal的基本概念 ThreadLocal是Java中的一个类&#xff0c;位于java.lang包下&#xff0c;它提供了线程局部变量的功能。每个使用该变量的线程都有自己独立的初…

TomSolver 库 | config详解及其测试

一、C 关键特性解析 1. enum class 强类型枚举 enum class LogLevel { OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL }; enum class NonlinearMethod { NEWTON_RAPHSON, LM };核心特性&#xff1a; 类型安全&#xff1a;禁止隐式转换为整数作用域限定&#xff1a;必须…

【DB2】ERRORCODE=-4499, SQLSTATE=08001

客户在连接DB2压测时报错ERRORCODE-4499, SQLSTATE08001&#xff0c;连接失败&#xff0c;主要是因为通信失败 在本地进行复现&#xff0c;用DBeaver代替java程序&#xff0c;将DB2COMM从TCPIP置为空&#xff0c;重启后重新连接&#xff0c;报一样的错误 而将防火墙开启&…

MicroPython+L298N+ESP32控制电机转速

要使用MicroPython控制L298N电机驱动板来控制电机的转速&#xff0c;你可以通过PWM&#xff08;脉冲宽度调制&#xff09;信号来调节电机速度。L298N是一个双H桥驱动器&#xff0c;可以同时控制两个电机的正反转和速度。 硬件准备&#xff1a; 1. L298N 电机控制板 2. ESP32…

WPF 全局加载界面、多界面实现渐变过渡效果

WPF 全局加载界面与渐变过渡效果 完整实现方案 MainWindow.xaml <Window x:Class"LoadingScreenDemo.MainWindow"xmlns"http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x"http://schemas.microsoft.com/winfx/2006/xaml&quo…

RabbitMQ深度解析:从基础实践到高阶架构设计

引言​​ 在分布式系统与微服务架构主导的现代软件开发中&#xff0c;服务间通信的可靠性、异步处理能力及流量管控成为核心挑战。​​RabbitMQ​​作为基于AMQP协议的企业级消息中间件&#xff0c;凭借其灵活的路由机制、高可用架构与丰富的扩展能力&#xff0c;成为异步通信…

华为OD机试真题——矩形相交的面积(2025A卷:100分)Java/python/JavaScript/C/C++/GO最佳实现

2025 A卷 100分 题型 本专栏内全部题目均提供Java、python、JavaScript、C、C++、GO六种语言的最佳实现方式; 并且每种语言均涵盖详细的问题分析、解题思路、代码实现、代码详解、3个测试用例以及综合分析; 本文收录于专栏:《2025华为OD真题目录+全流程解析+备考攻略+经验分…

基于随机函数链接神经网络(RVFL)的锂电池健康状态(SOH)预测

基于随机函数链接神经网络(RVFL)的锂电池健康状态(SOH)预测 一、RVFL网络的基本原理与结构 随机向量功能链接(Random Vector Functional Link, RVFL)网络是一种单隐藏层前馈神经网络的随机化版本,其核心特征在于输入层到隐藏层的权重随机生成且固定,输出层权重通过最…

阿里云国际站,如何通过代理商邀请的链接注册账号

阿里云国际站&#xff1a;如何通过代理商邀请链接注册&#xff0c;解锁“云端超能力”与专属福利&#xff1f; 渴望在全球化浪潮中抢占先机&#xff1f;想获得阿里云国际站的海量云资源、遍布全球的加速节点与前沿AI服务&#xff0c;同时又能享受专属折扣、VIP级增值服务支持或…

PMOS以及电源转换电路设计

PMOS的使用 5V_EN5V时&#xff0c;PMOS截止&#xff1b; 5V_EN0V时&#xff0c;PMOS导通&#xff1b; 电源转换电路 当Vout0V时&#xff0c;Vg0V, Vgs>Vth, PMOS导通&#xff0c;只有电池供电&#xff1b; 当Vout5V时&#xff0c;Vg4.9V, Vs4.8V?, Vgs<Vth, PMOS截止&am…

云时代:DMZ安全架构的演进与实践

随着云计算的普及,传统的DMZ安全边界正在经历根本性变革。本文探讨如何在云环境中重新设计和实现DMZ架构,以应对现代安全挑战。 1. 传统DMZ与云DMZ的对比 传统DMZ(隔离区)是网络安全的经典架构,但云环境带来了新的挑战: 特性传统DMZ云DMZ物理边界明确的物理网络分区虚拟网…

mqtt协议连接阿里云平台

首先现在的阿里云物联网平台已经不在新购了&#xff0c;如下图所示&#xff1a; 解决办法&#xff1a;在咸鱼上租用一个账号&#xff0c;先用起来。 搭建阿里云平台&#xff0c;参考博客&#xff1a; &#xff08;一&#xff09;MQTT连接阿里云物联网平台&#xff08;小白向&…

职业本科院校无人机专业人才培养解决方案

2023年的中央经济工作会议强调了以科技创新推动现代化产业体系构建的重要性&#xff0c;并提出发展生物制造、商业航天、低空经济等战略性新兴产业。低空经济&#xff0c;依托民用无人机等低空飞行器&#xff0c;在多场景低空飞行活动的牵引下&#xff0c;正逐步形成一个辐射广…

Go语言字符串类型详解

1. 定义字符串类型 package mainimport ("fmt");func main() {var str1 string "你好 GoLang 1"var str2 "你好 GoLang 2"str3 : "你好 GoLang 3"fmt.Printf("%v--%T\n", str1, str1)// 你好 GoLang 1--stringfmt.Printf…

设计模式——中介者设计模式(行为型)

摘要 文章详细介绍了中介者设计模式&#xff0c;这是一种行为型设计模式&#xff0c;通过中介者对象封装多个对象间的交互&#xff0c;降低系统耦合度。文中阐述了其核心角色、优缺点、适用场景&#xff0c;并通过类图、时序图、实现方式、实战示例等多方面进行讲解&#xff0…

也说字母L:柔软的长舌

英语单词 tongue&#xff0c;意为“舌头” tongue n.舌&#xff0c;舌头&#xff1b;语言 很显然&#xff0c;“语言”是引申义&#xff0c;因为语言是抽象的&#xff0c;但舌头是具象的&#xff0c;根据由简入繁的原则&#xff0c;tongue显然首先是象形起义&#xff0c;表达…

性能测试实例(http和ldap协议压测)

一、某授权服务器生成授权码效率验证&#xff08;http协议&#xff09; 测试背景 在存量数据23万条的情况下&#xff0c;生成一条授权数据&#xff0c;需要10秒左右&#xff0c;用户反应数据生成效率太差&#xff0c;需要优化。初步判断是由于在授权数据生成时&#xff0c;有查…

Spring Boot中的事件与JMS消息集成

Spring Boot事件机制 Spring框架的事件处理是其核心特性之一,通过ApplicationEvent类和ApplicationListener接口实现。在Spring Boot应用中,事件机制是实现模块间消息传递的重要方式,通常用于业务逻辑内部通信。 内置事件类型 Spring应用上下文在启动时会触发多种内置事件…