# Movment...

This topic is 4798 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have thougt a bit about this...but I don't know how to implement it, if it's possible. I'll use a ball to try explaning what I mean. Let's say this! In 2d a ball can move at 360 different angles. But on the screen it can only move in 8 different angles. ((x+1,y+0)(x+0,y+1)etc.) but then you can kind of represent all 360 angles. Then if you bounce this ball the reflection of the ball is depending on the angle of impact. Ok...this doesn't make any sense I think... But I assume my question is: How do you make a better representation of a bonce then just inverting x or y velocity? If anyone have done this I would be greatful for a formula or someting...but I am in my second year of High School and I have nor read much math and zero physics... And I know this post is kind of woolly and doesn't make so much sense...but please dont flame me about it...just ignore it =) //walle

##### Share on other sites
Hi,

OK - here goes:
Quote:
 In 2d a ball can move at 360 different angles.

Actually a ball could move in an infinite number of angles. There are 360 degrees in a circle, but the ball could bounce by 1/2 a degree, for example. As you get more exposed to maths, you'll find that angles can be expressed in many other ways - for example, on a calculator you'll typically find:

degrees (360 units to a circle)
radians (2 * pi units to a circle)
gradians (400 units to a circle)

So - for example, 180 degrees = pi radians = 200 gradians.

Quote:
 But on the screen it can only move in 8 different angles.((x+1,y+0)(x+0,y+1)etc.) but then you can kind of represent all 360 angles.

