Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5946967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:53:52+00:00 2026-05-22T16:53:52+00:00

I wrote a program that will simulate a ball being thrown off a 50

  • 0

I wrote a program that will simulate a ball being thrown off a 50 meter building.
I added in collision detection by reversing the velocity in the y direction when the ball hits the ground (y < 0), keeping the horizontal velocity the same, and multiplying both velocities by some min value, so that the ball will ultimately come to a rest.

 #include<stdio.h>
 #include<math.h>
 #include <stdlib.h>

 int main() {

     FILE *fp; 
     FILE *fr;

     float ax = 0, ay = 0, x = 0, y = 0, vx = 0, vy = 0;
     float time = 0, deltaTime = .001; 

     float min = -.00000000001; 
     int numBounces = 0;

     fr = fopen("input_data.txt", "rt"); 

     fp = fopen( "output_data.txt", "w" ); 

     if(fr == NULL){ printf("File not found");} 

     if(fp == NULL){ printf("File not found");} 

     fscanf(fr, "ax: %f ay: %f x: %f y: %f vx: %f vy: %f\n", &ax, &ay, &x, &y, &vx, &vy); 

     while (vx > min && vy > min) {

          time = time + deltaTime;
          vx = vx + ax*deltaTime;
          vy = vy + ay*deltaTime;
          x = x + vx*deltaTime + (.5*ax*deltaTime*deltaTime);
          y = y + vy*deltaTime + (.5*ay*deltaTime*deltaTime);  

          fprintf(fp, "%f\t%f\t%f\t%f\t%f\t%f\t%f\t\n", ax, ay, x, y, vx, vy, time);

     //Collision occurs; implement collision response
          if(y < 0) {
               vx = vx + ax*deltaTime*(.00001);
               vy = -(vy + ay*deltaTime*(.00001));
               numBounces++;

               fprintf(fp, "%f\t%f\t%f\t%f\t%f\t%f\t%f\t\n", ax, ay, x, y, vx, vy, time);
      }
 }

fclose(fp); 
fclose(fr); 

system ("PAUSE"); 
return 0;

 }

I am not getting the correct values needed to produce a correct graph of the data.
It could be because my conditions in the while loop need to be changed, or that I did not implement collision response correctly.

Here is also some sample data:

ax: 0 ay: -9.8 x: 0 y: 50 vx: 8.66 vy: 5

Picture of data plot

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:53:52+00:00Added an answer on May 22, 2026 at 4:53 pm

    for not outputing anything you can try fflush(fp) at the end of each cycle. and as far as I can see in your code your object gets some more speed whenever it hits the ground you have to change vy = -(vy + ay*deltaTime*(.00001)) to vy = -(vy - ay*deltaTime*(.00001)) to correct it. you can also create a better implementation for collision if you calculate the exact time of collision whenever y < 0 and then move object down, change speeds, and move object up for the rest of cycle to have more realistic collision.

    we know that deltaY = 1/2*ay*t^2 + vy*t so we can compute t using the folling formula :

    assuming py is the current height of object(it's distance to ground)
    => -py = 0.5 * ay* t * t + vy * t
    => 0 = 0.5 * ay * t * t+ vy * t + py
    => t = (-vy +- sqrt(vy*vy - 2 * ay * py)) / (2 * ay)
    

    and since t has to be positive and knowing that ay is negetive and py is positive, we can assume the currect answer is

    => tc = (sqrt(vy*vy - 2 * ay * py) - vy) / 2 / ay
    

    now we have tc which is time of collision. so we have to reverse the last changes in position and speed, then just step time tc seconds and then reverse vy and step deltaTime - tc seconds to complete that frame. so inside the if condition would be like (I just may have some problems doing the math, so if by any chance you didn’t get expected results jsut doublecheck all equations):

    if (y < 0) {
        float tc = (sqrt(vy*vy - 2 *ay * y)) / 2 / ay;
        x = x - vx*deltaTime - (.5*ax*deltaTime*deltaTime);
        y = y - vy*deltaTime - (.5*ay*deltaTime*deltaTime);
        vx = vx - ax * deltaTime;
        vy = vy - ay * deltaTime;
        vx = vx + ax * tc;
        vy = vy + ay * tc;
        x = x + vx*tc + (.5*ax*tc*tc);
        y = y + vy*tc + (.5*ay*tc*tc);
        vy = -(vy - ay*deltaTime*(.00001));
        // you can also change above line and simply write 
        // vy = vy * -0.99;
        // that will also create friction as you want it to be there
        vx = vx + ax * (deltaTime - tc);
        vy = vy + ay * (deltaTime - tc);
        x = x + vx* (deltaTime - tc) + (.5*ax* (deltaTime - tc)* (deltaTime - tc));
        y = y + vy* (deltaTime - tc) + (.5*ay* (deltaTime - tc)* (deltaTime - tc));
        numBounces++;
    
        fprintf(fp, "%f\t%f\t%f\t%f\t%f\t%f\t%f\t\n", ax, ay, x, y, vx, vy, time);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a test program thinking that the address of p1 will be less
I wrote a dll in VS 2005 that will be loaded by another program
I wrote a simple program that will push to another screen whenever a field
I wrote a program in windows that will play binary audio sent to it
I would like to write a program that will identify a machine( for licensing
Is it possible to write a program that will change the phone numbers a
I am using VB .NET to write a program that will get the words
I am trying to write a fragment program that will take a texture and
Hey there. I'm trying to write a small program that will read the four
You will write a program that evaluates the integral of sin(x) using the left-hand

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.