ok i have my objects moving based on speed and direction...
and i want them to bounce when they reach the side of the screen.
here is my bounce code thus far, its not as accurate as it should be...
I wrote an algorithm for ball bouncing in other language. No time to dig up where I put it on my 160GB HD. But here is a hint:
Say the moving ball has a new position (x2,y2) to move from the previous position (x1,y1). If x2 is outside of the vertical wall, mirror it and keep y2 unchanged. Likewise, for y2. Best way to figure out is to draw a diagram to measure the bouncing off the wall.
KawaGeo wrote:I wrote an algorithm for ball bouncing in other language. No time to dig up where I put it on my 160GB HD. But here is a hint:
Say the moving ball has a new position (x2,y2) to move from the previous position (x1,y1). If x2 is outside of the vertical wall, mirror it and keep y2 unchanged. Likewise, for y2. Best way to figure out is to draw a diagram to measure the bouncing off the wall.
Treat my hint as your homework. :)
That's great if your walls are Up-down or left-right, but what if the walls are at an angle
if you only have horizontal and vertical walls do it like KawaGeo suggested, or, alternatively, if you want to use polar coordinates for the ball direction use my method from Crystalise (hor == horizontal wall, use 180 instead of math.pi if you use degrees):
if hor then
self.dir[1] = -self.dir[1]
else
self.dir[1] = math.pi-self.dir[1]
end
If you also want to support walls at arbitrary angles you'll have to determine which side of the wall the ball hits from and then mirror the movement vector by adding 180 and subtracting the correct wall angle. If that makes any sense ;)
arrggg wrote:That's great if your walls are Up-down or left-right, but what if the walls are at an angle
The author of this thread mentioned "the side of the screen". I presumed he meant vertical wall. But, your question sounds interesting. Imagine putting a normal at the angled wall where the ball hits and measuring the angle between the travel line and the normal. Put the bounce line from the normal with the measured angle and use the distance in the "mirror" to put the new position (x2,y2) for the ball to move
Rather complicated but it should work. Hope I made it clear.
ok, this is prolly going to sound very very noobish..and for that im sorry...
ive read about normals and vector math and the such but i dont understand it, if someone could give me and example of how to find the normal of something i would really appreciate it.
a simplified explanation would work as well...heh
let me reiterate that im not asking anyone to do this for me, i just learn better by example, thats all.
We have plenty. Nearly all Lua scripts are open-source. They are good examples. Visit http://psp-news.dcemu.co.uk/ at the left side. You might want to "study" one called Jezzball. I haven't take a look at it yet. I think it is a good example - lots of balls bouncing around.
I did normals and things in high school physics, I could be (and am probably) wrong, but isn't the normal of a surface, 90 degrees from it? say you have a horizontal line
i have a question...(sorry for being such a bother about this!) but how would i go about getting the perpendicular line? i know its 90 degrees from the edge of the surface, but thats where im at a lose how would i make the game understand the surface? as of right now the entire movement in my game is based on angle and speed.
durante: you said a normal of a vector is v = (a,b) and the normals are
(-b,a) and (b,-a). is v the velocity? or am i just completely wrong?
What Durante meant is that v is a vector like an arrow, starting from the origin point (x0,y0) to the destination point (a,b). If you draw a diagram, you would understand better. Draw x-axis and y-axis lines. The intersection is the origin point. Mark 'a' on the x-axis and 'b' on the y-axis. Draw an arrow from (x0,y0) to (a,b). Remember v is not a number but just a line and both a and b are numbers. Now, come the normal vectors: (-b,a) and (b,-a). What the hell are they? They are just vectors or arrows. Say the first starts from (x0,y0) to (-b,a). Remember '-b' which is the first element of (-b,a) is ON the x-axis and the second is on the y-axis. Draw that arrow. You'll see that the normal vector is perpendicular (90 degrees) to the given vector. You do likewise for the second normal vector. I hope I made it clear.
If you dont understand something, draw a diagram like I always did during my engineering career.