Hello,
I’m trying to write simple Top-down car simulator. Ofc i browse the forum and the internet before and there is massive amount of answers but… nothing works for me that why i created new topic.
Best related-topics:
My car is made up with 4 images(wheels) + 1 image(body). X,Y of images are in the relation to an imaginary center of Car (200,200)
Car class constructor:
public Car()
{
MiddlePoint.x = 200;
MiddlePoint.y = 200;
LwheelU = new Wheels(MiddlePoint.x - 60.5 ,MiddlePoint.y -85);
LwheelD = new Wheels(MiddlePoint.x - 60.5, MiddlePoint.y + 65);
RwheelU = new Wheels(MiddlePoint.x + 36.5, MiddlePoint.y -85);
RwheelD = new Wheels(MiddlePoint.x + 36.5,MiddlePoint.y + 65);
body = new Body(MiddlePoint.x - 36.5, (MiddlePoint.y - (251/1.9)));
}
Global game parameters: [#EDIT 1.0]
//this variable will decide if the specified car is controlled by a human player or by the computer (in Part II of this tutorial you will learn how to add opponents and this variable will come in handy)
public double acceleration = 0.4;
//the acceleration variable will add to the speed variable on every enterFrame event (in this case 24 times per second); a higher value translates in a faster acceleration of the car
public double speedDecay = 0.96;
//when the car is not accelerated (the UP Key is released), the car will have to slow down smoothly; the speed will multiply with this value (less than 1); the lower this value is, the faster the car will slow down
public double rotationStep = 1;
//this is the number of degrees that will increase or decrease the car's rotation (angle) when the left or right keys are pressed
public double maxSpeed = 3;
//this is the speed limit on our track; increase it if you want the car to go faster
public double backSpeed = 1;
//this is the speed limit when going backwards
public double speedx = 0, speedy = 0;
public double rotation = 0;
//**********************
Move method [#Edit 1.0]
if(speed < 0.3)
speed *= 0.70; //Friction
else
speed = 0;
if (up && speed < maxSpeed)
{
speed -=acceleration;
}
if (down)
{
speed += backSpeed;
}
if (left )
{
rotation -= rotationStep * (speed/maxSpeed);
}
if (right )
{
rotation += rotationStep * (speed/maxSpeed);
}
speedx = Math.sin(Math.toRadians(rotation)) * speed;
speedy = Math.cos(Math.toRadians(rotation)) * speed;
MiddlePoint.x += speedx;
MiddlePoint.y += speedy;
carAngle = Math.toRadians(rotation);
I’m drawing all elements like that:
AffineTransform at;
at = new AffineTransform();
at.translate(MiddlePoint.x - 60.5 ,MiddlePoint.y -85);
at.rotate(Math.toRadians(carAngle), MiddlePoint.x, Sr_PKT.y);
g2d.drawImage(LwheelU.getImage(),at,null);
I’m using timer to refresh actions on board
public void paint(Graphics g)
{
super.paint(g);
Car.paint(g);
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void actionPerformed(ActionEvent e)
{
Car.move();
repaint();
}
At the moment car is moving UP/DOWN but when it comes to UP/DOWN + Right/left it is starting to act very weird. The huge problem is also that elements like wheels/body are ,not connecteted"
look:
Actually i just want the car to move correctly - i dont need any kind of colission , friction.
Thanks in advance for any kind of help
Kamil,
##################################
EDIT 1.0
- Changed keyboard handling to set Flags TRUE (PRESSED) and FALSE (RELEASED)
- update move function:
[list]
[li]Whole x,y changing is now in move function - Go UP/DOWN works fine (Frictions stops the car)
- LEFT/RIGHT doesn’t work correct (or it does but my drawing function is bugged)
[/li]
[/list]