shooting Rays

Started by
8 comments, last by haphazardlynamed 17 years, 4 months ago
Sup, I want to shoot a ray from an object in the game, and I want to check if it hits a plane, so how do I shoot a ray from a moving object in 3D, and later check if it collides with a plane, and knowing the angle at which it collided would also be helpful. Thnx, btw im writing in C++

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

Advertisement
Well a plane is very big, so as long as the ray is shot in the direction of the plane then you know it's going to eventually intersect, and the angle of incidence can be found rather easily as well. Is there any other information you need? Intersection point? Time of impact? Does the ray move with the object (as though attached), does it inherit the object's velocity at the time it is shot, or is it a completely independent entity? Is the plane infinite or does it have boundaries you'd like to establish? Is the plane moving? These questions will help us tailor our answers into something useful :)
ok, the object that im shooting the ray from is moving and I need the ray to move with it, the planes are stationary but there is 6 of them, its a cube, so what I have is a ball bouncing inside the cube, and I want to tell which side its gonna hit before it hits it, the planes are limiting each other,

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

You didn't specify what your ray represents so its impossible to know whether the fact that it is coming from a moving object will even make a difference. Well if the ray/line isn't paralell to the plane then it will always collide. If you need to know the point of collission solve the line equation with the plane equation. To find the angle between a line and a plane take the angle between the line's direction and the planes normal and subtract that from pi/2(90 degrees).
n=plane normal
v = line direction vector
angle = pi/2-acos(dot(n, v)/(|n|*|v|)) ;

edit: dang Im slow at responding

Solve the balls position equation against all the planes and then it hits the one with the lowest positive time. To take into account the balls size add a vector to the position of the balls center in the direction of the normal of the plane your are checking collission (pointing towards the plane)with a length equal to the radius of the ball.
How do I "shoot" the ray in the first place? How do I make an infinite length line come out from the center of a sphere?

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

The line exists only as an equation. Parametrically, it's something like P + Dt for position P and direction D. If you break that down into x, y, and z components and then plug the parametric equations for the line into the equation for each plane, you should find a value for 't' that gives you the intersection point. If it's positive, then the ball hits the plane by going forward. It if's negative, then the ball only hits the plane by going backward and you throw out that plane. If you end up dividing by 0 when solving for 't', ignore that plane because it's parallel to the ball's direction. As the previous poster said, the plane that gives you the smallest positive 't' is the one you hit first. He also mentioned you could adjust the position of the ball to take into account its size, but it would probably just be easier to increase (or decrease depending the direction of the plane normal) the distance parameter in the plane equation by the radius of the ball.
Quote:Original post by Zipster
Well a plane is very big, so as long as the ray is shot in the direction of the plane then you know it's going to eventually intersect, and the angle of incidence can be found rather easily as well.


A plane is actually infinite in two dimensions. So as long the ray isn't parallel to the plane ( the direction of the ray is orthogonal to the planes surface normal ) there is an intersection.

Quote:Original post by greenhybrid
A plane is actually infinite in two dimensions. So as long the ray isn't parallel to the plane ( the direction of the ray is orthogonal to the planes surface normal ) there is an intersection.

Of course a plane is infinite, I wasn't using the phrase "very big" to imply it was finite. But a ray has a starting point and a direction, so there is not always an intersection subject to the constraint that 't' in the parametric equation be positive. For instance, if the ray's starting point is in front of the plane and the dot product between its direction and the plane normal is non-negative, there is no intersection.
Quote:Original post by VanillaSnake21
How do I "shoot" the ray in the first place? How do I make an infinite length line come out from the center of a sphere?


Since you want to use this ray to predict the ball's collision.
You're going to want to align this ray with the ball's velocity...

I'd represent the ray as a direction vector and starting point. Corresponding to the ball's velocity(normalized) and current position.

Ball velocity can be approximated by the ball current position - previous position.

This topic is closed to new replies.

Advertisement