| View previous topic :: View next topic |
| Author |
Message |
psiko_scweek
Joined: 12 Nov 2005 Posts: 42
|
Posted: Sat Nov 12, 2005 2:34 am Post subject: ok...simple NOOB problem... |
|
|
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...
can anyone help me?!
| Code: | .
function object.bounce(o)
o.dir = (o.dir - 90)
if o.dir < -360 then o.dir = o.dir+360 end
end
|
that only works with 45 degree angles. if anyone could help me get a code that would work for any angle id really appreciate it!
thanks in advance!!
matt |
|
| Back to top |
|
 |
arrggg
Joined: 29 Sep 2005 Posts: 11
|
Posted: Sat Nov 12, 2005 3:20 am Post subject: |
|
|
assuming:
Begining angle 45 = after bounce 135
Begining angle 80 = after bounce 100
function object.bounce(o)
o.dir = (90 + (90 - (o.dir)) )
if o.dir < -360 then o.dir = o.dir+360 end
end
This is untested. |
|
| Back to top |
|
 |
KawaGeo
Joined: 27 Aug 2005 Posts: 191 Location: Calif Mountains
|
Posted: Sat Nov 12, 2005 3:28 am Post subject: |
|
|
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. :) _________________ Geo Massar
Retired Engineer |
|
| Back to top |
|
 |
arrggg
Joined: 29 Sep 2005 Posts: 11
|
Posted: Sat Nov 12, 2005 3:40 am Post subject: |
|
|
| 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 |
|
| Back to top |
|
 |
psiko_scweek
Joined: 12 Nov 2005 Posts: 42
|
Posted: Sat Nov 12, 2005 4:11 am Post subject: lol heh |
|
|
alright
| Code: | function object.bounce(o)
o.dir = (90 + (90 - (o.dir)) )
if o.dir < -360 then o.dir = o.dir+360 end
end |
ok that should work...but when it hits the top of the screen at say
45 degrees, it should bouce out at 315
but instead it bounces back at.. 135 which is
| Code: | 0
\
\
-----------
/
/ |
sorry for the crappy pics but thats what it does :((
any ideas of what i can do? im totally stumped here. |
|
| Back to top |
|
 |
Durante
Joined: 02 Oct 2005 Posts: 65 Location: Austria
|
Posted: Sat Nov 12, 2005 11:29 am Post subject: |
|
|
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):
| Code: |
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 ;) |
|
| Back to top |
|
 |
KawaGeo
Joined: 27 Aug 2005 Posts: 191 Location: Calif Mountains
|
Posted: Sun Nov 13, 2005 5:34 am Post subject: |
|
|
| 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. _________________ Geo Massar
Retired Engineer |
|
| Back to top |
|
 |
psiko_scweek
Joined: 12 Nov 2005 Posts: 42
|
Posted: Mon Nov 14, 2005 7:35 am Post subject: |
|
|
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.
thanks in advance!
regards,
PSIKO |
|
| Back to top |
|
 |
KawaGeo
Joined: 27 Aug 2005 Posts: 191 Location: Calif Mountains
|
Posted: Mon Nov 14, 2005 10:16 am Post subject: |
|
|
Wanna examples?
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.
Have fun.
PS Real balls, mind you! _________________ Geo Massar
Retired Engineer |
|
| Back to top |
|
 |
psiko_scweek
Joined: 12 Nov 2005 Posts: 42
|
Posted: Mon Nov 14, 2005 4:51 pm Post subject: |
|
|
thanks for that suggestion,
ill check that out tomorrow after i get out of work!
thanks again, hopefully you will see a game from me soon enough!
Regards,
PSIKO |
|
| Back to top |
|
 |
Zenurb
Joined: 30 Sep 2005 Posts: 106 Location: United Kingdom
|
Posted: Mon Nov 14, 2005 9:36 pm Post subject: |
|
|
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
----------------------------------
the normal would be
| Code: |
|
|
_________|________
|
_________________ Proud Dvorak User
US 1.5 PSP (Original) |
|
| Back to top |
|
 |
KawaGeo
Joined: 27 Aug 2005 Posts: 191 Location: Calif Mountains
|
Posted: Tue Nov 15, 2005 1:02 am Post subject: |
|
|
Yes and 90 degrees around the normal on the surface in 3-D scene like a telephone pole. _________________ Geo Massar
Retired Engineer |
|
| Back to top |
|
 |
Durante
Joined: 02 Oct 2005 Posts: 65 Location: Austria
|
Posted: Tue Nov 15, 2005 2:13 am Post subject: |
|
|
RE finding the normal of a vector in 2D:
That's actually very simple, given v = (a, b) the normal vectors are (-b, a) and (b, -a).
For 3D look here |
|
| Back to top |
|
 |
psiko_scweek
Joined: 12 Nov 2005 Posts: 42
|
Posted: Tue Nov 15, 2005 2:26 am Post subject: normals... |
|
|
ok so the normal of a surface is a 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?
again thanks!
PSIKO |
|
| Back to top |
|
 |
KawaGeo
Joined: 27 Aug 2005 Posts: 191 Location: Calif Mountains
|
Posted: Tue Nov 15, 2005 4:21 am Post subject: |
|
|
Let's take a step a time, ok?
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. _________________ Geo Massar
Retired Engineer |
|
| Back to top |
|
 |
|