
-----------------------------------
WayyTooBrown
Thu Dec 16, 2010 6:41 pm

Astro Game Help.
-----------------------------------
Alright, so im tryna make an astroid game for school 
all i need help with is how to make astroids come towards the middle of the screen.
 
Help please?

-----------------------------------
TokenHerbz
Thu Dec 16, 2010 9:37 pm

RE:Astro Game Help.
-----------------------------------
your screenXsize and screenYsize div 2 will give you the center of your program screen.

-----------------------------------
WayyTooBrown
Fri Dec 17, 2010 5:59 pm

Re: RE:Astro Game Help.
-----------------------------------
your screenXsize and screenYsize div 2 will give you the center of your program screen.

Uhm, i know that 
But how do i make it so that astroids come TOWARDS the centre
i dont understand how to write that.

-----------------------------------
DemonWasp
Fri Dec 17, 2010 7:02 pm

RE:Astro Game Help.
-----------------------------------
Do you know basic trigonometry? Slope = Rise / Run, etc?

Let's say that your asteroids have a speed (variable named speed) and an (x,y) location. You know the location of the centre (cx,cy) == (maxx div 2, maxy div 2). You can now calculate the difference in x- and y-coordinates to the centre, which we'll call:

(run, rise) = (x-cx, y-cy)

Given that, you should be able to find length of the hypotenuse of the triangle (which is the line from your asteroid to the centre). Call that H.

Now, using similar triangles, you want to scale the large triangle to a smaller triangle, so that you travel (speed) units along the hypotenuse, H. The scale that gets you from the length of H to (speed) will also get you from (run, rise) to (speedX, speedY).

You can now use (speedX, speedY) to modify your (x,y) coordinates for each asteroid so that they move towards the centre. If you're not familiar with the basic game loop, look it up in the Turing help.


If you're clever, you can use this so that they move towards any given points that you label (cx, cy), in case you want to have a nonstandard "centre".

-----------------------------------
TerranceN
Fri Dec 17, 2010 7:30 pm

RE:Astro Game Help.
-----------------------------------
You can also look at this as normalizing the vector from (x, y) to (cx, cy). Then moving in each coordinate of that unit vector will yield a total distance traveled of 1 in the direction from (x, y) to (cx, cy). You can then just multiply each component by speed and add it to the corresponding position component.

-----------------------------------
WayyTooBrown
Sat Dec 18, 2010 11:35 am

Re: Astro Game Help.
-----------------------------------
Uhm, i still dont get how to make the astroid travel the hypotenuse (H)
can you show me a code similar to that?

-----------------------------------
TokenHerbz
Sat Dec 18, 2010 3:31 pm

RE:Astro Game Help.
-----------------------------------
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

Safe to say, that 3,3 is the middle of your map.

1x (left side) 5x (right side)
1y (bottom) 5y (top)

if you want your object to travel "TOWARDS" middle.

1) find location of the object
2) find the direction towards middle
3) Apply that direction to object

So lets say, your spawning your astroids outside of the map, and you want them to come INTO the game, towards the middle, (for some reason) lol so...

1) where is this astroid spawned? top/bottom/left/right?
ex: if astroid X < 0 then 
     its on the left side, needs to move RIGHT.
    astroid X += vx (velocity X you want)
to give aim (so its not just going right)
   if astroid y < maxy div 2 then
      astroids under 1/2 the map, lets give it an upwards boost a bit
       atroid Y += vy 

Keep inmind, velocitys can be different speeds and directions and id recommend adding a minor random factor into it, to make the game more realistic since not all astroids travel the same speed, etc.

Above is not actual CODE to make your program work, its theory to assist you in creating your own, working game.

If you need more help in explaining, i'll be glad to write a thourough explanation followed with examples.

-----------------------------------
TerranceN
Sat Dec 18, 2010 5:04 pm

Re: Astro Game Help.
-----------------------------------
The problem with TokenHerbz's solution is that you will not go directly towards the middle, and you cannot go towards the middle at an exact speed. Consider this picture:
http://img88.imageshack.us/img88/7022/normalizingvectors.png
Note that dx**2 is the same as dx^2 is the same as dx to the power of 2.
d is the vector from (x, y) to (cx, cy), and its components are dx and dy along the x and y axis respectively.
The triangle in the top right represents the unit vector of d, and is a similar triangle to the one formed by d and its components. Most importantly is has a length of 1 (hence the name unit vector) Unce we find those components we know how far one has to travel to move 1 unit in the direction from (x, y) to (cx, cy).

All this vector stuff isn't really important to understand, as we can derive it ourself. Since these triangles are similar, the ratios of corresponding sides are equal. This means d/1 = dx / ux = dy / uy. So lets just take d / 1 = dx / ux. When rearranged to solve for ux, you get ux = dx / d, and similarly for uy, you get uy = dy / d. So now we can solve for ux and uy, which when put into the Pythagorean theorem (the length formula) will always result in a length of 1.

Now for speed. If sqrt(ux^2 + uy^2) = 1, what does it equal if we multiply both ux and uy by 2?
sqrt((2*ux)^2 + (2*uy)^2)
sqrt(4*ux^2 + 4*uy^2)
sqrt(4*(ux^2 + uy^2))
2*sqrt(ux^2 + uy^2)
since sqrt(ux^2 + uy^2) = 1
2*1
2
therefore when we multiply the components by a scalar multiple, the length is multiplied by a scalar multiple as well. So the final values you add to the position (lets call it (px, py)) of the asteroid is
px = px + ux * speed
py = py + uy * speed

Hope that cleared up what I was talking about with vectors.

-----------------------------------
WayyTooBrown
Mon Dec 20, 2010 10:33 pm

RE:Astro Game Help.
-----------------------------------
i still dont understand how to do the speed and how to make it move in a certain direction :|
can you please explain a different way?

-----------------------------------
TokenHerbz
Mon Dec 20, 2010 11:22 pm

RE:Astro Game Help.
-----------------------------------
uhh, explaining vectors while boozed up... blahh.. tomorrow ill leave you a nice simple answer with everything you need to know... yes yes... wait.. for me... :) 