- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我遇到了一个我无法弄清楚的错误。我尝试了很多次,碰撞检测和计算新速度似乎很好,但有些球似乎彼此粘在一起,我不知道为什么。你能帮忙吗我出去。
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.Random;
import javax.swing.JFrame;
public class ElasticCollision extends Canvas implements Runnable {
private static final int WIDTH = 300;
private static final int HEIGHT = WIDTH / 16 * 9;
private static final int SCALE = 3;
private static final String TITLE = "Elastic collision";
private boolean running = false;
private JFrame frame;
private Thread thread;
private Random random = new Random();
private Color color;
private int a, b, c;
private Ball[] ball;
private int x = 0, y = 0;
private int radius = 0;
private int speedX = 0, speedY = 0;
private int noOfBalls = 25;
private double newVelX1 = 0, newVelY1 = 0;
private double newVelX2 = 0, newVelY2 = 0;
private double angle1 = 0, angle2 = 0, angle3 = 0;
private int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
public ElasticCollision() {
Dimension size = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
setPreferredSize(size);
frame = new JFrame();
ball = new Ball[noOfBalls];
}
public void start() {
for (int i = 0; i < noOfBalls; i++) {
x = random.nextInt(getWidth());
y = random.nextInt(getHeight());
radius = 25 + random.nextInt(25);
speedX = 1 + random.nextInt(2);
speedY = 1 + random.nextInt(2);
ball[i] = new Ball(x, y, radius, speedX, speedY);
}
running = true;
thread = new Thread(this);
thread.start();
}
public void stop() {
running = false;
}
public void run() {
long lastTime = System.nanoTime();
double unprocessed = 0;
double nsPerTick = 1000000000.0 / 60;
int frames = 0;
int ticks = 0;
long lastTimer = System.currentTimeMillis();
while (running) {
long now = System.nanoTime();
unprocessed += (now - lastTime) / nsPerTick;
lastTime = now;
while (unprocessed >= 1) {
ticks++;
update();
unprocessed -= 1;
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i < noOfBalls; i++) {
for (int j = i + 1; j < noOfBalls; j++) {
if (ball[i].inCollision != true
|| ball[j].inCollision != true)
checkCollision(ball[i], ball[j]);
}
}
frames++;
render();
if (System.currentTimeMillis() - lastTimer > 1000) {
lastTimer += 1000;
frame.setTitle(TITLE + " | " + ticks + " ticks, " + frames
+ " fps");
frames = 0;
ticks = 0;
}
}
stop();
}
public void update() {
for (int i = 0; i < noOfBalls; i++) {
ball[i].x += ball[i].speedX;
ball[i].y += ball[i].speedY;
if (ball[i].x >= getWidth() - ball[i].radius && ball[i].speedX > 0)
ball[i].speedX = -ball[i].speedX;
if (ball[i].x <= ball[i].radius && ball[i].speedX < 0)
ball[i].speedX = -ball[i].speedX;
if (ball[i].y >= getHeight() - ball[i].radius && ball[i].speedY > 0)
ball[i].speedY = -ball[i].speedY;
if (ball[i].y <= ball[i].radius && ball[i].speedY < 0)
ball[i].speedY = -ball[i].speedY;
}
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.yellow);
g.fillRect(0, 0, getWidth(), getHeight());
for (int i = 0; i < noOfBalls; i++)
ball[i].paint(g);
g.dispose();
bs.show();
}
public void checkCollision(Ball ball1, Ball ball2) {
double distance;
if (ball1.x + ball1.radius + ball2.radius > ball2.x
&& ball1.x < ball1.x + ball1.radius + ball2.radius
&& ball1.y + ball1.radius + ball2.radius > ball2.y
&& ball1.y < ball2.y + ball1.radius + ball2.radius) {
distance = Math.sqrt(((ball1.x - ball2.x) * (ball1.x - ball2.x))
+ ((ball1.y - ball2.y) * (ball1.y - ball2.y)));
if ((int) distance < ball1.radius + ball2.radius) {
ball1.collision = true;
ball2.collision = true;
ball1.inCollision = true;
ball2.inCollision = true;
ball1.collisionX = ((ball1.x * ball2.radius) + (ball2.x * ball1.radius))
/ (ball1.radius + ball2.radius) + ball1.radius;
ball1.collisionY = ((ball1.y * ball2.radius) + (ball2.y * ball1.radius))
/ (ball1.radius + ball2.radius) + ball1.radius;
ball2.collisionX = ((ball1.x * ball2.radius) + (ball2.x * ball1.radius))
/ (ball1.radius + ball2.radius) + ball2.radius;
ball2.collisionY =
((ball1.y * ball2.radius) + (ball2.y * ball1.radius))
/ (ball1.radius + ball2.radius) + ball2.radius;
/*
* x1 = (ball1.x - getWidth()) / 2; y1 = (ball2.y - getHeight())
* / 2; angle1 = Math.toDegrees(Math.atan2(y1, x1));
*
* x2 = (ball1.x - getWidth()) / 2; y2 = (ball2.y - getHeight())
* / 2; angle2 = Math.toDegrees(Math.atan2(y2, x2));
*/
double colision_angle = Math.toDegrees(Math.atan2(
(ball2.y - ball1.y), (ball2.x - ball1.x)));
double speed1 = Math.sqrt(ball1.speedX * ball1.speedX
+ ball1.speedY * ball1.speedY);
double speed2 = Math.sqrt(ball2.speedX * ball2.speedX
+ ball2.speedY * ball2.speedY);
double direction1 = Math.atan2(ball1.speedY, ball1.speedX);
double direction2 = Math.atan2(ball2.speedY, ball2.speedX);
double vx_1 = speed1 * Math.cos(direction1 - colision_angle);
double vy_1 = speed1 * Math.sin(direction1 - colision_angle);
double vx_2 = speed2 * Math.cos(direction2 - colision_angle);
double vy_2 = speed2 * Math.sin(direction2 - colision_angle);
double final_vx_1 = ((ball1.radius - ball2.radius) * vx_1 + (ball2.radius + ball2.radius)
* vx_2)
/ (ball1.radius + ball2.radius);
double final_vx_2 = ((ball1.radius + ball1.radius) * vx_1 + (ball2.radius - ball1.radius)
* vx_2)
/ (ball1.radius + ball2.radius);
double final_vy_1 = vy_1;
double final_vy_2 = vy_2;
newVelX1 = (int) (Math.cos(colision_angle) * final_vx_1 + Math
.cos(colision_angle + Math.PI / 2) * final_vy_1);
newVelY1 = (int) (Math.sin(colision_angle) * final_vx_1 + Math
.sin(colision_angle + Math.PI / 2) * final_vy_1);
newVelX2 = (int) (Math.cos(colision_angle) * final_vx_2 + Math
.cos(colision_angle + Math.PI / 2) * final_vy_2);
newVelY2 = (int) (Math.sin(colision_angle) * final_vx_2 + Math
.sin(colision_angle + Math.PI / 2) * final_vy_2);
ball1.speedX = (int) newVelX1;
ball1.speedY = (int) newVelY1;
ball2.speedX = (int) newVelX2;
ball2.speedY = (int) newVelY2;
ball1.x = ball1.x + (int) newVelX1;
ball1.y = ball1.y + (int) newVelY1;
ball2.x = ball2.x + (int) newVelX2;
ball2.y = ball2.y + (int) newVelY2;
}
}
}
public static void main(String[] args) {
ElasticCollision balls = new ElasticCollision();
balls.frame.setResizable(false);
balls.frame.add(balls);
balls.frame.pack();
balls.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
balls.frame.setVisible(true);
balls.start();
}
}
class Ball {
protected int x = 0, y = 0;
protected int radius;
protected int speedX = 0, speedY = 0;
protected boolean collision = false;
protected int collisionX = 0, collisionY = 0;
protected boolean inCollision = false;
public Ball(int x, int y, int radius, int speedX, int speedY) {
this.x = x + radius;
this.y = y + radius;
this.radius = radius;
this.speedX = speedX;
this.speedY = speedY;
}
public void paint(Graphics g) {
if (!collision) {
g.setColor(Color.red);
g.fillOval(x - radius, y - radius, radius * 2, radius * 2);
} else {
g.setColor(Color.green);
g.fillOval(x - radius, y - radius, radius * 2, radius * 2);
g.setColor(Color.blue);
g.fillOval(collisionX - radius, collisionY - radius, 20, 20);
g.setColor(Color.black);
g.drawOval(collisionX - radius, collisionY - radius, 20, 20);
collision = false;
inCollision = false;
}
g.setColor(Color.black);
g.drawOval(x - radius, y - radius, radius * 2, radius * 2);
}
}
最佳答案
我的猜测是,一旦检测到碰撞,您的逻辑就不会让球分开。仅改变一次运动方向,而不是每时每刻球都彼此靠近。
关于java - 球的弹性碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20224547/
我在 AWS Elastic beanstalk 上有一个 tomcat 应用程序,建议 here我正在使用环境属性设置与数据库的 jdbc 连接,此参数包括数据库的用户和密码。问题是,tomcat
经典的Elasticsearch不解析您的日期。也许我开始失明了。我不知道自己在做什么错,我的模板是: "datestamp": {
我想使用 flex 查询在Kibana中创建警报。我正在使用opendistro警报功能。我想检查最近10分钟内cpu.pct字段的所有值是否大于50,如果是,则发出警报。 { "size": 500
我正在使用Kibana中的警报功能,并且想检查字段的最后5个连续值是否超过阈值x,但是如果我在 flex 查询中使用过滤器,则会在前N个聚合之前应用该过滤器。 有没有一种方法可以使用其他选择器或方法在
我们的指数是超标准的。它是平坦的,并使用logstash从oracle / jdbc转储。 我们也使用相当标准的查询语法来查询 flex : { "size": 20, "from"
您好,我正在执行以下查询: { "_source": [ "source1", "source2", "source3", "source4", ], "q
请告诉我正确的方向:) 我有一个任务:通过查询以 flex 搜索方式查找文档,其中可能包含不必要的单词。 我将说明我的意思: 假设我有一些包含单词“ big red car ”的文档(现在它们在索引的
这是我的一份文件可能看起来像的 { "CC":{"colors":["Blue","Green","Yellow"]}, "CN":{"colors":["White"
语境 弹性 6.0.0 我有以下结构: { "age": 24, "blood": 450, "iv": 700, "job": "boss", "damage": "cut,
我可以使文档不可变吗?当文档提交到相同的 id 时,它们是否不会被重写? POST "localhost:9200/index001/_doc/1" // First time it is creat
我正在制作一个图形应用程序,我可以在其中通过拖动多段线的控制点来编辑多段线。但是,我想通过使其具有弹性来使其更易于使用;拖动控制点时,不是移动单个点,我希望也移动该点一定距离内的点,具体取决于“拉”控
我们都知道云计算拥有弹性扩展的特性,所谓的弹性,即云端的可用资源能够随着用户的需求而灵活变化、自由升降,在业务高峰或低谷期,均能匹配适量的资源,既不捉襟见肘、也不过分浪费。弹性云服务器,简单地说,
我有一个索引,其中多个人可以通过他们的姓名+角色分配给一个文档。 到目前为止,我们通过将“人员”添加为嵌套文档并仅将所有相关人员索引到一个文档中来实现搜索。 我们对查询性能不满意,我想尝试从 "peo
我要执行2个不同的批量上传,每次上传的顺序都是完全无法预测的 在一次加载中,我将具有以下字段:SERVER_NAME,OS和PROD_1_VERSION在另一项中,我将具有以下字段:SERVER_NA
如果我有一个索引到Elasticsearch的JSON文档,如下所示: "_source": { "pid_no": 19321, "aggregator_id
我似乎无法按顺序排列 flex 查询的结果。 我将内存利用率统计信息存储在elasticsearch中。文档示例如下所示: { "Component": "ABC", "memUsage"
给定输入“快速的棕色狐狸跳”,我想为单词创建每种可能的 token 组合。因此,示例字符串将被标记为 [ "quick", "quick brown", "quick fox", "quick jum
我基本上是在尝试编写一个查询,它应该在哪里返回文档 学校是“神圣的国际”,成绩是“第二”。 但当前查询的问题在于它没有考虑必须匹配查询部分。即,即使我没有指定学校给我这份文件,因为它不匹配。 查询给了
我正在尝试在Kibana Canvas 中设置日期格式,因此我遵循了此文档: https://www.elastic.co/blog/kibana-canvas-data-table-and-debu
在用 Flex 编写 token 生成器时,我遇到了这个恼人的错误:“无法识别的规则” 我的代码是: /* Keywords */ TYPE int|double|bool|char L
我是一名优秀的程序员,十分优秀!