0
\$\begingroup\$

I'm creating a simple game in python that will follow the same logic of DDTank and Worms. The graph is formed by characters. The buildings that will be destroyed are formed by '*', players are '@' and the projectile I still did not think the character. For now I did the part that draws the buildings with a random height.

But I was thinking the logic of launching projectiles. I learned in physics classes some formulas for oblique launch. So wanted to know if these formulas would fit in a computer program or I would have to invent another logic?

enter image description here

Formulas:

vx = v0 . cos ?
x = vx * t
v0y = v0 * sen ?
y = v0y * t + g * t² / 2
\$\endgroup\$
  • \$\begingroup\$ It's very nice that you showed us your game, but what does it have to do with the actual question? \$\endgroup\$ – Kromster says support Monica Oct 6 '14 at 20:13
  • \$\begingroup\$ @Krom Stern I showed it to help with answers. \$\endgroup\$ – user52558 Oct 6 '14 at 23:20
  • \$\begingroup\$ Why don't show a kitty as well, I'm sure it will help as well? ;) *I hope you get my point, the intro and the pic are irrelevant to the question and only distract from it. \$\endgroup\$ – Kromster says support Monica Oct 7 '14 at 4:09
2
\$\begingroup\$

Although you can use this type of equasions they are quite unhandy. These are derived from intigration where a fragment of time known as delta time or even dt limits to zero. They are most precise but usually unfit for physics engine due to the complexity of finding the time at which the object or projectile will collide. The complexity grows with more dynamic objects and once those dynamic object may appear at any time the system simply becomes unmaintainable. In other words I highly recommend you not to use them...

So what it is best to do is create a fixed delta time which is most commenly the duration of your frame or 1/FPS seconds.

The eqasions are then as easy as:

position = position + speed * dt 

speed = speed + force * dt

Then on each frame you would call for each projectile to move in which you would compute both, the new speed and the new position. After you have moved the projectiles you should check for crashes and handle them if any.

Note that all of those parameters I used are vectors except the dt. The gravity in your case would be a constant force vector of (0,G).

To add a bit on your specific case: if the user input would be an angle at which the projectile would be launched and its absolute speed aSpeed then all you needed to do in the begining is to determine the speed:

speedX = aSpeed * cos(angle) speedY = aSpeed * sin(angle)

Then if for instance your gravity acceleration is 10 (m/(s*s)) and your FPS is 10 you would in each frame call:

speedY = speedY - 10 * 1/10

positionX = positionX + speedX * 1/10

positionY = positionY + speedY * 1/10
\$\endgroup\$
  • \$\begingroup\$ But this would work in a character game, where there are only integer positions? And I would like to calculate the height in this case? \$\endgroup\$ – user52558 Oct 6 '14 at 18:11
  • \$\begingroup\$ No, this works great with floats, the smaller the dt, the better the performance. If done propperly you would not find the difference. \$\endgroup\$ – Matic Oblak Oct 6 '14 at 18:19
  • 1
    \$\begingroup\$ Actually if you are sceptical you may create a quick program which would implement both of the methods and both would output the result as X,Y and having an input of time. Note though that the method I described needs to be iterated with some constant dt, you can not simply insert the time. \$\endgroup\$ – Matic Oblak Oct 6 '14 at 18:22
  • \$\begingroup\$ Thanks! So what can I do to work with integer positions? Need not be very realistic move, because with characters it's hard to do something good. \$\endgroup\$ – user52558 Oct 6 '14 at 18:27
  • 1
    \$\begingroup\$ If the position needs to be snapped to an integer percision grid you should simply round the float values to integer values. This issue persists in both methods. If you for some reason need to work with integer values overall you may simply multiply all the values by some appropriate constant such as 100 and then divide the position with the same constant when applying a character position.. \$\endgroup\$ – Matic Oblak Oct 6 '14 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.