網(wǎng)站首頁 編程語言 正文
前言
本文將基于Android對一個小球自由碰撞動畫項(xiàng)目做一個簡單介紹,并展示部分關(guān)鍵代碼以及實(shí)際效果動態(tài)圖
1. add balls List
添加顏色為綠,黃,紅,黑,藍(lán),灰的小球,并設(shè)置其大小
ballList.add(new Ball(80, Color.GREEN, 700, width / 2, height / 2)); ballList.add(new Ball(100, Color.YELLOW, 600, width / 4 * 3, height / 4 * 3)); ballList.add(new Ball(120, Color.RED, 800, width / 3 * 3, height / 3 * 3)); ballList.add(new Ball(100, Color.BLACK, 800, width / 3 * 4, height / 3 * 4)); ballList.add(new Ball(120, Color.BLUE, 800, width / 5 * 3, height / 5 * 3)); ballList.add(new Ball(100, Color.DKGRAY, 800, width / 4 * 5, height / 5 * 4));
2.ball parameter
int radius; //半徑大小 long speed =-1; //碰撞速度 int color; //顏色 long degree =-1; //初始方向值 int bgAlpha; //透明度 long lifeSpan =-1;
3. 判斷是否有發(fā)生碰撞的小球
關(guān)鍵代碼
private void checkoutBall() { for (int i = 0; i < ballList.size(); i++) { Ball ball1 = ballList.get(i); Ball ball2 = (Ball) distanceMap.get(ball1); if (ball2.getX() == -1 && ball2.getY() == -1) { continue; } int distance2 = Math.abs(ball1.getX() - ball2.getX()) * Math.abs(ball1.getX() - ball2.getX()) + Math.abs(ball1.getY() - ball2.getY()) * Math.abs(ball1.getY() - ball2.getY()); //最小距離小于閾值,發(fā)生碰撞 計(jì)算碰撞角度 if (distance2 <= (ball1.getRadius() + ball2.getRadius()) * (ball1.getRadius() + ball2.getRadius())) { int x1 = ball1.getX(); int y1 = ball1.getY(); int x2 = ball2.getX(); int y2 = ball2.getY(); //兩球的角度差為180 即兩球正碰 交換運(yùn)動方向 if (Math.abs(ball1.getDegree() - ball2.getDegree()) == 180) { int temp = ball1.getDegree(); ball1.setDegree(ball2.getDegree()); ball2.setDegree(temp); } else if (y1 == y2) { //正弦值不存在 角度為90 if (x1 > x2) { ball1.setDegree(90); ball2.setDegree(270); } else { ball1.setDegree(270); ball2.setDegree(90); } } else { //斜碰 兩球圓心連接線的角度做反向運(yùn)動 float tan = (x1 - x2) / (y1 - y2); //正弦對應(yīng)的角度在[-90,90] 在這取絕對值讓角度值為正值 int angle = (int) Math.abs(Math.atan(tan)); if (x1 > x2 && y1 > y2) { //正弦值為正數(shù) 角度為[0,90] ball1在右下 ball2在左上 ball1.setDegree(angle + 90); ball2.setDegree(angle + 270); } else if (x1 > x2 && y1 < y2) { //正弦值為負(fù)數(shù) 角度為[-90,0] ball1在右上 ball2在左下 ball1.setDegree(90 - angle); ball2.setDegree(270 - angle); } else if (x1 < x2 && y1 > y2) { //正弦值為負(fù)數(shù) 角度為[-90,0] ball1在右下 ball2在左上 ball1.setDegree(270 - angle); ball2.setDegree(90 - angle); } else if (x1 < x2 && y1 < y2) { //正弦值為正數(shù) 角度為[0,90] ball1在左上 ball2在右下 ball1.setDegree(angle + 270); ball2.setDegree(angle + 90); } else if (x1 == x1) { //正弦值為0 angle為0 if (y1 > y2) { ball1.setDegree(180); ball2.setDegree(angle); } else { ball1.setDegree(angle); ball2.setDegree(180); } } } //發(fā)生碰撞 重新初始化數(shù)據(jù) distanceMap.put(ball2, new Ball()); distanceMap.put(ball1, new Ball()); } } }
1.collision detection
2.碰撞后重新調(diào)用onDraw(繪制小球)
private class MyThread extends Thread { @Override public void run() { while (true) { actionBall(); postInvalidate(); // 更新界面,重新調(diào)用onDraw() try { // sleep(1000 / 36); sleep(10); } catch (Exception e) { e.printStackTrace(); } } } }
onDraw
protected void onDraw(Canvas canvas) { super.onDraw(canvas); for (int i = 0; i < ballList.size(); i++) { Ball ball = ballList.get(i); Paint paint = new Paint(); paint.setColor(ball.getColor()); paint.setAlpha(ball.getAlpha()); paint.setStyle(Paint.Style.FILL); paint.setStrokeWidth(1); canvas.drawCircle(ball.getX(), ball.getY(), ball.getRadius(), paint); } }
4.application display
1.靜態(tài)界面
2.動態(tài)效果圖
原文鏈接:https://blog.csdn.net/jyb_007/article/details/121888916
相關(guān)推薦
- 2022-11-04 C++多態(tài)特性之派生與虛函數(shù)與模板詳細(xì)介紹_C 語言
- 2022-03-19 K8S中五種控制器的介紹以及使用_云其它
- 2022-07-22 定時任務(wù)和延時任務(wù)詳解
- 2022-04-11 python中pip安裝、升級以及升級固定的包_python
- 2022-06-10 阿里云日志服務(wù)日志過濾器配置_服務(wù)器其它
- 2023-11-16 python 插值 —— 如何實(shí)現(xiàn)插值,以及錯誤ValueError: A value in x_n
- 2023-11-11 Flask 表單form.validate_on_submit()什么情況下會是false——解決辦
- 2022-12-15 詳解Golang如何比較兩個slice是否相等_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支