java 小球碰撞 条件是球心距离 导致"黏住"2个球相撞,2个球心在一定距离内就可以认为相撞 。
两球相撞反弹运动 if((x1-x20)(y1-y20)){ if((x1-x2=r)(y1-y2=r)){ dr1=3; dr2=1;} } if((x1-x20)(y1-y20)){ if((x1-x2=r)(y2-y1=r)){ dr1=0; dr2=2;} } if((x2-x10)
JAVA如何实现小球的弹性碰撞我没用java写过代码,所以我只说算法,代码你自己翻译下
按C的语法来:
void xiaoqiu
{
int UB=10,DB=200,LB=10,RB=200;//定义弹球范围的边界
int sh=1;//定义横向步长
int sz=1;//定义纵向步长(两步长之比决定了反弹的角度)
int x=LB,y=UB;//定义坐标
int i=10000;//循环次数(自己选择跳出手段)
while(i0)
{
i--;
x=x+sh;
if(x=RB||x=LB) sh=-sh;//碰壁后步长变反
y=y+sz;
if(y=DB||y=UB) sz=-sz;//碰壁后步长变反
(显示代码)
}
return;
}
总得来说,就是相当于横向和纵向分别处理移动、反弹的问题,碰壁后步长变为相反数
【java碰撞小球代码 js两个小球碰撞】不懂请追问
JAVA做一个作业,制作一个window框里面有三个小球碰撞,碰到边框或者小球变色加反弹,错误代码求指点没办法了 只能注册一个马甲了
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
public class yes {
public static void main(String args[])
{
Mywindow win=new Mywindow();
Ball qiu1=new Ball(5,5);
Thread thread1=new Thread(qiu1);
Ball qiu2=new Ball(10,5);
Thread thread2=new Thread(qiu2);
Ball qiu3=new Ball(15,5);
Thread thread3=new Thread(qiu3);
thread1.start();
thread2.start();
thread3.start();
}
}
class Mywindow extends Frame {
Mywindow (){
setSize(350,350);
setVisible(true);
setBackground(Color.BLACK);
validate();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
}
class Ball extends Mywindow implements Runnable
{
int rgb=0;
Color color;
int x,y;
int dx=5,dy=5;
Ball(int x,int y){
this.x=x;
this.y=y;
}
public void doColor(){
rgb=(int)(Math.random()*0xFFFFFF);
color=new Color(rgb);
}
public void paint(Graphics g){
g.setColor(color);
g.fillOval(x,y,50,50);
}
public void run(){
while(true){
if(x=0) {dx=5;doColor();}
else if((x+50)=getWidth()) {dx=-5; doColor();}
if(y=0) {dy=5;doColor();}
else if((y+50)=getHeight()) {dy=-5; doColor();}
x=x+dx;
y=y+dy;
repaint();
try{Thread.sleep(50);}
catch(InterruptedException e) {;}
}
}
}
java小球碰撞窗体边缘来回反弹的代码import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RunningBallDemo extends JFrame {
public static void main(String args[]) {
new RunningBallDemo();
}
public RunningBallDemo() {
Ball ballPanel = new Ball(5, 5);
getContentPane().add(ballPanel);
setBackground(Color.BLACK);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setSize(350, 350);
setVisible(true);
Thread thread1 = new Thread(ballPanel);
thread1.start();
}
}
class Ball extends JPanel implements Runnable {
int rgb = 0;
Color color;
int x, y;
int dx = 5, dy = 5;
Ball(int x, int y) {
this.x = x;
this.y = y;
推荐阅读
- 怎么让pdf变黑白色,怎么把pdf变成黑色
- js如何设置浏览器信息,js调用默认浏览器打开
- 手机看电视就黑屏了怎么办,手机看电视黑屏什么原因
- 神秘虎牙直播软件下载,虎牙神秘狗表情包
- mysql怎么删除子信息 mysql删除字段的语句
- 怎么在硬盘里下载资源,怎么下载到硬盘
- 无线4g路由器怎么改名字,无线4g路由器怎么改名字和密码
- 直播间卖货可以吗,直播间卖货可以吗现在
- 接口常用的linux命令 linux的接口类型