Why can't the ball move to (x+2, y+0)? You're probably thinking this doesn't make sense, as you're effectively working on a 2-d grid of pixels and it makes sense that the ball should only move one step at a time - but, quick quiz - is the distance travelled from (x, y) to (x+1, y) the same as (x, y) to (x+1, y+1) (hint : pythagorus' theorem).

This is quite a topic you're starting to break into. What I would suggest is starting to research Vectors (in particular, Vectors in 2-dimensions). I'm not quite sure how old you are (we don't use the term High School in the UK) and I'm guessing you're about 11-12 - in that case it might be worth chatting to your maths or physics teacher about some Vector references. Alternatively, try some of these websites:

MathWorld - everything you could even want to know about maths
GameDevs maths and physics articles - check the vectors and then the matrix sections

Some of this may seem a little daunting at times, but bear in mind you've got years to learn ahead of you - I've worked as a mathematician for many years and can thoroughly recommend it (even worked for a swedish company!)

Hope this helps,
Jim.

##### Share on other sites
Quote:
 Original post by JimPricequick quiz - is the distance travelled from (x, y) to (x+1, y) the same as (x, y) to (x+1, y+1) (hint : pythagorus' theorem).

Yes. It has only travelled one pixel. But in the second example it have moved one pixel right and one down. But it's still just one pixel. Right??

And I'm 17. I used american school levels so as many as possible would understand. Or so I thougt...dont you go to high school when you are 16-19? What do you go to in Canada/UK?

//walle

##### Share on other sites
Quote:
 But it's still just one pixel. Right??

The pythagorian theorem is especially useful for calculating distances in a cartesian coordinate system it states that for a right triangle, the length of the square of the hypotenuse is equal to the sum of the squares of the other two sides. Any diagonal line in cartesian coordinate space implies a right triangle because you can create one by adding two segments, aligned to the x and y axis respectively. in this case if you move the ball +1 in the x direction and +1 in the y direction, you imply a right triangle were the two shortest sides are of length 1 and 1. The hypotenuse of this triangle is the distance that the ball moves.

   ball's eventual position->    X                                /|                               / |                              /  | up one                             /   |                            /    | ball's original position-> X-----+                              over one

There for if the ball moves up one pixel and over one pixel, the actual distance traveled is the SQRT of 2 = 1.41 (approximately)
So the ball moves about 1.41 pixels in that case.

[Edited by - Kelly G on November 30, 2004 6:02:36 AM]

##### Share on other sites
Ok. This is getting a bit over my head =( have to read the tutorials.

I don't understand this:

Quote:
 ball's eventual position-> X /| / | / | up one / | / | ball's original position-> X-----+

The math I have lerned is: one (kateter) one of the sides of the triangle (not the hypotenuse) ^2 + the other side(not the hypotenuse) ^2 = hypotenuse

In this case shouldn't the hypotenuse be 2?

1^2+1^2=2

//walle

##### Share on other sites
It's actually:

"one of the sides of the triangle (not the hypotenuse) ^2 + the other side(not the hypotenuse) ^2 = hypotenuse ^2 "

so its SQRT (1^2+1^2);

##### Share on other sites
Morning,

Aargh - just posted a long reply and then managed to delete it - oh well, here goes again.

OK - so KellyG has explained why a move from (x, y) to (x+1, y+1) is a physically longer step than the move from (x, y) to (x+1, y). If you can accept this, then the next thing to accept is that we could step from any pixel to any other, so long as we understand what the step length is. If you think about it in relation to speed this makes sense also - for example, how would you model a ball travelling twice as fast? You'd have it go twice the distance (so from (x, y) to (x+2, y), for example). If you can do this, why can't you take any arbitrary step of length 2?

OK - time for some ASCII art. Here is the first example:
o : empty pixel
b : ball
w : wall

Frame 1:      Frame 2:      Frame 3:b o o         o o o         o o bo o o         o b o         o o ow w w         w w w         w w w

This is the example you originally gave. The ball is approaching the wall at an angle of 45 degrees, the bounce angle is correct, and the ball is travelling at 1.4 units / frame.

Now let's double the ball speed:

Frame 1:     Frame 2:     Frame 3:b o o o o    o o o o o    o o o o bo o o o o    o o o o o    o o o o o o o o o o    o o b o o    o o o o ow w w w w    w w w w w    w w w w w

Exactly the same example, only now the ball is traveling twice as fast. So now we can accept that the ball could travel at any speed we wish - and can therefore step to any pixel if it's next step that is consistent with the angle it's travelling at, so long as that step has distance equal to it's speed.

So let's now assume that the ball is travelling like a Knight in chess - so from (x, y) to (x+2, y+1), or some rotation thereof. We now have:

Frame 1:      Frame 2:      Frame 3:b o o o o     o o o o o     o o o o bo o o o o     o o b o o     o o o o ow w w w w     w w w w w     w w w w w

Can you see how this travels in a shallower angle, but still accurately represents bouncing?

In practice, we tend to record co-ordinates as real numbers, because in order to calculate angles we have to use trigonometric functions like sin and cos. But, when the image is rasterised to the screen, those co-ordinates are turned into integers so they can be physically represented - but that's a whole 'nother topic.

This is the basis of vectors that I spoke about in my first reply. A vector (as used here) is just a representation of direction of travel. For example, if we know that in one step the ball travels from (x, y) to (x+2, y+1), then the direction of travel is the vector (2, 1). Once you understand vector nomenclature and then start to learn more about them, you'll find they're immensely powerful - and vectors and their bigger brothers, matrices, underpin loads of things that go on in computer programming.

Quote:
 And I'm 17. I used american school levels so as many as possible would understand. Or so I thougt...dont you go to high school when you are 16-19? What do you go to in Canada/UK?

No offence meant, I was just trying to get a bead on what your background was likely to be and what environment you were in - I emmigrated to Canada from the UK recently, so don't really have any knowledge of the North American school system, and we have different names for schools in the UK. Same caveat applies - talk to your maths and /or physics teachers about vectors, see what they come through with.

Any more questions, let us know.
Jim.

Edited to add source tags to preserve spacing in diagrams!

[Edited by - JimPrice on November 30, 2004 11:55:25 AM]

##### Share on other sites
JimPrice: Thanks for the long reply!

Kelly G: Thanks for pointing that out...forgot about the last ^2...

Ok. This is really educational, I've seen all the exampels by testing. But it's nice to see the math behind.

I have used two variables to change the balls position: xvel and yvel.
And then changed the speed by increasing them...I'm starting to feel that this is a very easy way to do this. And collisiondetection doesn't work well with higher speeds.

Quote:
 No offence meant, I was just trying to get a bead on what your background was likely to be and what environment you were in

Non taken. =)

//walle

##### Share on other sites
Ah, collision detection is a whole new topic!

The problem I expect you're hitting is this - using the same bouncing ball example, let's say the ball is travelling at a 45 degree angle to the wall, but is travelling very quickly - so that now, in one frame, it's travelling from (x, y) to (x+4, y+4). Time for some ascii art again:

Frame 1:        Frame 2:b o o o o       o o o o oo o o o o       o o o o ow w w w w       w w w w wo o o o o       o o o o oo o o o o       o o o o b

The problem is the ball is never immediately next to the wall, but is moving so quickly it seems like it passes through the wall.

This is again where vectors come in useful. Both the wall and the ball's direction of travel during the last frame can be represented by 2 different lines (the wall by the 'w's in the picture above, and the ball by a line travelling from top-left to bottom-right corners). Each of these lines can be represented by a vector. We can immediately see that the 2 lines cross, and therefore that there must have been a collision during the balls travel. Using a neat bit of maths, you can also compare the 2 vectors and find out not only if the ball collided with the wall, but also at exactly what co-ordinates.

So - the morale of the story is still the same. Time to learn about Vectors!

All the best,
Jim.

##### Share on other sites
I spent a year learning something this simple without guidance :(
it turns out that math actually is useful :P
First I made a pong game, which was pretty simple to do with a ball that only bounces at 45 degree angles. Next I moved to a game where you have a space ship on the center of the screen which can move at any speed in any direction (0-360 degrees).
Initially my spaceship moved in the following 8 directions (using 12 oclock as 0 degrees):

0 degrees (y-1, x+0)
45 degrees (y-1,x+1)
90 degrees (y+0,x+1)
135 degrees (y+1,x+1)
180 degrees (y+1,x+0)
225 degrees (y+1,x-1)
270 degrees (y+0,x-1)
315 degrees (y-1,x-1)

essencially in 8 directions. Animation wasn't very smooth, and its hard to get to a point if its between say, halfway between 0 & 45 degrees. We start to get into dealing with circles if you look at this closely with this ascii art (magnified by 2x):

X0X0X
00000
X0*0X
00000
X0X0X

but if I wanted to go at say, 22.5 degrees, I could roughly do it with (y-1,x+2). After playing for a while, I noticed that when my spaceship was going at the half angles, it was traveling faster then if it was traveling at the original 8 angles. A player who wanted to travel faster could tap the keys just a bit to find the sweet spots in the angles to move faster, or the enemy suicide mines would occasionally track on the sweet spot angles and suddenly home in on the players slightly faster.
How could I find a solution to move the same speed in any direction? Heres where it becomes important to pay attention in math class, or you'll spend a year trying to figure it out like I did. :P
a perfect circle can be modeled with X&Y coordinates using Sin() and Cos(), which convert an angle into an exact X&Y value between 0-1. (multiply the result to increase speed).
So:
Sin(90) degrees = 1, and Cos(90) degrees = 0
Sin(22.5) degrees = .382, and Cos(22.5) degrees = .923
Sin(45) degrees = .707, and Cos(45) degrees = .707
Sin(0) degrees = 0, and Cos(0) degrees = 1
I'm going to venture a guess that .707 is the square root of 2, with the pythagorean theorem in mind (1^2 + 1^2 = sqrt(2)=.707?)

Thus, you can use an infinite number of angles using Sin and Cos. Sin = X value, Cos = Y value for your vectors.
To move at a 22.5 degree angle with speed of 1px per frame we'd use:
Ball.X += Sin(22.5); //.382 pixels in X direction
Ball.Y += Cos(22.5); //.923 pixels in Y direction
Which is far more accurate then the (x+1, y+2) I was using earlier.

##### Share on other sites
the only issue with using sine and cosineto manage the pixel movement, would be how do you draw your object 0.707 pixels in either direction? would you round it up to 1 pixel, if that's the case then you're increasing speed, if you round down you're decreasing speed, unless of course you decide to just keep the position internally, and just draw it off by a pixel, but with the next calculation use the internal value and not the actual drawn value... of course i don't know if anything i said makes sence, but it is quite the problem if you lose your real location and start rounding up or down as your speed will increase or decrease accordingly...

##### Share on other sites
btw, SQRT(2) actually == 1.414, however 1/SQRT(2) == 0.707

##### Share on other sites
Quote:
 the only issue with using sine and cosineto manage the pixel movement, would be how do you draw your object 0.707 pixels in either direction?

I think you'd go with a float for keeping track of the X & Y values, and then let the computer figure out the best place to plot it.

Quote:
 btw, SQRT(2) actually == 1.414, however 1/SQRT(2) == 0.707

ah, something like that. ;P
1/SQRT(2) = sin(45) & cos(45). interesting :D
I forget the significance, though it has something to do with triangles...1-1-srqt(2) right triangle comes to mind

##### Share on other sites
Quote:
 the only issue with using sine and cosineto manage the pixel movement, would be how do you draw your object 0.707 pixels in either direction? would you round it up to 1 pixel, if that's the case then you're increasing speed, if you round down you're decreasing speed, unless of course you decide to just keep the position internally, and just draw it off by a pixel, but with the next calculation use the internal value and not the actual drawn value... of course i don't know if anything i said makes sence, but it is quite the problem if you lose your real location and start rounding up or down as your speed will increase or decrease accordingly...

Yup, you got it. Store location as floats (or whatever), but plot as integers. When you do 3D work, your floats will be transformed through a number of matrices first, but still end up getting turned into integers (as pixels).

This becomes more interesting when you try doing some other things. For example, if you want to plot a wall in 2D, then if it runs from (x, y) to (x, y+5), it's pretty easy. But what if you want the wall to run from (x, y) to (x+2, y+5). What pixels does it go through?

Here b is the beginning of the wall, and e is the end.

b o o o o oo o oo o oo o o o o e

Jim.

##### Share on other sites
Quote:
 This is again where vectors come in useful. Both the wall and the ball's direction of travel during the last frame can be represented by 2 different lines (the wall by the 'w's in the picture above, and the ball by a line travelling from top-left to bottom-right corners). Each of these lines can be represented by a vector. We can immediately see that the 2 lines cross, and therefore that there must have been a collision during the balls travel. Using a neat bit of maths, you can also compare the 2 vectors and find out not only if the ball collided with the wall, but also at exactly what co-ordinates.

Yes. I found a tutorial on using lines in collision detection here on gamedev. But it was a little short in the explanations...I'll keep looking...

Quote:
 How could I find a solution to move the same speed in any direction? Heres where it becomes important to pay attention in math class, or you'll spend a year trying to figure it out like I did. :P

Only problem...I don't get to read any more math after this course...and trigonomi is in the nex or so course...have to lern this on my own.

//walle