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
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/83381.shtml
英文地址,請注明出處:http://en.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應用上下文在啟動時會觸發多種內置事件…