Vector problem

Blitz3D Forums/Blitz3D Programming/Vector problem

Stollentroll(Posted 2012) [#1]
Hi there and a Happy New Year!

Currently I am having the following problem:

I'd like to have light beam that "travels" through a 2D space until it hits a wall, then it bounces off. (Angle of incident equals angle of relection.)

The basic concept is easy.
I have a velocity vector with "X" and "Y". I add these values to the current x and y value of the light beam.
When the beam hits the wall I just multiply the according velocity vector with "-1" and just keep the other one.

(See example code below for clarification.)

This works nice and easy.

Now the question comes up: Right now my "borders" are orthogonal. (Like a billiard table viewed from above.) But what if these "reflection walls" are tilted by any degree?

How do I now calculate the correct vector after the "bouncing"?

Do I need vectors or will I have revert to triangle calculation? Mirroring a vector? Rotating the vector? Sin, Cos, ..... ?

I am absolutely cluelss what the smartest approach would be.

If anyone could come up with a suggestion that would be great. What kind of math is required to solve this problem?

The light does not have to slow down, there is no gravity, just 2D space. The collision between the light beam and the "tilted mirror" is not of importance now. The question is only how to calculate the correct reflection angle.

Thanks a lot!

Stollentroll




----------- EXAMPLE CODE ---------------
beam_x = 50
beam_y = 50
velocity_x = 5
velocity_y = 2
WHILE NOT KEYHIT(1)
  beam_x = beam_x + velocity_x
  beam_y = beam_y + velocity_y
  IF (beam_x > 320) OR (beam_x < 10) then velocity_x = velocity_x * -1
  IF (beam_y > 400) OR (beam_y < 10) then velocity_y = velocity_y * -1
  PLOT beam_x, beam_y
WEND

-----------------------------------------

Last edited 2012


Kryzon(Posted 2012) [#2]
Hi Stollentroll, happy new year and welcome back.
I don't mean this in a rude way, but there's plenty of material to be found in Google.

In fact, it happened to find these entries that are totally related to Blitz:
How to Mirror/Reflect a vector against another Vec (BlitzMax)
Reflecting a 2d vector by a 2d normal? (BlitzMax)

The code should be easily ported to Blitz3D. Best of luck!


Stollentroll(Posted 2012) [#3]
Hi Kryzon,

you're not rude and I really would love to use Google to find the according math formula.
But I simply did not know what kind of math I had to look into. I wasn't sure whether vectors are really the way to go. Or if I had to use some triangle calculation.

So, thank you for your reply. Vectors seem to be the solution.

And I will take a look at the two links you posted. They will surely help me to solve my problem.

Cheers,

Stollentroll


Warpy(Posted 2012) [#4]
I'm still loitering, in case you need more help.


Kryzon(Posted 2012) [#5]
@Stollentroll: I'm thinking the vectors used in those links don't have an "origin" - i.e: they only have an end point; the origin is assumed to be [0,0].
This kind of vector would be your beam's speed vector, which you can find with this:
Beam\SpeedX = Beam\Speed * Cos(Beam\Angle)
Beam\SpeedY = Beam\Speed * Sin(Beam\Angle)
The SpeedX and SpeedY members are what you should input to that function - this speed vector is considered to be starting from a [0,0] origin.
Since you also need the walls' normals, you can use the above but with your walls' angles, and with any "speed" value since the size of this normal doesn't matter (use 1.0 if you will).

@Warpy: Hi. If you wouldn't mind, could you please explain how your function works?
method reflectVector(vectx#,vecty#,normx#,normy#)
	dotprod#=-vectx*normx-vecty*normy
	relx=vectx+2*normx*dotprod
	rely=vecty+2*normy*dotprod
end method
I'm having a hard time understanding how it mirrors the vector - more specifically, what that dot-product is responsible for.


Warpy(Posted 2012) [#6]
Split the velocity into two components - the part heading directly into the wall and the part heading along the wall. When something bounces, the part heading into the wall points in the opposite direction, and the part heading along the wall isn't changed.

The dot product is a measure of how much the vector is pointing directly into the wall. The normal vector (normx,normy) is at right angles to the wall, pointing outwards. If you multiply the normal vector by the dot product you get a vector representing the component of the velocity heading into the wall. If you add 2 times that vector to the velocity, you get the velocity after the ball has bounced.


Kryzon(Posted 2012) [#7]
Sweet. Thanks a lot!