ok...simple NOOB problem...

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
psiko_scweek
Posts: 42
Joined: Sat Nov 12, 2005 2:30 am

ok...simple NOOB problem...

Post by psiko_scweek »

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: Select all

.
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
arrggg
Posts: 11
Joined: Thu Sep 29, 2005 10:07 am

Post by arrggg »

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.
KawaGeo
Posts: 191
Joined: Sat Aug 27, 2005 6:52 am
Location: Calif Mountains

Post by KawaGeo »

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
arrggg
Posts: 11
Joined: Thu Sep 29, 2005 10:07 am

Post by arrggg »

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
psiko_scweek
Posts: 42
Joined: Sat Nov 12, 2005 2:30 am

lol heh

Post by psiko_scweek »

alright

Code: Select all

function object.bounce&#40;o&#41;
o.dir = &#40;90 + &#40;90 - &#40;o.dir&#41;&#41; &#41;
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

Code: Select all

  _______
      /\
    /    \
          0
but instead it bounces back at.. 135 which is

Code: Select all

 0 
   \
     \
-----------
     /
   /  



sorry for the crappy pics but thats what it does :((
any ideas of what i can do? im totally stumped here.
Durante
Posts: 65
Joined: Sun Oct 02, 2005 6:07 am
Location: Austria

Post by Durante »

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: Select all

if hor then
	self.dir&#91;1&#93; = -self.dir&#91;1&#93; 
else
	self.dir&#91;1&#93; = math.pi-self.dir&#91;1&#93;
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 ;)
KawaGeo
Posts: 191
Joined: Sat Aug 27, 2005 6:52 am
Location: Calif Mountains

Post by KawaGeo »

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
psiko_scweek
Posts: 42
Joined: Sat Nov 12, 2005 2:30 am

Post by psiko_scweek »

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
KawaGeo
Posts: 191
Joined: Sat Aug 27, 2005 6:52 am
Location: Calif Mountains

Post by KawaGeo »

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
psiko_scweek
Posts: 42
Joined: Sat Nov 12, 2005 2:30 am

Post by psiko_scweek »

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
Zenurb
Posts: 106
Joined: Fri Sep 30, 2005 8:33 am
Location: United Kingdom
Contact:

Post by Zenurb »

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: Select all

         |
         |
_________|________
Proud Dvorak User
US 1.5 PSP (Original)
KawaGeo
Posts: 191
Joined: Sat Aug 27, 2005 6:52 am
Location: Calif Mountains

Post by KawaGeo »

Yes and 90 degrees around the normal on the surface in 3-D scene like a telephone pole.
Geo Massar
Retired Engineer
Durante
Posts: 65
Joined: Sun Oct 02, 2005 6:07 am
Location: Austria

Post by Durante »

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
psiko_scweek
Posts: 42
Joined: Sat Nov 12, 2005 2:30 am

normals...

Post by psiko_scweek »

ok so the normal of a surface is a line

Code: Select all

             |
             |
      ____|_____
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
KawaGeo
Posts: 191
Joined: Sat Aug 27, 2005 6:52 am
Location: Calif Mountains

Post by KawaGeo »

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
Post Reply