This site requires JavaScript, please enable it in your browser!
Greenfoot back
DMKxD
DMKxD wrote ...

2012/3/18

Car Racing - need help with acceleration system

DMKxD DMKxD

2012/3/18

#
Hey Community! I'm working on a 2D Car Racing game atm. and i was wondering if anyone of you know how to make a good acceleration system for cars. I tried it a few times, but all what i was trying failed. For example:
    private void Tasten2()
    {   if(Greenfoot.isKeyDown("w")) {
            move(geschwindigkeit);
            vorwärts = System.currentTimeMillis();
        if (vorwärts != 0 && vorwärts <= System.currentTimeMillis() + 1000000000 && geschwindigkeit == 1)  
        {  
            geschwindigkeit = geschwindigkeit + 2;
        }
        if (vorwärts != 0 && vorwärts <= System.currentTimeMillis() + 2000000000 && geschwindigkeit == 3)  
        {  
            geschwindigkeit = geschwindigkeit + 2;
        }
When you release "up" the car have to "roll out", so it don't have to stop immediately (don't know how to do it). The second thing i want to make is, when you drive in a curve you have a small drifting. Thanks for your help!
plcs plcs

2012/3/19

#
Hi DMKxD I'd avoid messing around with time and simply use an Int counter, that goes up when the "w" key is pressed, and down when it isn't.
private int count;

public void act() {
  // psuedo code
  if "w" is down
    accelerate()
  else
    decelerate()
}

public int getCount() {
  return count;
}

public void accelerate() {
  count++;
}

public void decelerate() {
  if (count < 1) {
    count = 0;
  } else {
    count--;
  }
}
Then, I'd set ranges for speeds, so if count is between 1 and 10 it moves a certain speed (setLocation), and if it is 10 - 15 slightly further, and so on, with perhaps a > 50 or something to illustrate max speed.
Monkey3030 Monkey3030

2012/9/27

#
i was just messing around with some code and came up with this, hope its what you were looking for. you can change x to whatever you want to name the integer, have fun!
public int x = 0;

 public void accelerate()
    {
        if(Greenfoot.isKeyDown("up"))
            x++;
        else
            x--;
        move(x);
        if (x>10) x=10;
        if(x<1)x=1;
    }
nooby123 nooby123

2012/9/27

#
Here, this is a code that I have used for a very long time!
double acceleration = 0.5;
double speed = 0;

public void accelerate() {
    if(Greenfoot.isKeyDown("up")) speed += acceleration;
    else speed -= acceleration;

    move((int)speed);
    if(speed > 10) speed = 10;
    else if(speed <=0) speed = 0;
}

You can change the acceleration whenever you want. Increase it to make the car go faster!
nccb nccb

2012/9/28

#
I've previously written about a system for implementing driving -- this is the last post, there's also some previous posts (linked to before that); http://sinepost.wordpress.com/2012/04/17/driving-like-a-bus/
You need to login to post a reply.