### Author Topic: Reflection in 2D  (Read 1709 times)

0 Members and 1 Guest are viewing this topic.

#### mike_g

• Amiga 1200
• Posts: 435
• Karma: 34
##### Reflection in 2D
« on: June 22, 2007 »
If something moving at angle_a hits a flat surface that runs along angle_b how could I work out the angle at which the thing moving along angle_a bounces off the surface? I did trigonometry once but forgot it all

If someone could give me a little explanation, about how I could do this it would be very helpful

#### Shockwave

• good/evil
• Founder Member
• Posts: 17378
• Karma: 497
• evil/good
##### Re: Reflection in 2D
« Reply #1 on: June 22, 2007 »
Well Mike, if it's just hitting the floor or wall and the surface that is being hit is not moving just reversing the direction of impact would work. However it depends what you're after, if the surface being hit is angled you'll have to use a little bit of math.

Probably best if you describe what you're trying to do in more detail
Shockwave ^ Codigos
Challenge Trophies Won:

#### mike_g

• Amiga 1200
• Posts: 435
• Karma: 34
##### Re: Reflection in 2D
« Reply #2 on: June 22, 2007 »
Yeah maybe I'll draw a little diagram or something to try show what I mean.

#### Shockwave

• good/evil
• Founder Member
• Posts: 17378
• Karma: 497
• evil/good
##### Re: Reflection in 2D
« Reply #3 on: June 22, 2007 »
Sure, do that and we'll fix it for you in no time
Shockwave ^ Codigos
Challenge Trophies Won:

#### Stonemonkey

• Pentium
• Posts: 1310
• Karma: 96
##### Re: Reflection in 2D
« Reply #4 on: July 09, 2007 »
Crappy drawing time. Along with a little bit of FB code to show how reflections can be calculated. I think it can be done with trig but I much prefer this way. Maybe someone else who's done it with trig could post something too.

Code: [Select]
`option explicitoption byval'p1 and p2 are 2 points on the line to be reflected off'vx,vy is the incoming vector to be reflected. The values in vx,vy are modified by this sub to contain the reflected vector.sub vector_reflect(p1_x as single,p1_y as single,p2_x as single,p2_y as single,byref vx as single,byref vy as single)'get the normal (line perpendicular to) of the line to be reflected off. Then normalise it.    dim as single nx=p2_y-p1_y,ny=p1_x-p2_x    dim as single d=1.0/sqr(nx*nx+ny*ny)    nx*=d    ny*=d    'the dot product gives the shortest distance from the end of the vector to the collision line (can be on the line beyond p1 or p2)'multiply by 2.0 and will give the distance to the point on the opposite side of the line in the direction of the normal.'The vector from that new point to the point of collision is the new reflected vector.    d=(vx*nx+vy*ny)*2.0 vx-=nx*d vy-=ny*dEnd subsub main()    screenres 640,480,32,2    screenset 1,0    randomize timer    dim as integer mouse_x,mouse_y    dim as single p1x=100+rnd*440,p1y=100+rnd*280    dim as single p2x=100+rnd*440,p2y=100+rnd*280    dim as single cx=(p1x+p2x)*.5,cy=(p1y+p2y)*.5,vx,vy    do        cls'draw the wall        color &hffffff        line (p1x,p1y)-(p2x,p2y)'draw the initial vector        color &hff        getmouse(mouse_x,mouse_y)        line(mouse_x,mouse_y)-(cx,cy)'calculate and draw the reflected vector        color &hff0000        vx=cx-mouse_x        vy=cy-mouse_y        vector_reflect(p1x,p1y,p2x,p2y,vx,vy)        line(vx+cx,vy+cy)-(cx,cy)        flip            loop until inkey\$<>""end submain`
« Last Edit: July 09, 2007 by Stonemonkey »

#### mike_g

• Amiga 1200
• Posts: 435
• Karma: 34
##### Re: Reflection in 2D
« Reply #5 on: July 11, 2007 »
Nice one stonemonkey, I couldent have drawn a better diagram if I tried

This looks like what I was after. I'll have a good read through the code. Cheers.