日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++ 2D小球 碰撞模拟

發布時間:2024/8/1 c/c++ 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++ 2D小球 碰撞模拟 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一個簡單的2D小球碰撞模擬程序,代碼不長主程序90多行。

全部用c++實現,借助easyx繪圖,同時實現簡單向量類便于計算 (默認小球質量相同 大小相同)。

#include <iostream> #include <easyx.h> #include <conio.h> #include <algorithm> #include <vector> #include <cmath> #include <stdlib.h> #include <time.h> #include "Vector2D.h"//2D向量頭 #include "CollisionBox.h"//只有一個球 using namespace std; const int WIDTH = 1100; const int HEIGHT = 600;void CollisionCheck(vector<Ball> &v) { //碰撞檢測/****************為簡化模型 視小球為 “一動一靜” ********************************/for (int i = 0; i < v.size(); i++) {for (int j = i + 1; j < v.size(); j++) {if (pow(v[i].x - v[j].x, 2) + pow(v[i].y - v[j].y, 2) <= 4.0 * v[i].r*v[i].r) {Vector2D vv(v[i].vx - v[j].vx, v[i].vy - v[j].vy); //和速度向量Vector2D v1, v2; //小球1速度向量、小球2速度向量/*****************根據兩球坐標計算兩球碰撞球心連線的向量(方向)**********************/Vector2D t1(v[i].x, v[i].y), t2(v[j].x, v[j].y);v2 = t2 - t1;double angle = Angle(vv, v2); //得到球心連線與和速度夾角double k = Len(vv)*cos(Rad(angle)) / Len(v2);v2.x *= k; //得到小球2的速度向量v2.y *= k;v1 = vv - v2; //計算得到小球1的速度向量/******************至此已經計算出碰撞后兩小球的相對運動狀態*************************/v[i].vx = v1.x + v[j].vx; //再加上之前的速度是因為計算過程是將一個球看做靜態來計算的v[i].vy = v1.y + v[j].vy;v[j].vx += v2.x;v[j].vy += v2.y;}}} } void WallCheck(vector<Ball> &v) {for (int i = 0; i < v.size(); i++) {if (v[i].x + v[i].r >= WIDTH) v[i].vx *= -1; //右壁else if (v[i].y - v[i].r <= 0) v[i].vy *= -1; //上壁else if (v[i].x - v[i].r <= 0) v[i].vx *= -1; //左壁else if (v[i].y + v[i].r >= HEIGHT) v[i].vy *= -1; //下壁} } void CreatBall(vector<Ball> &v) {srand((unsigned int)time(NULL));for (int i = 0; i < 10; i++) { //隨機生成10個小球double x, y, r = 20, vx, vy;bool f = 1;while (f) {f = 0;x = (rand() % (WIDTH - 2 * (int)r))*1.0 + r;y = (rand() % (HEIGHT - 2 * (int)r))*1.0 + r;for (int j = 0; j < v.size(); j++) { //要保證生成的位置不重疊if (pow(v[j].x - x, 2) + pow(v[j].y - y, 2) <= 4.0 * pow(v[j].r, 2)) {f = 1;}}if (!f) {vx = rand() % 6; //速度太快會粘到一起......汗vy = rand() % 6;Ball t(x, y, r, vx, vy);v.push_back(t);}}} } int main() {initgraph(WIDTH, HEIGHT);vector<Ball> v;CreatBall(v);while (1) {BeginBatchDraw();for (int i = 0; i < v.size(); i++) solidcircle(v[i].x, v[i].y, v[i].r);CollisionCheck(v);WallCheck(v);for (int i = 0; i <= 10000000; i++); //適當延時EndBatchDraw();cleardevice();for (int i = 0; i < v.size(); i++) {v[i].x += v[i].vx;v[i].y += v[i].vy;}}closegraph();return 0; }

頭文件:
Vector2D.h

#pragma once #include<cmath> const double PI = 3.1415926; //轉角度 double Deg(double rad) { return rad * 180.0 / PI; } //轉弧度 double Rad(double deg) { return deg / 180.0 * PI; } /*2D向量頭*/ struct Vector2D {Vector2D() :x(), y() {};Vector2D(double px, double py) :x(px), y(py) {};double x, y;Vector2D operator + (const Vector2D &b)const {//減法return Vector2D(this->x + b.x, this->y + b.y);}Vector2D operator - (const Vector2D &b)const {//加法return Vector2D(this->x - b.x, this->y - b.y);}Vector2D operator () (double px, double py) {this->x = px, this->y = py;return *this;}double operator * (const Vector2D &b)const {//向量內積return (this->x * b.x + this->y * b.y);} };double Len(const Vector2D &a) {//向量的模return sqrt(a.x*a.x + a.y*a.y); } double Angle(const Vector2D &a, const Vector2D &b) {//兩個向量夾角return Deg(acos(a*b / (Len(a)*Len(b)))); }

CollisionBox.h

#pragma once struct Ball {double x, y, r;double vx, vy; //x、y軸的的方向向量Ball() :x(), y(), r(), vx(), vy() {}Ball(double sx, double sy, double sr = 30.0, double svx = 3.0, double svy = 0.0):x(sx), y(sy), r(sr), vx(svx), vy(svy) {} };

總結

以上是生活随笔為你收集整理的c++ 2D小球 碰撞模拟的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。