我有一个有点(读:极其)损坏的撞墙功能,我不明白我做错了什么

发布于 2025-01-07 03:15:23 字数 2393 浏览 2 评论 0原文

好吧,现在我正在开发一个基于布朗运动的小型游戏,并且我正在研究粒子的撞墙功能,但它不起作用。基本上,我用弧度表示粒子的方向,每当它撞到墙壁时,我都会在方向上添加 Pi 弧度以翻转它,但由于某种原因,它要么没有被调用,要么不起作用。这些是我的代码片段,欢迎任何建议。

import java.awt.*;


public class Particle implements Actor {
protected double xPos;
protected double yPos;
protected Velocity v;
protected Color hue;
protected boolean needsUpdate;

public Particle(){
    this((Math.random()*500),(Math.random()*500),new Velocity((int)(Math.random()*500),(Math.random()*Velocity.TAU)));
}

public Particle(double x, double y, Velocity vel){
    xPos=x;
    yPos=y;
    v=vel;
    hue=Color.red;
    needsUpdate=false;
}

public void draw(Graphics g) {
    g.setColor(hue);
    g.fillOval((int)xPos, (int)yPos, 16, 16);
}

public void act() {
    xPos+=v.getSlopefromDirection();
    yPos+=1;
}

public void onHitWall(int dir) {
    v.setDirection((v.getDirection()+Math.PI)%(Math.PI*2));     
}

public void onHitOther(Actor other) {

}

public boolean canCollide() {
    return true;
}

public int getLeftX() {
    return (int)xPos;
}

public int getRightX() {
    return (int)xPos+4;
}

public int getTopY() {
    return (int)yPos;
}

public int getBottomY() {
    return (int)yPos+4;
}

是我用来显示它的类:

import java.awt.*;
import java.util.*;
import javax.swing.*;
import static java.lang.System.out;

public class Feild extends JFrame{
protected ArrayList<Actor> actors;
private final int size=500;
protected Thread th;

public Feild(ArrayList<Actor> a){
    super("A Brownian Love Story");
    setSize(size, size);
    actors=a;
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    th = new Thread();
    while(true){
        paint(getGraphics());
    }

}

public void paint(Graphics g){
    super.paint(g);
    //g.fillRect(0, 0, 500, 500);
    for(int i=0;i<actors.size();i++){
        if(actors.get(i).getLeftX()<=0)
            actors.get(i).onHitWall(1);
        if(actors.get(i).getRightX()>=500)
            actors.get(i).onHitWall(2);
        if(actors.get(i).getTopY()<=0)
            actors.get(i).onHitWall(1);
        if(actors.get(i).getBottomY()>=500)
            actors.get(i).onHitWall(2);
        actors.get(i).act();
        actors.get(i).draw(g);
    }
    for(int i=0;i<1000;i++){out.println(i);}
}



}

所以我尝试将绘制函数更改为在执行后检查碰撞,并且它看起来仍然像 onHitWall 被跳过,即使在放入打印语句后它没有被跳过。

Alright, now I am working on a small derp-ish game based on Brownian motion, and I am working on a hit-wall function for the particles, but it isn't working. Basically, I am expressing the direction of the particles in radians, and whenever it hits a wall, I add Pi radians to the direction to flip it, but for some reason, it either isn't getting called or not working. These are the peices of code I have, any suggestions are welcome.

import java.awt.*;


public class Particle implements Actor {
protected double xPos;
protected double yPos;
protected Velocity v;
protected Color hue;
protected boolean needsUpdate;

public Particle(){
    this((Math.random()*500),(Math.random()*500),new Velocity((int)(Math.random()*500),(Math.random()*Velocity.TAU)));
}

public Particle(double x, double y, Velocity vel){
    xPos=x;
    yPos=y;
    v=vel;
    hue=Color.red;
    needsUpdate=false;
}

public void draw(Graphics g) {
    g.setColor(hue);
    g.fillOval((int)xPos, (int)yPos, 16, 16);
}

public void act() {
    xPos+=v.getSlopefromDirection();
    yPos+=1;
}

public void onHitWall(int dir) {
    v.setDirection((v.getDirection()+Math.PI)%(Math.PI*2));     
}

public void onHitOther(Actor other) {

}

public boolean canCollide() {
    return true;
}

public int getLeftX() {
    return (int)xPos;
}

public int getRightX() {
    return (int)xPos+4;
}

public int getTopY() {
    return (int)yPos;
}

public int getBottomY() {
    return (int)yPos+4;
}

}

And this is the class I am using to display it:

import java.awt.*;
import java.util.*;
import javax.swing.*;
import static java.lang.System.out;

public class Feild extends JFrame{
protected ArrayList<Actor> actors;
private final int size=500;
protected Thread th;

public Feild(ArrayList<Actor> a){
    super("A Brownian Love Story");
    setSize(size, size);
    actors=a;
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    th = new Thread();
    while(true){
        paint(getGraphics());
    }

}

public void paint(Graphics g){
    super.paint(g);
    //g.fillRect(0, 0, 500, 500);
    for(int i=0;i<actors.size();i++){
        if(actors.get(i).getLeftX()<=0)
            actors.get(i).onHitWall(1);
        if(actors.get(i).getRightX()>=500)
            actors.get(i).onHitWall(2);
        if(actors.get(i).getTopY()<=0)
            actors.get(i).onHitWall(1);
        if(actors.get(i).getBottomY()>=500)
            actors.get(i).onHitWall(2);
        actors.get(i).act();
        actors.get(i).draw(g);
    }
    for(int i=0;i<1000;i++){out.println(i);}
}



}

So i tried changing the paint function to checking for collisions after acting, and it still looks like onHitWall is getting skipped over, even though after putting in a print statement it isn't.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隐诗 2025-01-14 03:15:23

你遇到的问题是你的粒子会移动,直到它们撞到墙上,然后当你下次查看时,它们仍然在墙上,再次转身,并继续进入墙壁。

如果您在 act() 函数之后检查碰撞,这应该可以解决问题。

他们会进入墙内,看到自己在墙内,然后转身。下一个循环,他们将移回墙外,并继续前进。

The problem you have is that your particles will move until they hit a wall, and then the next time you go through the look, they will still be in the wall, turn around again, and continue going into the wall.

If you check for collisions after your act() function this should solve the problem.

They will go into the wall, see they are in the wall, and turn around. The next loop, they will move back out of the wall, and continue on.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文