Game Programming Projectile shoot (angry birds like)

Posted 23 June 2014 - 11:30 AM

Hey, I'm developing an angry-birds like game. But it shoots a ball into a basket. I have a problem on the game logic. I do not know how the ball will be shut from the slingshot, to the basket on the right using projectile maybe? I don't really know.

Note that when I click my left mouse button, the ball will be shut to the right, and the basket should be moving on its x-axis while I try to shoot the ball into it.

theta is just the angle the slingshot is tilted, and you could just have the initial velocity be a percentage of how far back the sling is pulled
acceleration in the Y-equation is the force of gravity (whatever you want that to be). Note how there is no acceleration in the X-direction, I'm assuming that you don't want to bother with air resistance.

That's the general math out of the way, now for putting it into programming terms.
Since your x-directional velocity is always going to be constant, you can calculate it once (using the initial_v_x equation above). Now you have the ball's x-velocity in units per second. To actually draw this, you need it in units per frame, so you'd just divide that by your current FPS (or 60 as a standard, but using the current one smooths it out if you're way over that), and now you have the ball's units per frame movement value (deltaX). Every frame, you're going to add deltaX to the ball's current X coordinate, and there's your horizontal movement done. For the displacement in the Y-direction, it's even easier. You take the ball's current X-coordinate (not deltaX, the ball's actual position, assuming the slingshot is at x = 0) and plug it in as the time value in the Y-directional position equation. This gives you the exact y-coordinate of the ball at X distance from the slingshot.

That's pretty much basic projectile motion. As a side note: don't forget to add a check somewhere to make sure the ball is stopping when it either hits the ground (y = 0) or the bucket (you'll need collision for that), or it goes off the screen.