Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7523837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:55:04+00:00 2026-05-30T02:55:04+00:00

Alright, now I am working on a small derp-ish game based on Brownian motion,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:55:05+00:00Added an answer on May 30, 2026 at 2:55 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to objective-c and ios, but I'm coming along alright. Now I've run
Alright, this is driving me nuts because my regex is working on Rubular, but
Alright, so I'm currently working on a game and ran into a memory issue
Alright, i'm making a small pong game, and, i'm making a start menu for
Alright so I've been working on a game engine, and I'm running into a
Just more asynchronous stuff! Alright, so I now have a working asynchronous socket program
Got the guestbook working alright. New messages are building up nicely. Now, only the
Alright so here it goes, I have my user system working perfectly, and now
Alright, so I'm working on a game and I have found out that my
Alright I've been working on this for hours now and can't figure out my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.