Advertisement

How can i figure out the coordinates of a projectile after it has moved at an angle

Started by September 08, 2016 05:54 PM
5 comments, last by szokoz 7 years, 12 months ago

Imagine I was making a pong game. If the ball starts at (0,5), the goal is a line 10 units away starting from (10,0) and ending at (10,10) and the ball moves towards the line at an angle, say 20 degrees. How can I find out where on the goal it will land?

I appreciate any answers and suggestions.

Treat the path the ball will travel as a ray, treat the goal as a line segment, and perform a ray-line segment intersection.

If the ball is a circle with a nonzero radius, you'll have to do ray-capsule intersection instead.
Advertisement

This gets easier with vector math, instead of an angle, you have a directional vector that you use for the ray. If you have walls, you'll also need to reflect the ray and compute that intersection (and if that ray intersects a wall, you compute that intersection... you'll want to limit the check by distance, or you'll look where the ball is an infinite number of bounces later)

Some examples of those methods in use? It would help me understand how I implement the direction/angle into those methods since I'm a bit confused on how that works.

So your ball has a position (x,y) and a direction vector (dx, dy).

If you want to specify the direction in terms of an angle then you need to calculate the (dx,dy) vector using the parametric equation of a circle:

dx = cos(angle)

dy = sin(angle)

Using (x,y) and (dx,dy) you have a ray. So now you can use ray-line intersection to find out the point of intersection with your 'goal'.

This is the point where the 'center' of your ball would intersect the goal.

If necessary you could apply a bit more trig to work out where the perimeter of the ball 'first' intersects the goal.

https://en.wikipedia.org/wiki/Polar_coordinate_system#Converting_between_polar_and_Cartesian_coordinates
https://en.wikipedia.org/wiki/Euclidean_vector

A vector is (x,y) in 2D and (x,y,z) in 3D. Sometimes there's a w component but you can ignore that here.

A ray is two vectors: One indicates a staring position, the other indicates the direction of travel.

A line segment is two vectors, but instead of a starting position and a direction, it's a starting and ending position.

Ray-with-other-object collisions usually solve for "t" which is a number indicating how far along the ray the collision occurs, usually in the form: collisionPoint = rayStartPoint + rayDirection * t

A capsule is a box with circles at each end (in 2D) or a cylinder with spheres at each end (in 3D), or in another way of thinking about it, the surface of all points that are a constant distance away from a line segment inside the capsule. The idea here is that instead of trying to collide your ball with a line, you simultaneously shrink the ball to a point and expand the object-to-collide-with by that radius in all directions, and in certain cases this makes the collision equations simpler to solve. When you expand a line segment by a constant radius in all directions, you get a capsule.

The reason why you could use a capsule in a pong game is that the ball might hit one of the ends of the paddle. If you use a plain two-line-segments test, you will get a collision point that isn't quite where the ball will actually collide. The difference will be more extreme the further from perpendicular the ball is moving with respect to the paddle's surface.
Advertisement

I hope it's not too late to thank you all for you help. Most Appreciated!

This topic is closed to new replies.

Advertisement