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.

Code Snippets / 2D Line Segment Intersection

Author
Message
RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 14th Feb 2007 02:50
First off, I (an idiot when it comes to math) wouldn't have been able to write this snippet if Milkman wasn't around to provide some great explanation on how line segment intersection works, along with the formula's from this site.

Basically, the function intersectLines() will take the xy coordinates of two 2D lines (thats 8 coordinates in total) and return a 1 if the lines are intersecting and a 0 if not. For a lot of people, this isnt anything special, but I always struggle with things like this, so I was really happy when I got this function working (now 2D collision is a lot easier).

Code:


The parameters are pretty self explanatory, x1#/y1# are line 1's
starting position, x2#/y2# are it's end position. The same goes for line 2 using x3#/y3# and x4#/y4#.

Again, a big thanks to Milkman and his math guru powers for helping me grasp at least some of why this formula works at all.

- RUC'

RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 15th Feb 2007 00:22 Edited at: 15th Feb 2007 14:18
Ive been slowly working out the maths involved in getting 2D sliding collision working and I think Ive solved the main bulk of it.

Here's a fairly large code snippet including a few helpfull functions from a math library ive been building that calculates a normal and the proper sliding collision position when a line intersects the normal (the sliding collision position is stored in bounce.x2# and bounce.y2#).

code:


My next step is to set up more than 1 normal and get the proper sliding position after the incident ray has hit both normals (simulating concave corners).

If anyones wondering, I plan to implement this into my Gibble Blops game, Ive already built a map editor that allows for the creation of maps by drawing lines, afterwards I load all of the lines into the game and draw them. I plan on then looping through each line checking for intersection/sliding collision using a line segment cast from the player's old position to their new position. If all goes well and I get this working solidly Ill add it to the stickied "Massive Collision Resource" post.

(Use W/S to change the normal, and the mouse + arrow keys to change the incident ray in the code snippet).

<edit>

And here's a demo of what Ive gotten so far. Its working exactly as planned right now, the only problem is at 1 specific angle the sphere goes through the wall a bit and then gets repositioned, I know why, I just dont know if I can figure out how to fix it or not .

</edit>

- RUC'

Attachments

Login to view attachments
TEH_CODERER
21
Years of Service
User Offline
Joined: 12th Nov 2003
Location: Right behind you!
Posted: 15th Feb 2007 10:44
Nice and I like the plan for Gibble Blops!

RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 15th Feb 2007 18:44 Edited at: 16th Feb 2007 18:30
Thanks Coder,

Alright, I think ive gotten the sliding collision functions working! Ive only tested it against a few different normal sets right now, but its worked flawlessly with every single one, no more vibrating or anything.

The command-set:


slidingCollision2D() takes 9 parameters. The first 4 are the line segment coordinates of the player (the player's old coordinates to their new coordinates). The next 4 are the normal's coordinates, and the last one is the radius of the player's collision circle.

The other 2 commands, getSlideX() and getSlideY(), return the x and y coordinate of the calculated slide position. You'll need to dim slide(2) for the time being in order to use the function.

When I get home from school later today Ill post up a commented demo that allows you to draw normals and move around with sliding collision on your creation. If nobody finds any bugs then Ill polish it up and release it in a new thread in code snippets and a link to it from the Massive Collision Resource.


- RUC'

wildbill
18
Years of Service
User Offline
Joined: 14th Apr 2006
Location:
Posted: 16th Feb 2007 17:57
I wish my math was only as bad as yours.
I can see some really useful stuff from this. Various weapon intercepts (lead the target and fire) for one.
TEH_CODERER
21
Years of Service
User Offline
Joined: 12th Nov 2003
Location: Right behind you!
Posted: 16th Feb 2007 21:33
A little editor/game using your excellent code.


RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 17th Feb 2007 22:11
Cool, I still have to figure out the corners though . Ill also have to take a look at fixing the radius, I think Im multiplying something wrong.

TEH_CODERER
21
Years of Service
User Offline
Joined: 12th Nov 2003
Location: Right behind you!
Posted: 18th Feb 2007 12:59
Yeah, it would seem that you are multiplying the radius by 2 for no apparent reason!


RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 18th Feb 2007 14:53
Thats what I was thinking it was (havent taken a look yet) but I remember beforehand I had to multiply it by 2 to get it right on the surface, then I changed some formulas around and forgot to remove it.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 19th Feb 2007 04:56
you can pass right through the line when coming from below it.

I've done a few 2D routines you might find useful.
http://forum.thegamecreators.com/?m=forum_view&t=76419&b=6

RUCCUS
19
Years of Service
User Offline
Joined: 11th Dec 2004
Location: Canada
Posted: 19th Feb 2007 05:23
I did that knowing that would happen, just like in 3D, this way later on I can cull the system by only checking normals facing the user, under the assumption that the user will never be behind a normal if set up right. But Ill take a look at that link, any help is good help.

Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 19th Feb 2007 05:25
ahh, good idea then.

Login to post a reply

Server time is: 2024-11-26 13:58:19
Your offset time is: 2024-11-26 13:58:19