Author Topic: Math help for game  (Read 2674 times)

0 Members and 1 Guest are viewing this topic.

Offline bilyp

  • LV2 Member (Next: 40)
  • **
  • Posts: 21
  • Rating: +9/-1
  • Axing the z80
    • View Profile
Math help for game
« on: November 13, 2011, 03:25:09 pm »
I am working on a game where a ball falls from the top of the screen, and it bounces on lines, which all are rotated at the same time around the center of the screen.

I want two know how you would check if point x,y is between two other points (if it hit the line). If it is, then I also want to know the angle of the line (to see how to bounce off of it).

Also, I would like help to find out how to rotate all the points (of the lines) around the center.
I have looked on google, but I was not able to find anything, so I was hoping that omnimaga could help!  :w00t:
« Last Edit: November 13, 2011, 03:43:45 pm by bilyp »

Offline Nick

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1166
  • Rating: +161/-3
  • You just got omnom'd
    • View Profile
    • Nick Steen
Re: Math help for game
« Reply #1 on: November 13, 2011, 04:43:00 pm »
you're making a kind of vortex (ipod) game? for the line, i should use the formula for a chord of a cirlce


to know the angle of the ball with the line, find the line with the ball and the center of the circle in it (as a kind of function in ax+b=0 ) and look for the intersections of the line and the function of the ball with a= (y2-y1)/(x2-x1) with y2=ycenter y1=yball, x2=xcenter, x1=xball and b=y2-ax2

when you get the function, you can find the angle by the geometry:
angle=Arctan(a/b) arctan=atan=Bgtan=tan^-1



« Last Edit: November 13, 2011, 04:43:23 pm by Nick »

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Math help for game
« Reply #2 on: November 13, 2011, 05:05:49 pm »
the equation for the line is Y=(B1-B2)/(A1-A2)*(X-A1)+B1 (this is point-slope form) so to find if a certain point (X,Y) is on that line, plug in X and see if you get Y.

to find angle C, find angle D and add 90°. angle D is calculated most easily by doing Tan-1((Y-B1)/(X-A1))
« Last Edit: November 13, 2011, 05:06:23 pm by nemo »


Offline bilyp

  • LV2 Member (Next: 40)
  • **
  • Posts: 21
  • Rating: +9/-1
  • Axing the z80
    • View Profile
Re: Math help for game
« Reply #3 on: November 13, 2011, 05:09:04 pm »
Whoa! that is a lot of information right there. It is kind of like vortex (I never even knew about it before today), but the ball is not coming from the center.

I guess i did not describe the game as much as i should've.

The ball falls from the cieling, due to gravity. the goal is to get the ball to the floor, without hitting the walls. There will be lines in the way of the ball, which the ball bounces off of. The only thing you can control is the rotation of the lines, as shown in the pictures below.

The original level:

Now, after you press the -> arrow on the keyboard:

all of the lines have been rotated counterclockwise around the center point.

The question should be, what formula could be applied to all the starting/ending points of the lines to rotate (counter)clockwise?

Where GDB1 is the point data for the lines, x1,y1 is the first point of the line, and x2,y2 is the second point of the line.
Code: [Select]
:For(A,1,L)
:{A*4+GDB1->B} .is x1
:{B+1} .is y1
:{B+3} .is x2
:{B+4} .is y2
:(Insert formula here to rotate points)
:End
And for the test to see if a point is on the line, that was to check if the ball is hitting a line.
Thanks for any future help.
EDIT:
Thanks Nemo for the explanation, that was exactly what I was looking for. Thanks!
« Last Edit: November 13, 2011, 05:12:39 pm by bilyp »

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: Math help for game
« Reply #4 on: November 13, 2011, 05:34:59 pm »
you can never write too much when describing something. however, finding the angle of the ball is only that easy if the ball is falling from straight down, but looking at your game the ball isn't going to always fall straight down. unfortunately, i'm not sure how to calculate the angle.

according to this code, to rotate the the line about the point you would have something like this:

Code: [Select]
//point of rotation is always (48, 32) (the center of the screen)
//the line is described as (X1,Y1) (X2,Y2)
//Z is the angle of rotation... however Axe represents it. binary degrees?
//A,B,C,D are all temp vars
X1-48->A
Y1-32->B
X2-48->C
Y1-32->D
48-(Acos(Z)-(Bsin(Z))->X1
32-(Asin(Z)-(Bcos(Z))->Y1
48-(Ccos(Z)-(Dsin(Z))->X2
32-(Csin(Z)-(Dcos(Z))->Y2

of course, i'm taking his word for it. i didn't actually do the math.


Offline bilyp

  • LV2 Member (Next: 40)
  • **
  • Posts: 21
  • Rating: +9/-1
  • Axing the z80
    • View Profile
Re: Math help for game
« Reply #5 on: November 13, 2011, 06:49:33 pm »
 Thanks nemo! the rotation thing worked! now i just need to optimize and do collisions...