nice routes in simple game?
Hi everyone!
I need help desperately!! I'm programming a simple game with a user that's shooting cars moving around. The problem I have is the routes for the cars. I need an algorithm that makes the cars move in random (interpolated) nice routes (not back and forth or routes with "cuts" or "edges"). And I don't want them to follow premade positions that are read from files, since I want the routes to be new and random every time and for all cars.
Is there anyone who might help me with this!?! Where might I find the algorithm or help? The routes are supposed to look natural, in other words the cars are to move with constant velocity and along interpolated nice curves.
Help me please!!!
/ jezna
[735 byte] By [
jezna] at [2007-9-27 20:23:09]

could you catagorize the legal moves of the cars for meie. do they follow a trach, are they in an open arena, are there obstcals to avoid, should they ram the opponent, which moves are illegal etc.
And what kind of viewpoint are you taking- is it top-down, 3D, horizontal scrolling?
The legal moves are almost any and they're moving in a very simple arena to begin with. The view is from above, ie you only see an icon moving over a 2D map. Terrain is unimportant for the moment.
The problem is, I want the paths to be random but look realistic, ie no cutting edges or sudden turn-arounds (smooth curves). So I guess I have to use a direction for the movements. Til now I've used prewritten tracks read from a file, but this isn't neat or exciting to deal with.
I also want a speed for the vehicles (or timestepped moving), but this could perhaps be managed with threads and timesteps.
Do you have any idea how this could be done? (very grateful for help...)
/ jezna
jezna at 2007-7-7 0:52:36 >

