多个小球碰撞

//反弹球之间相互碰撞,碰撞为理想的弹性碰撞,两球相撞之后交换速度#include <graphics.h>#include <conio.h>#include <stdlib.h>#include <math.h>#define high 480#define width 640#define ballnum 15int mai...
摘要由CSDN通过智能技术生成
//反弹球之间相互碰撞,碰撞为理想的弹性碰撞,两球相撞之后交换速度
#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#define high 480
#define width 640
#define ballnum 15

int main()
{
    float ball_x[ballnum],ball_y[ballnum];
    float ball_vx[ballnum],ball_vy[ballnum];
    float radius;
    int i,j;
    radius=20;
    for(i=0;i<ballnum;i++)
    {
        ball_x[i]=rand()%int (width-4*radius)+2*radius;
        ball_y[i]=rand()%int (high-4*radius)+2*radius;
        ball_vx[i]=(rand()%2)*2-1;
        ball_vy[i]=(rand()%2)*2-1;
        
    }
    initgraph(width,high);
    BeginBatchDraw();
    while(1)
    {
        setcolor(BLACK);
        setfillcolor(BLACK);
        for(i=0;i<ballnum;i++)
        {
            fillcircle(ball_x[i],ball_y[i],radius);
        }
        for(i=0;i<ballnum;i++)
        {
            ball_x[i]=ball_x[i]
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Vue 中实现多个小球碰撞,可以按照以下步骤进行: 1. 创建一个 Ball 组件,用于显示单个小球的界面。这个组件需要包含小球的位置、速度等信息,并且需要实现小球的移动和碰撞检测等功能。 ``` <template> <div :style="{ left: x + 'px', top: y + 'px' }"></div> </template> <script> export default { props: { x: Number, y: Number, speedX: Number, speedY: Number, radius: Number, color: String, bounds: Object }, data() { return { timerId: null } }, created() { this.timerId = setInterval(this.move, 10); }, methods: { move() { let x = this.x + this.speedX; let y = this.y + this.speedY; if (x - this.radius < this.bounds.left || x + this.radius > this.bounds.right) { this.speedX = -this.speedX; } if (y - this.radius < this.bounds.top || y + this.radius > this.bounds.bottom) { this.speedY = -this.speedY; } this.$emit('update:x', x); this.$emit('update:y', y); } } } </script> <style> div { position: absolute; width: 20px; height: 20px; border-radius: 50%; } </style> ``` 2. 在父组件中,创建多个 Ball 组件,并为每个组件设置不同的位置、速度等信息。同时,还需要为每个组件设置边框信息,用于碰撞检测。 ``` <template> <div ref="container" style="position: relative; width: 400px; height: 400px; border: 1px solid black;"> <Ball v-for="(ball, index) in balls" :key="index" :x.sync="ball.x" :y.sync="ball.y" :speedX="ball.speedX" :speedY="ball.speedY" :radius="ball.radius" :color="ball.color" :bounds="bounds" /> </div> </template> <script> import Ball from './Ball.vue'; export default { components: { Ball }, data() { return { balls: [ { x: 50, y: 50, speedX: 5, speedY: 3, radius: 10, color: 'red' }, { x: 100, y: 100, speedX: -3, speedY: 4, radius: 15, color: 'blue' }, { x: 150, y: 150, speedX: 4, speedY: -5, radius: 20, color: 'green' } ], bounds: { left: 0, top: 0, right: 400, bottom: 400 } } } } </script> ``` 3. 在 Ball 组件中,实现碰撞检测的功能。可以在 move 方法中,遍历所有其他的 Ball 组件,检测当前小球是否与其他小球相交。如果相交了,就需要计算当前小球的新速度,并更新位置。 ``` move() { let x = this.x + this.speedX; let y = this.y + this.speedY; let radius = this.radius; if (x - radius < this.bounds.left || x + radius > this.bounds.right) { this.speedX = -this.speedX; } if (y - radius < this.bounds.top || y + radius > this.bounds.bottom) { this.speedY = -this.speedY; } this.balls.forEach(ball => { if (ball !== this) { let dx = ball.x - x; let dy = ball.y - y; let distance = Math.sqrt(dx * dx + dy * dy); if (distance < radius + ball.radius) { let angle = Math.atan2(dy, dx); let sin = Math.sin(angle); let cos = Math.cos(angle); // 旋转坐标系 let vx1 = this.speedX * cos + this.speedY * sin; let vy1 = this.speedY * cos - this.speedX * sin; let vx2 = ball.speedX * cos + ball.speedY * sin; let vy2 = ball.speedY * cos - ball.speedX * sin; // 碰撞后的新速度 let vx1Final = ((this.radius - ball.radius) * vx1 + (2 * ball.radius) * vx2) / (this.radius + ball.radius); let vx2Final = ((2 * this.radius) * vx1 + (ball.radius - this.radius) * vx2) / (this.radius + ball.radius); // 更新速度 this.speedX = vx1Final * cos - vy1 * sin; this.speedY = vy1 * cos + vx1Final * sin; ball.speedX = vx2Final * cos - vy2 * sin; ball.speedY = vy2 * cos + vx2Final * sin; // 更新位置 x += this.speedX; y += this.speedY; ball.x += ball.speedX; ball.y += ball.speedY; } } }); this.$emit('update:x', x); this.$emit('update:y', y); } ``` 通过以上步骤,就可以实现多个小球碰撞,并且在 Vue 中动态更新小球的位置和速度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值