Question

I am currently programming in java using proccessing and I am try to make 4 instances...

I am currently programming in java using proccessing and I am try to make 4 instances of the same class collide with one another, I have used a Boolean function to detect when the images are close to each other but I'm unsure of what to do in the next stage, I need them to bounce off each other and go in different directions.

Here is all my code -

Walker Bob, Fred, John, Eric;

void setup()
{
size(500,500);
Bob = new Walker(0,5, 1, 1);
Fred = new Walker(0,120, 1, 1);
John = new Walker(0,240, 1, 1);
Eric = new Walker(0,360, 1, 1);
}

void draw()
{
background(125);
Bob.update();
Fred.update();
John.update();
Eric.update();
}

Walker Class -

{
float x=0;
float y=0;
PImage img1, img2, img3, img4;
float speedX;
float speedY;

Walker(int y, int x, int speedX, int speedY)
{
this.y = y;
this.x = x;
img1 = loadImage("walk1.gif");
img2 = loadImage("walk2.gif");
img3 = loadImage("walk3.gif");
img4 = loadImage("walk4.gif");
this.speedX = speedX;
this.speedY = speedY;
}

void render()
{
if (speedX > 0)
image(img4, x, y);
if (speedX < 1)
image(img3, x, y);
if (speedY < 1 && speedX > 0)
image(img1, x, y);
if (speedY > 0 && speedX < 1)
image(img2, x, y);
{
x=x+speedX;
if (x>=485 || x<=0)
speedX=-speedX;

y=y+speedY;
if (y>=485 || y<=0)
speedY=-speedY;
}
}
void move()
{
x=x+speedX;
y=y+speedY;
}

void update()
{
render();
move();
}
  
boolean crash(Walker other)
{
return (abs(this.x-other.x) < 10 && abs(this.y-other.y) < 10);
}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Create a infinite loop in main class in which you have:

Bob.move();
Fred.move();
John.move();
Eric.move();

After each move check if they crash with all the other classes or not, if they crash:

speedX = -speedX

speedY = -speedY

Add a comment
Know the answer?
Add Answer to:
I am currently programming in java using proccessing and I am try to make 4 instances...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please make this code workable and straight. this code does produce a proper output but just...

    Please make this code workable and straight. this code does produce a proper output but just not in proper format. please solve this. thank you. import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Random; public class MyMaze { private int dimensionX, dimensionY; // dimension of maze private int gridDimensionX, gridDimensionY; // dimension of output grid private char[][] grid; // output grid private Cell[][] cells; // 2d array of Cells private Random random = new Random(); // The random object public MyMaze(int...

  • Java Programming .zip or .jar with 4 files: Shape.java, Square.java, TSquare.java, Triangle.java In this homework, you w...

    Java Programming .zip or .jar with 4 files: Shape.java, Square.java, TSquare.java, Triangle.java In this homework, you will build the below hierarchy: Overview: You will implement the supplier code: Shape, Square, TSquare, and Triangle, which will be used by the Homework7Main program. This program will use the DrawingPanel to draw these objects onto a canvas. Specification: You will take advantage of inheritance with using a super class, Shape.java. (below) Shape is an abstract class, which means it can be extended, but...

  • In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show...

    In Java Swing, in my ButtonHandler class, I need to have 2 different showDialog messages show when my acceptbutton1 is pressed. One message is if the mouse HAS been dragged. One is if the mouse HAS NOT been dragged. /// I tried to make the Points class or the Mouse Dragged function return a boolean of true, so that I could construct an IF/THEN statement for the showDialog messages, but my boolean value was never accepted by ButtonHandler. *************************ButtonHandler class************************************...

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

  • Java Question 4 Hello. I am a new student of java class and I struggle below...

    Java Question 4 Hello. I am a new student of java class and I struggle below question 4. Could anyone please help me? Thank you very much. Card Deck suit: int value: int deck: Cardl deckSize: int + MAXSize = 52: int Deck() Deck(fullDeck:boolean) -initialiseFullDeck()E void rdrawCard0: Card place Card(card : Card): void 0, 52 +Card(value: int, suit: int) +getvalue): int getSuit): int +toString) : String - +shuffle0: void +getDeckSize) int +hasCardsRemaining0: boolean toString0: String sinterfaces> ComparablesCard> + compare To(other...

  • Hello, I am currently taking a Foundations of Programming course where we are learning the basics of Java. I am struggli...

    Hello, I am currently taking a Foundations of Programming course where we are learning the basics of Java. I am struggling with a part of a question assigned by the professor. He gave us this code to fill in: import java.util.Arrays; import java.util.Random; public class RobotGo {     public static Random r = new Random(58);     public static void main(String[] args) {         char[][] currentBoard = createRandomObstacle(createBoard(10, 20), 100);         displayBoard(startNavigation(currentBoard))     }     public static int[] getRandomCoordinate(int maxY, int maxX) {         int[] coor = new...

  • Must be in Java. Show proper reasoning with step by step process. Comment on the code...

    Must be in Java. Show proper reasoning with step by step process. Comment on the code please and show proper indentation. Use simplicity. Must match the exact Output. CODE GIVEN: LineSegment.java LineSegmentTest.java Point.java public class Point { private double x, y; // x and y coordinates of point /** * Creates an instance of Point with the provided coordinates * @param inX the x coordinate * @param inY the y coordinate */ public Point (double inX, double inY) { this.x...

  • I am currently doing homework for my java class and i am getting an error in...

    I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact {       public static void main(String[] args) {        Random Rand = new Random();        Scanner sc = new Scanner(System.in);        System.out.println("welcome to the contact application");        System.out.println();               int die1;        int die2;        int Total;               String choice = "y";...

  • I need help with my java code i am having a hard time compiling it //...

    I need help with my java code i am having a hard time compiling it // Cristian Benitez import java.awt.Color; import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JPanel; Face class public class FaceDraw{ int width; int height; int x; int y; String smileStatus; //default constructor public FaceDraw(){ } // Getters and Setters for width,height,x,y public int getWidth(){ return width; } public void setWidth(int width){ this.width=width; } public int getHeight(){ return height; } public void setHeight(int height){ this.height=height; } public int getX(){ return...

  • JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST...

    JAVA PROGRAM USING ECLIPSE. THE FIRST IMAGE IS THE INSTRUCTIONS, THE SECOND IS THE OUTPUT TEST CASES(WHAT IM TRYING TO PRODUCE) BELOW THOSE IMAGES ARE THE .JAVA FILES THAT I HAVE CREATED. THESE ARE GeometircObject.Java,Point.java, and Tester.Java. I just need help making the Rectangle.java and Rectangle2D.java classes. GeometricObject.Java: public abstract class GeometricObject { private String color = "white"; // shape color private boolean filled; // fill status protected GeometricObject() { // POST: default shape is unfilled blue this.color = "blue";...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT