Hi everyone,
First off let me say that I’ve been browsing this forum as a guest for a while now, and the community really seems to be friendly compared to some places I’ve seen in the past and I hope that I can contribute as much as I’ve seen people do. It really is a pleasure not seeing anyone being flamed because of the level of intellect they may or may not have
Moving on to business then. As much as I’ve scoured the internet in search of information concerning Java game programming, I’ve found that this forum in addition to some other sources are close enough to the best you can be lead to. I’ve got a problem with positioning and rotation in a top down car game I’m building and all the resources I’ve seen so far do not go through as much detail as I would hope concerning certain aspects.
The following code is what I have presently, and it would be of great help if someone could point me in the right direction or tell me what is actually being done wrong.
This is where I render my drawing from the game loop.
public void render(Graphics2D g) {
...
g.rotate(rotation,x,y);
g.drawImage(carImage, x, y, null);
g.setColor(Color.GRAY);
g.drawRect(getBounds().x, getBounds().y, getBounds().width, getBounds().height);
}
This is where i update the drawing for the render process
public void update(int deltaTime) {
if ((Map.getGameArea().contains(getBounds()))) {
if (speed >= 0) {
if (slowdown)
speed -= .1;
move();
}
}
...
This is when keys are pressed
public void keyPressed(KeyEvent evt) {
slowdown = false;
if (evt.getKeyCode() == KeyEvent.VK_UP) {
speed += acceleration;
}
if (evt.getKeyCode() == KeyEvent.VK_DOWN) {
speed -= acceleration;
}
if (evt.getKeyCode() == KeyEvent.VK_RIGHT) {
rotation += rotationStep * (speed/TOPSPEED);
}
if (evt.getKeyCode() == KeyEvent.VK_LEFT) {
rotation -= rotationStep * (speed/TOPSPEED);
}
}
These are variables that are used to determine the angle
private double rotation = 0;
private double rotationStep = 1.1;
And finally the most important? method to actually change the coordinates
private void move() {
double ax = Math.sin(rotation * (Math.PI/180)) * speed;
double ay = Math.cos(rotation * (Math.PI/180)) * speed * -1;
x += ax;
y += ay;
}
The variables for speed and acceleration are initialized with 0 and .3 respectively
The actual problem seems the lie in the move method as the x constantly remains 0, and in all honesty I don’t understand why.
Thanks for your time!