design 2D baseball physics?

  • Hey there — wondering if anyone has any thoughts on how best to code a really simple baseball.

    If you've played the old nes baseball games, there's always that view of the field after the batter hits a pitch. It flies in a single direction, collides with the walls, changes in height and speed, etc.

    I'm mostly trying to figure out the variables at play here. There's the horizontal direction of the ball, and its forward speed. But then there's also vertical direction and speed. How would you determine a pop-fly versus a line drive?

    I was trying to do this without the physics engine, to keep it authentic, but maybe that's a mistake.

    If anyone's got any insight, would love your thoughts!

  • You can do it by moving in 3D, which actually is pretty simple.

    First off, since all of the behaviors are in 2d, let me explain how to move with events. Which is also pretty simple.

    Give your sprite an instance variable “vx” for x velocity. And add this event:

    Every tick

    — sprite: set x to self.x + self.vx*dt

    Pretty simple. You can do the same for y and z if you add a z variable too. You can change the vx, vy, vz variables to change the speed.

    Now, to see the ball move in a 3D way we need some way to show the z. One idea is to have two objects: the ball and a shadow. The shadow would move in 2d and the ball would be z pixels above.

    So if shadow has all our varaiables we can do this:

    Every tick

    — shadow: set x to self.x + self.vx*dt

    — shadow: set y to self.y + self.vy*dt

    — shadow: set z to self.z + self.vz*dt

    Every tick

    — Ball: set position to (shadow.x, shadow.y+shadow.z)

    That takes care of the 3D positioning and the linear motion. We can make the ball move in a parabola like a real ball by adding this action:

    Shadow: add 300*dt to dz

    300 is the gravity and can be adjusted as needed. Next we can do a slew of other simple things.

    The launch? Set the v variables to point in the direction you want.

    Start of layout

    — shadow: set vx to 100*cos(30)

    — shadow: set vy to 100*sin(30)

    — shadow: set vz to -200

    100 is the ground speed.

    30 is the angle you want to hit the ball.

    -200 is the vertical speed. This is what would change a ground ball vs lobbing in the air.

    You could also look up spherical coordinates that can give you an xyz from two angles and a speed. I just don’t know them off the top of my head, it would be ideal though.

    Has ball landed?

    Z>0

    — set v variable to 0

    Or if you want it to bounce

    Set vz to -0.5*self.vz

    You can also make the xy speeds slow with

    Set vx to 0.5*self.vx

    Same for vy

    Wall bouncing can be done similarly.

    Anyways just some ideas.

  • This is incredible! Thanks so much for your insight, I can't wait to try all this out. Really appreciate it!

  • rojohound Thanks again for your help with this. I've had some time to experiment with it, and it's totally working out. If you have a moment, one question has come up so far:

    How do I use the formula to move the ball in every direction? Right now I can input from 0-90 degrees, but I think the sin/cos functions have a problem beyond that (though I'm no mathematician!)

    If you've got the time, any help is much appreciated!

  • Hello,

    1. You can use any angle with sin and cos and it will work.

    2. You can make the ball slow by setting the vx and vy to self.vx*0.5 and self.vy*0.5 in the bounce event.

    I’ll post a capx shortly. Ended up eating dinner so that delayed me.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Here's a capx:

    dropbox.com/s/8q7n27kfdc5n431/ball_3Dish.capx

    I did it slightly different than my description and I got carried away with some tangents, but it hopefully will give some ideas.

    Edit:

    What happened? I made a post, then posted again and it ate my previous post. This forum... oh well, it probably needs more bug testing.

    Anyways to answer your question sin and cos will work with any angle whatsoever.

  • So good! This is really helpful. It solved my problem, and I've learned a lot from this. Thanks very much!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)