Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Calculating normal from a line?

Author
Message
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 30th Aug 2011 00:54 Edited at: 30th Aug 2011 01:13
Hi


Given a line from P1 to P2, how would I calculate the normal vector N of that line? I need the normal to be able to calculate how a ball will bounce off it.


Thanks

TheComet

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 30th Aug 2011 01:13 Edited at: 30th Aug 2011 01:26
You need a minimum of 3 points to calculate a normal, along with knowing which side of the triangle made from those points is the front - usually this is determined by the winding of the points (DBPro uses counter-clockwise, or CCW).

Example code (using CCW):


If you want clockwise winding instead, either negate the result, or swap the U/V vectors so that U=p1-p3 and V=p1-p2

[edit]Doh, I just realised you want 2D reflection, so you need to calculate the normal for that line.

I've never needed this myself - as long as the lines the object is bouncing on are either horizontal or vertical, what I do is determine how far the object has moved past the wall and adjust by that distance to the other side of the wall.



Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 30th Aug 2011 01:58
ah crap I typed something and my browser crashed...


Anyways, before I write up more math, is what you want like this but with physics?
http://forum.thegamecreators.com/?m=forum_view&t=179370&b=6

Basically, For a given line, there are two possible normal vectors (pointing in opposite directions). Given two points P1 and P2:

X=P2-P1 //create a vector X
XRot=(-X.y,X.x) //rotate X 90 degrees
Norm1=XRot/|X| //normalize it. |X|=sqrt(X.x^2+X.y^2)
Norm2=-Norm1

There you go. 2d normal of a 2d line.


Why does blue text appear every time you are near?
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 30th Aug 2011 13:42 Edited at: 30th Aug 2011 13:42
Thanks, that worked perfectly

I have another problem now though. I use hidden lines for collision checks with the bouncing balls (circles), but the lines are able to move around and rotate. Whenever a ball hits one of the moving lines, the ball pops straight through it... I'm using your "circle and lines" collision example from the code snippets, Neoro Fuzzy

How can I make the ball bounce off correctly, even when the line moves faster than the ball (without accelerating the ball's initial speed)?


Thanks,

TheComet

Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 30th Aug 2011 22:42 Edited at: 30th Aug 2011 22:44
Well... you could probably do some weird stuff to ensure that the ball is always on the same side of a line as it was before (with some additional stuff to account for line segments), but what would be simpler would just be to adjust the time step.

IE, if your object is moving at 360 degrees per second, and your program is running at 60 frames per second, then your line would be rotating at 6 degrees per frame, which for a long line is more than enough to skip through your object.

Just calculate collision after 1 degree of movement, then increase the angle by 1 more degree, and calculate collision again, until your at the needed amount.

For a more accurate and potentially time saving way to do it, you could calculate the circle's distance from the line's center of rotation, d. Then calculate the line's rotational velocity in degrees per frame, and convert it to radians. d*Rad_Per_Frame is the arc length that the line would sweep out each frame, so if that arc length is greater than half the circle's radius, take multiple steps, otherwise just do it normally.

[edit]
so
if d*DegreesPerFrame*0.017453292>circle_radius
//check for collision multiple times per frame
endif


Why does blue text appear every time you are near?
Indicium
18
Years of Service
User Offline
Joined: 26th May 2008
Location:
Posted: 30th Aug 2011 23:30
I'd go for decoupling the display loop from the collision detection. I think it was KISTech who wrote a tutorial on it.

Login to post a reply

Server time is: 2026-07-11 03:38:38
Your offset time is: 2026-07-11 03:38:38