I have a somewhat (read: EXTREMELY) broken hitwall function and I don't see what I am doing wrong

StackOverflow https://stackoverflow.com/questions/9346319

  •  30-04-2021
  •  | 
  •  

Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top