Hi there, I'm trying to code a simple program whereby an object (in this case a boat) can move freely around the screen via the use of the arrow keys. Now the left and right arrows should change the angle the boat faces, while the up arrow makes the boat move in the direction the boat faces, while the back arrow, you've guessed it, makes the boat reverse.

Now so far I have been able to make the boat rotate by 22.5 degrees in both directions using the left and right arrows by using transform.

The applet I have allows me to move forward and backwards too, but not in the way I need it, for example sometimes it changes up to down, or goes in the wrong direction altogether.

I was hoping someone here could please review the code I will add at the bottom, and tell me where I'm going wrong with this. Thank you.

Here is my KeyPressed code:

public void keyPressed(KeyEvent event){

	switch (event.getKeyCode()){
		case KeyEvent.VK_UP:
		//forward movement
      	// work out horizontal and vertical components of the speed
			hspeed = ((double)boatSpeed) * Math.cos(currentAngle);
			vspeed = ((double)boatSpeed) * Math.sin(currentAngle);
		
			// move the actual boats here
			xpos = xpos + (int) hspeed;
			ypos = ypos + (int) vspeed;
		break;

		case KeyEvent.VK_DOWN:
			
			// work out horizontal and vertical components of the speed
			hspeed = ((double)boatSpeed) * Math.cos(currentAngle);
			vspeed = ((double)boatSpeed) * Math.sin(currentAngle);

			// move the actual boats here
			xpos = xpos - (int) hspeed;
			ypos = ypos - (int) vspeed;
		break;

		case KeyEvent.VK_LEFT:
			//rotates image 22.5 degrees
			currentAngle -=22.5;
		break;

		case KeyEvent.VK_RIGHT:
			//rotates image 22.5 degrees
			currentAngle +=22.5;
		break;
	}
	//repaints image after each move
	repaint();
}

Note that Math.sin() and cos() take radians for their parameters. 22.5 is perhaps more radians than you want to add to your angle.

Note that Math.sin() and cos() take radians for their parameters. 22.5 is perhaps more radians than you want to add to your angle.

I also have the code:

newXform.rotate(Math.toRadians(currentAngle), xpos, ypos);

Which as I understand it, tells the program to convert currentAngle into Radians.

I think my issue lies in the formula for hspeed and vspeed, as the direction of movement is affected, for example if the boat is point south east, and up is pressed, the boat will move closer to north west.

Yes, those were the calcs that I was referring to. You didn't post the other code. Your speed calcs are using degrees instead of radians.

Yes, those were the calcs that I was referring to. You didn't post the other code. Your speed calcs are using degrees instead of radians.

But I thought:

Math.cos(currentAngle);

Which is calculating the speed, will take currentAngle which the code I added in my 2nd post, took in as degrees and converted into Radians automatically? Or am I misunderstanding that?

No, check the API: Math.cos(double) The parameter is an angle in radians. You need to convert first or keep currentAngle in radians.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.