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 8896875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:11:22+00:00 2026-06-15T00:11:22+00:00

I’d like to throw a ball (with an image) into a 2d scene and

  • 0

I’d like to throw a ball (with an image) into a 2d scene and check it for a collision when it reached some distance. But I can’t make it “fly” correctly. It seems like this has been asked like a million times, but with the more I find, the more confused I get..
Now I followed this answer but it seems, like the ball behaves very different than I expect. In fact, its moving to the top left of my canvas and becoming too little way too fast – ofcouse I could adjust this by setting vz to 0.01 or similar, but then I dont’t see a ball at all…

This is my object (simplyfied) / Link to full source who is interested. Important parts are update() and render()

var ball = function(x,y) {

  this.x        = x;
  this.y        = y;
  this.z        = 0;
  this.r        = 0;
  this.src      = 'img/ball.png';
  this.gravity  = -0.097;

  this.scaleX   = 1;
  this.scaleY   = 1;

  this.vx       = 0;
  this.vy       = 3.0;
  this.vz       = 5.0;

  this.isLoaded = false;

  // update is called inside window.requestAnimationFrame game loop
  this.update = function() {
    if(this.isLoaded) {
      // ball should fly 'into' the scene
      this.x += this.vx;
      this.y += this.vy;
      this.z += this.vz;

      // do more stuff like removing it when hit the ground or check for collision
      //this.r += ?

      this.vz += this.gravity;
    }
  };

  // render is called inside window.requestAnimationFrame game loop after this.update()
  this.render = function() {
    if(this.isLoaded) {

      var x       = this.x / this.z;
      var y       = this.y / this.z;

      this.scaleX = this.scaleX / this.z;
      this.scaleY = this.scaleY / this.z;

      var width   = this.img.width * this.scaleX;
      var height  = this.img.height * this.scaleY;

      canvasContext.drawImage(this.img, x, y, width, height);

    }
  };

  // load image
  var self      = this;
  this.img      = new Image();
  this.img.onLoad = function() {
    self.isLoaded = true;
    // update offset to spawn the ball in the middle of the click
    self.x        = this.width/2;
    self.y        = this.height/2;
    // set radius for collision detection because the ball is round
    self.r        = this.x;
  };
  this.img.src = this.src;

} 

I’m also wondering, which parametes for velocity should be apropriate when rendering the canvas with ~ 60fps using requestAnimationFrame, to have a “natural” flying animation

I’d appreciate it very much, if anyone could point me to the right direction (also with pseudocode explaining the logic ofcourse).

Thanks

  • 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-06-15T00:11:24+00:00Added an answer on June 15, 2026 at 12:11 am

    I think the best way is to simulate the situation first within metric system.

    speed = 30; // 30 meters per second or 108 km/hour -- quite fast ...
    angle = 30 * pi/180;  // 30 degree angle, moved to radians.
    
    speed_x = speed * cos(angle);
    speed_y = speed * sin(angle);  // now you have initial direction vector
    
    x_coord = 0;
    y_coord = 0;  // assuming quadrant 1 of traditional cartesian coordinate system
    
    time_step = 1.0/60.0;    // every frame...
    
    // at most 100 meters and while not below ground
    while (y_coord > 0 && x_coord < 100) {
    
       x_coord += speed_x * time_step;
       y_coord += speed_y * time_step;
    
       speed_y -= 9.81 * time_step;   // in one second the speed has changed 9.81m/s
    
       // Final stage: ball shape, mass and viscosity of air causes a counter force
       // that is proportional to the speed of the object. This is a funny part:
       // just multiply each speed component separately by a factor (< 1.0)
       // (You can calculate the actual factor by noticing that there is a limit for speed
       //  speed == (speed - 9.81 * time_step)*0.99, called _terminal velocity_
       // if you know or guesstimate that, you don't need to remember _rho_,
       // projected Area or any other terms for the counter force.
    
       speed_x *= 0.99; speed_y *=0.99;
    }
    

    Now you’ll have a time / position series, which start at 0,0 (you can calculate this with Excel or OpenOffice Calc)

    speed_x        speed_y       position_x     position_y    time 
    25,9807687475  14,9999885096 0              0             0 
    25,72096106    14,6881236245 0,4286826843   0,2448020604  1 / 60
    25,4637514494  14,3793773883 0,8530785418   0,4844583502  2 / 60
    25,2091139349  14,0737186144 1,2732304407   0,7190203271
    ...
    5,9296028059   -9,0687933774 33,0844238036  0,0565651137  147 / 60
    5,8703067779   -9,1399704437 33,1822622499 -0,0957677271  148 / 60
    

    From that sheet one can first estimate the distance of ball hitting ground and time.
    They are 33,08 meters and 2.45 seconds (or 148 frames). By continuing the simulation in excel, one also notices that the terminal velocity will be ~58 km/h, which is not much.

    Deciding that terminal velocity of 60 m/s or 216 km/h is suitable, a correct decay factor would be 0,9972824054451614.

    Now the only remaining task is to decide how long (in meters) the screen will be and multiply the pos_x, pos_y with correct scaling factor. If screen of 1024 pixels would be 32 meters, then each pixel would correspond to 3.125 centimeters. Depending on the application, one may wish to “improve” the reality and make the ball much larger.

    EDIT: Another thing is how to project this on 3D. I suggest you make the path generated by the former algorithm (or excel) as a visible object (consisting of line segments), which you will able to rotate & translate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from

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.