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?

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

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.

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.

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? + - * / ?

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.

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.