Help with some maths

Im pretty new to c++ programming and my maths is really rusty. What I'm trying to do is the following:

I'm trying to write a simple c++ program that outputs an objects current height once it hits a specific point. The object I'm trying to accomplish is that you have an object that starts at a varied position a moves off under a random velocity with gravity attached. If the ball collides with a wall or another object, it should move backward, with no energy loss, but still continue to fall due to gravity. Once the ball has reached a specific height, output that value.

Now, all I'm trying to do right now is check to see if my ball has gone beyond my width bounds. But for the life of me I can't see why my last if statement at the bottom wont call.

Am I missing / doing something really stupid?

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` `````` int _tmain(int argc, _TCHAR* argv[]) { float velocity; float height, targetHeight; float gravity; float time; float angle; float width; float move; float distance; gravity = 9.80f; time = 0; distance = 0; cout << "Set Height\n"; cin >> height; cout << "Set target height\n"; cin >> targetHeight; cout << "Set Angle ( 0 - 90 ): \n"; cin >> angle; angle *= 3.14 * 180; // convert to radians cout << "Set velocity (0 - 100): \n"; cin >> velocity; cout << "Set Play field Width: \n"; cin >> width; while( height >= target ) { time++; distance += velocity * cos(angle) * time; height += (velocity * sin(angle) * time) - (gravity * pow(time, 2) ) / 2; } if( distance == width) { cout << "You've hit the wall\n"; } return 0; }``````
what is target? i think it should be targetHeight.

Is the object moving upward or downward? If it is moving upward, then it should be
`while (height <= targetHeight) ` and if it is moving downward, then the velocity should be negative.

Also, I feel you should also change the value of velocity in each iteration.

closed account (D80DSL3A)
Your degrees to radians conversion is incorrect as well. It should be
`angle *= 3.14 / 180; // convert to radians `
The last if will never call, because you're using direct comparison with floating points. Use an expected range of accuracy:
 ``1234`` ``````if((distance <= width + .0001) && (distance >= width - .0001) { /*do stuff*/ }``````
Last edited on
Topic archived. No new replies allowed.