954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Finding point of impact on a 2D slope

Hi,

Basically I have a game type scenario in which objects are falling from the sky onto uneven terrain below but my problem is this:
I can't figure out how to determine when an object has actually collided with a slope (the point of impact) because obviously the y value is different depending upon the x value.

So.....given that I can calculate the slopes gradient and I know the two points from which I define it (the highest and lowest points), how can I find the y value on the slope (i.e. the height) using the x value of the object that may be colliding with it?

caged_fire
Junior Poster
128 posts since Mar 2009
Reputation Points: 16
Solved Threads: 9
 

Are the objects falling vertically or diagonally? It makes a big difference in the complexity.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

Isn't this basic math? If you know the terms (slope, axis, etc) you already know (or can find) the correct equations. Turn them into code.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Well for now they will be falling vertically but some will be falling diagonally when I implement that later.
I also realise that this is basic math but I've not done all that much C++ so its writing the code that I'm struggling with.

caged_fire
Junior Poster
128 posts since Mar 2009
Reputation Points: 16
Solved Threads: 9
 

I don't see any struggle at all. You have posted no code whatsoever.

An equation is an equation. If you've done any C++ you've done an equation, haven't you? + - * / ?

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

If they're falling vertically, all you need to do is take the X-coordinate of the projectile and plug that into the equation that defines the "surface". Once the projectile's Y-coordinate matches the solution for the equation, you have impact. This, of course, ignores any radius on the projectile.

Just break the algebra down into smaller sub-problems then write your statements to solve the sub-problems. Once you have those, combine them into the final solution. I'm sure you're familiar with the slope-intercept method (y=mx+b). If you already know m and b, you're all set, just plug in and solve. You can still find the solution algebraically even if you only know 2-points.

We really can't offer better information unless we know what you already do or don't know.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

Just imagine a ball falling in an uneven ground. Before the ball hits
the ground, you have to know some information. Its (x,y) position. Its
velocity vector. Its mass. And so on depending how detail the object is represented. As suggested, if the ball falls vertically, then you just take
its x position. This x position of where the ball is at time t, when falling
will be the same as the point of impact. Thus as suggested, you can just
get the terrain's height from the x position of the ball. Now all you
have to do is calculate the new velocity vector of the ball after the point
of impact. If the ball is falling from any angle, then it will be almost be
the same solution. You use the ball.x position. The calculation for the
velocity vector after the point of impact will be a little different.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

Generally I think people misunderstood what I meant but it doesn't matter because I've sorted it now :). I think perhaps I was being a bit 'slow' earlier so to speak.

Thanks for your help

caged_fire
Junior Poster
128 posts since Mar 2009
Reputation Points: 16
Solved Threads: 9
 

We knew you wanted code suggestions, but without a starting point from you we really couldn't help any more than we tried to. Glad you figured it out.

Fbody
Posting Maven
2,930 posts since Oct 2009
Reputation Points: 833
Solved Threads: 393
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You