Considering a car as a single object:
Memorize the car direction(as a an angle) in a private variable;
Each X millsec (I think 100 can be good, but you've to try) create a small random angle (from -10 to 10 degree can be good, but you have to try), sum the angle to the actual direction so you have the new direction, then turn the car to fit the new angle and then move the car a bit in that direction.
You can add a contrlo that if you are near a border of the screen you must to force the angle in a particular direction instead of calculate the random angle.
The less the timer/angle the more slow the application and the more smooth the curve... you have to find the right balance
Riccardo from Italy
That algorithm is OK, but likely to make the cars shudder. Maybe you could have an angle momentum, which is added to their current angle exery x millis, and the angle momentum is randomly changed. Make sure it doesn't go over a min and max value(-10 to 10 perhaps) which will indacate a max turning circle. This method will produce gentle arcs and a meadering behavour
Here is an implementation of cars which move randomly. Try it live on: http://jerome.abela.free.fr/cars.html
public class Cars extends Applet implements Runnable {
private ArrayList carList = new ArrayList();
public Cars() {
for(int i=0 ; i<5 ; i++) carList.add(new Car(new Color(
Car.rand.nextInt(256),
Car.rand.nextInt(256),
Car.rand.nextInt(256))));
}
public void paint(Graphics g) {
Iterator it = carList.iterator();
while(it.hasNext()) ((Car)it.next()).paint(g);
}
public void start() {
new Thread(this).start();
}
public void run() {
while(true) {
try { Thread.sleep(30); } catch(InterruptedException e) {}
Iterator it = carList.iterator();
while(it.hasNext()) ((Car)it.next()).tick();
repaint();
}
}
}
public class Car {
public static int RADIUS = 50;
public static double SPEED = 5.0;
public static int SIZE = 10;
public static int RANDOMNESS = 5;
public static Random rand = new Random();
double x, y, dir;
int direction; // -1, 0, 1
Color color;
public Car(Color c) {
x = RADIUS+rand.nextInt(600-2*RADIUS);
y = RADIUS+rand.nextInt(400-2*RADIUS);
dir = rand.nextInt(360)*2*Math.PI/360;
color = c;
}
public void tick() {
double cx, cy;
if(direction == 0) {
x = x + SPEED * Math.cos(dir);
y = y + SPEED * Math.sin(dir);
} else {
cx = x - direction * RADIUS * Math.sin(dir);
cy = y + direction * RADIUS * Math.cos(dir);
dir += direction * SPEED/RADIUS;
x = cx + direction * RADIUS * Math.sin(dir);
y = cy - direction * RADIUS * Math.cos(dir);
}
if(rand.nextInt(RANDOMNESS) == 0) direction = rand.nextInt(3) - 1;
// Avoid walls
cx = x - RADIUS * Math.sin(dir);
cy = y + RADIUS * Math.cos(dir);
if(cx<RADIUS || cx>600-RADIUS) direction = -1;
if(cy<RADIUS || cy>400-RADIUS) direction = -1;
cx = x + RADIUS * Math.sin(dir);
cy = y - RADIUS * Math.cos(dir);
if(cx<RADIUS || cx>600-RADIUS) direction = 1;
if(cy<RADIUS || cy>400-RADIUS) direction = 1;
}
public void paint(Graphics g) {
g.setColor(color);
Polygon p = new Polygon();
p.addPoint((int)(x+Math.cos(dir+3*Math.PI/4)*SIZE), (int)(y+Math.sin(dir+3*Math.PI/4)*SIZE));
p.addPoint((int)(x+Math.cos(dir-3*Math.PI/4)*SIZE), (int)(y+Math.sin(dir-3*Math.PI/4)*SIZE));
p.addPoint((int)(x+Math.cos(dir)*SIZE), (int)(y+Math.sin(dir)*SIZE));
g.fillPolygon(p);
}
}
> Here is an implementation of cars which move randomly.
> Try it live on: http://jerome.abela.free.fr/cars.html
>
> > public class Cars extends Applet implements Runnable
> {
>private ArrayList carList = new ArrayList();
>
>public Cars() {
> for(int i=0 ; i<5 ; i++) carList.add(new
> add(new Car(new Color(
>
>
>
>
>
>
>Car.rand.nextInt(256),
>
>
>
>
>
>
>Car.rand.nextInt(256),
>
>
>
>
>
>
>
>
>
>Car.rand.nextInt(256))));
>}
>
>public void paint(Graphics g) {
> Iterator it = carList.iterator();
> while(it.hasNext())
> Next()) ((Car)it.next()).paint(g);
>}
>
>public void start() {
> new Thread(this).start();
>}
>
>public void run() {
> while(true) {
> try { Thread.sleep(30); }
> leep(30); } catch(InterruptedException e) {}
> Iterator it = carList.iterator();
> while(it.hasNext())
> .hasNext()) ((Car)it.next()).tick();
> repaint();
> }
>}
> }
>
> public class Car {
>public static int RADIUS = 50;
>public static double SPEED = 5.0;
>public static int SIZE = 10;
>public static int RANDOMNESS = 5;
>public static Random rand = new Random();
>
>double x, y, dir;
>int direction; // -1, 0, 1
>Color color;
>
>public Car(Color c) {
> x = RADIUS+rand.nextInt(600-2*RADIUS);
> y = RADIUS+rand.nextInt(400-2*RADIUS);
> dir = rand.nextInt(360)*2*Math.PI/360;
> color = c;
>}
>
>public void tick() {
> double cx, cy;
> if(direction == 0) {
> x = x + SPEED * Math.cos(dir);
> y = y + SPEED * Math.sin(dir);
> } else {
> cx = x - direction * RADIUS *
> * RADIUS * Math.sin(dir);
> cy = y + direction * RADIUS *
> * RADIUS * Math.cos(dir);
> dir += direction * SPEED/RADIUS;
> x = cx + direction * RADIUS *
> * RADIUS * Math.sin(dir);
> y = cy - direction * RADIUS *
> * RADIUS * Math.cos(dir);
> }
>
> if(rand.nextInt(RANDOMNESS) == 0) direction =
> ction = rand.nextInt(3) - 1;
>
> // Avoid walls
> cx = x - RADIUS * Math.sin(dir);
> cy = y + RADIUS * Math.cos(dir);
> if(cx<RADIUS || cx>600-RADIUS) direction =
> ction = -1;
> if(cy<RADIUS || cy>400-RADIUS) direction =
> ction = -1;
> cx = x + RADIUS * Math.sin(dir);
> cy = y - RADIUS * Math.cos(dir);
> if(cx<RADIUS || cx>600-RADIUS) direction = 1;
> if(cy<RADIUS || cy>400-RADIUS) direction = 1;
>}
>
>public void paint(Graphics g) {
> g.setColor(color);
> Polygon p = new Polygon();
>
>
>
>
>
> p.addPoint((int)(x+Math.cos(dir+3*Math.PI/4)*SIZE),
> ), (int)(y+Math.sin(dir+3*Math.PI/4)*SIZE));
>
>
>
>
>
> p.addPoint((int)(x+Math.cos(dir-3*Math.PI/4)*SIZE),
> ), (int)(y+Math.sin(dir-3*Math.PI/4)*SIZE));
> p.addPoint((int)(x+Math.cos(dir)*SIZE),
> *SIZE), (int)(y+Math.sin(dir)*SIZE));
> g.fillPolygon(p);
>}
>
> }
>
>
Now that is cool...
Good job!
Virum at 2007-7-7 0:52:36 >

Wow!That's so much exactly what I was looking for!Thank you all very much for your help! You've been really helpful!! I have no words for it.I hope I might be able to do the same to one of you some day.Best regards/ jezna
jezna at 2007-7-7 0:52:36 >
