Reputation: 1507
I'm trying to create an applet in which the user gets a sheep into a pen by moving a dog towards the sheep and making the sheep move away from the dog in a random direction. The sheep and dog are defined as objects.
The applet is still in its very early stages. So far I can drag the dog object around the screen and when the dog object comes close to the sheep object it moves but only within a certain area (within bounds I have set). I'm not looking for the solution I'm just looking for some help.
What I would like help with is making the sheep object move in a random direction away from the dog when the dog object comes within the bounds I have set and not just move within the set bounds. Any help or tips would be greatly appreciated. Here is my code:
package mandAndDog;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class SheepDog extends Applet implements ActionListener, MouseListener, MouseMotionListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
Dog dog;
Sheep sheep;
int xposR;
int yposR;
int sheepx;
int sheepy;
int sheepBoundsx;
int sheepBoundsy;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
dog = new Dog(10, 10);
sheep = new Sheep(200, 100);
sheepx = 175;
sheepy = 75;
sheepBoundsx = 50;
sheepBoundsy = 50;
}
public void paint(Graphics g)
{
dog.display(g);
sheep.display(g);
}
public void actionPerformed(ActionEvent ev)
{}
public void mousePressed(MouseEvent e)
{}
public void mouseReleased(MouseEvent e)
{}
public void mouseEntered(MouseEvent e)
{}
public void mouseExited(MouseEvent e)
{}
public void mouseMoved(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{}
public void mouseDragged(MouseEvent e)
{
dog.setLocation(xposR, yposR);
if (xposR > sheepx&& xposR < sheepx+sheepBoundsx && yposR > sheepy
&& yposR < sheepy+sheepBoundsy){
sheep.setLocation(xposR + 50, yposR + 50);
}
xposR = e.getX();
yposR = e.getY();
repaint();
}
}
class Dog
{
int xpos;
int ypos;
int circleWidth = 30;
int circleHeight = 30;
public Dog(int x, int y)
{
xpos = x;
ypos = y;
}
public void setLocation(int lx, int ly)
{
xpos = lx;
ypos = ly;
}
public void display(Graphics g)
{
g.setColor(Color.blue);
g.fillOval(xpos, ypos, circleWidth, circleHeight);
}
}
class Sheep
{
int xpos;
int ypos;
int circleWidth = 10;
int circleHeight = 10;
public Sheep(int x, int y)
{
xpos = x;
ypos = y;
}
public void setLocation(int lx, int ly)
{
xpos = lx;
ypos = ly;
}
public void display(Graphics g)
{
g.setColor(Color.green);
g.fillOval(xpos , ypos, circleWidth, circleHeight);
}
}
Upvotes: 2
Views: 8317
Reputation: 513
You aren't looking for code, so here is what what I would do. This is very basic, and could yield predicable results due to the randomness of the JVM however, I like to use the built-in Math.random() function to generate a random number and the modulus operator to bound the random number to some integer value between 0 and an upper bound.
I would recommend calculating the direction and distance of travel as a vector.
int MAX_DISTANCE = 50;
int direction = (int) (Math.random() * 360) % 360;
int distance = (int) (Math.random() * MAX_DISTANCE) % MAX_DISTANCE;
Here distance is a measure of pixels, and direction is a measure of the angle of travel in degrees.
since we are working in vectors, we can calculate the new pixel location using the angle and distance, and some very basic trig.
I would suggest putting this into a function to calculate the new target location, this way, the function can calculate the new location, and if that location is too close to the dog, you can decide on a new path.
If I can make a suggestion, I suggest putting the sheep class into a Timer class that recalculates the sheep's next movement. In this way, the sheep can wander around. While wandering, the sheep might have a much smaller MAX_DISTANCE for each movement. Thereby simulating a level of threat.
If you really want to get into the simulation, you might measure the distance of the dog to the sheep, and based on the distance, scale the speed of the sheeps movements, so that the closer the dog gets, the fast the sheep moves (not to exceed a maximum distance). If you do this, I would recommend you consider using Math.log() to scale the movements so that a 10 pixel reduction in distance from dog to sheep has a smaller effect on the sheep's movement when the dog is 200 pixels away than when the dog is 15 pixels away.
hope this helps.
Upvotes: 1
Reputation: 389
I would divide the directions you want to move the sheep into a number. If the sheep can move up, down, left, and right, and you subtract a direction that the wolf comes from, say left you are left with three. Up down and right.
Use Math random function to pick a random one of these, you can look in the API but basically multiply by the max number you want random and cast as an Int to get rid of the decimals.
int intDirection = (int)(Math.random()*3);
this will give should give you either 0, 1 and 2. Then make a case statement to move the direction based off the number. Maybe for good practice look into enumerators for the three directions .
If you wanted to have more directions you could look into vectors, this is how you do random however.
Upvotes: 0
Reputation: 514
Have a look at the Random class in the Java standard library as it may be of some help. You could easily generate pseudo-random ints and adjust the (x,y) of your characters. Add some error-checking to make sure they move away from each other and you should be good to go!
Upvotes: 0