Jump to content
  • Advertisement
Sign in to follow this  
Zeraan

Line intersection problem

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok, I've been studying Andre LaMothe's TOTWGPG, particularly on the line collision detection. I'm working on a space shooter, and I want the ship to hit a force field and rebound. So that's the reason why I'm doing this. Problem is, I got the line collision down, the ship does rebound. But the problem is, the line is not where it's supposed to be. It's diagonal, when it was supposed to be horizonal, and it extends longer than it should. I don't fully understand how Andre explains it, I do understand some, but not all... :( Here's the code:
void ReactCircleVsLineCollision(float X1, float Y1, float R, float& XVel, float& YVel, float X2, float Y2, float X3, float Y3)
//X1, Y1 is the ship coordinates, R is radius, XVel, YVel is the ship's velocity
//X2,Y2,X3,Y3 is line segment endpoint
{
	float length,s,t,s1x,s1y,s2x,s2y,p0x,p0y,p1x,p1y,p2x,p2y,p3x,p3y,xi,yi,npx,npy,Nx,Ny,Fx,Fy;

	// build up vector in direction of trajectory

	p0x = X1;
	p0y = Y1;

	// this is the velocity vector used as segment 1

	p1x = X1 + XVel;
	p1y = Y1 + YVel;

	s1x = p1x - p0x;
	s1y = p1y - p0y;

	// normalize

	length = sqrt((s1x*s1x) + (s1y * s1y));

	s1x = R * s1x / length;
	s1y = R * s1y / length;

	p1x = p0x + s1x;
	p1y = p0y + s1y;

	// build up vector based on line

	p2x = X2;
	p2y = Y2;

	p3x = X3;
	p3y = Y3;

	// normalize s2x, s2y to create a perpendicular collision vector from the center

	length = sqrt((s2x*s2x) + (s2y*s2y));

	s1x = R * s2y / length;
    s1y = -R * s2x / length; 
    p1x = p0x + s1x;
    p1y = p0y + s1y;

    // compute s and t, the parameters
    // the problem most likely lies here...
    s = (-s1y*(p0x-p2x) + s1x*(p0y-p2y))/(-s2x*s1y + s1x*s2y);
    t =  (s2x*(p0y-p2y) - s2y*(p0x-p2x))/(-s2x*s1y + s1x*s2y);

    // test for valid range (0..1)
    if (s >= 0 && s <=1 && t >= 0 && t <=1)
	{
		xi = p0x+s*s1x;
		yi = p0y+s*s1y;

		// now we know point of intersection, reflect at current location

		// N = (-I . N')*N'
		// F = 2*N + I
		npx = -s2y;
		npy = s2x;

		// normalize p
		length = sqrt(npx*npx+npy*npy);
		npx/=length;
		npy/=length;

		// compute N = (-I . N')*N'

		Nx = -(XVel*npx + YVel*npy)*npx;
		Ny = -(XVel*npx + YVel*npy)*npy;

		// compute F = 2*N + I
		Fx = 2*Nx + XVel;
		Fy = 2*Ny + YVel;

		// update velocity with results
		XVel = Fx;
		YVel = Fy;
	}
}




Share this post


Link to post
Share on other sites
Advertisement
let me clarify the problem:

The line vs collison reaction works, but when I fly to the line, it's not there. So I fly around, and found the line in another place, and messed up at that too. I was wondering where I went wrong in the math? I like to get things working, then figure out how they work by twiddling with them, that's how I learn the best.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zeraan
let me clarify the problem:

The line vs collison reaction works, but when I fly to the line, it's not there. So I fly around, and found the line in another place, and messed up at that too. I was wondering where I went wrong in the math? I like to get things working, then figure out how they work by twiddling with them, that's how I learn the best.


Are you talking about the collision or the visual on screen? I mean, are the visual and collision response out of synch?

Share this post


Link to post
Share on other sites
interesting that your rotating the lines to see if they intersect, that could be an issue. That could very well be your problem, things are no longer in the right place if you normalize them out of sync with one another... I started a thread on polygon intersections. You might want to review it. I included a link to a very nice article on it. http://www.gamedev.net/community/forums/topic.asp?topic_id=332727

Share this post


Link to post
Share on other sites
Really? I'm rotating the lines? Where? I'm been trying to figure out Andre's code, and obiviously I didn't understand much...

As for the actual collision response, the line drawn doesn't match so I didn't draw the line, but I rendered 3D objects at endpoints of the line (proper place) and I know that it's where its supposed to be because the line (as the previous poster said) rotated on one of those endpoints. But the collision where the line is, it works... Seems like I need to remove the rotating part.

Thanks for your help!

Edit: Thanks for the link, studying it now. Lets see if I can figure this one out for myself.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!