Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

Bouncing...

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...

45°:

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

90°:

x = x + 1, y = y + 0

135°:

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

180°:

x = x + 0, y = y + 1

225°:

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

270°:

x = x - 1, y = y + 0

315°:

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

360°:

x = x + 0, y = y - 1

246°:

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!??

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
1  
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. –  Jonathan Hobbs Apr 30 '11 at 18:09
    
How do I add something to that tag? A great tutorial about vectors that I refer to often is blog.wolfire.com/2009/07/… –  jhocking Apr 30 '11 at 18:14
    
@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. –  Jonathan Hobbs Apr 30 '11 at 18:50
    
@Jonathan Hobbs Huge, huge thanks for pointing me to Vectors tag! You made my day! –  daGrevis Apr 30 '11 at 19:34
    
@daGrevis You're welcome! –  Jonathan Hobbs May 1 '11 at 2:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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