Hey, I'm creating my second game ever, but this time - using Java. Before I used Pascal and CRT module! =D It's 2D game where you need to drag the ball with mouse from location A to location B. Right now, I have ball on location A and I want to make it "alive". With that I mean that it will move from location A in 135° direction until it reaches wall, then bounce the wall and move in 45° direction, then again wall - in 315° and so on.


Let's imagine that all board is 460x640 units big. So, as you can see in image above, ball's start location is 0. When the ball for the first time bounce the wall, location may be like x = 400, y = 480. In this example I have four possible directions like North, East, South and West, but in real game, when player drags the ball with his mouse, options may be more, right? I think so. So I think that I need to use degrees for it. I made list as example of what do I mean...


x = x + .5, y = y - .5


x = x + 1, y = y + 0


x = x + .5, y = y + .5


x = x + 0, y = y + 1


x = x - .5, y = y + .5


x = x - 1, y = y + 0


x = x - .5, y = y - .5


x = x + 0, y = y - 1


x = ?, y = ?

The main problem is that I don't know how to covert degrees to coordinates and vice versa. For example I need to know x and y for 246°. Why? By knowing x and y I can move ball, because all my application basically is infinite loop that takes pause, then calculate x and y from degrees and then move ball. Please keep in mind that I'm very newbie to games development, I'm not good at math (unfortunately) and English isn't my native language... as you can see.

P.S. Maybe all idea about how-to is wrong!??


This is pretty basic vector math stuff.

Basically, the answer you're looking for is that if you want to be 1 unit away for a given angle of x, your coordinate is going to be ( cos( x ), sin( x ) ). You'll probably have to work in radians instead of degrees, though.

You can then take that unit vector and multiply it by whatever your per-frame velocity is to determine where you're going to be next frame.

  • 1
    \$\begingroup\$ The Vector tag links to a good tutorial on vectors. If you're working with positions, directions or velocity, vectors are going to come in handy. \$\endgroup\$ – doppelgreener Apr 30 '11 at 18:09
  • \$\begingroup\$ How do I add something to that tag? A great tutorial about vectors that I refer to often is blog.wolfire.com/2009/07/… \$\endgroup\$ – jhocking Apr 30 '11 at 18:14
  • \$\begingroup\$ @jhocking That's precisely the one it links to already! ;) But there is an Edit Tag Wiki button on the tag info page below the entry. \$\endgroup\$ – doppelgreener Apr 30 '11 at 18:50
  • \$\begingroup\$ @Jonathan Hobbs Huge, huge thanks for pointing me to Vectors tag! You made my day! \$\endgroup\$ – daGrevis Apr 30 '11 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.