New User Scripting Question for Baseball Game.

Started by Drakmyre, Sun 03/08/2014 11:48:27

Previous topic - Next topic

Drakmyre

Hey Everyone,

     This is my first post so here goes...

     How would you script a standard pitch and hit?

Khris

That's pretty vague, do you mean in terms of a ball flying in 3D space?

Drakmyre

Sorry for the slow response.

2-D space. Hits and then reacts like it was hit by a bat.

Khris

The basic idea is to have a position and a movement vector.

Code: ags
// in repeatedly_execute

  if (ball_is_flying) {
    ball_movement_y += 9.81 / IntToFloat(GetGameSpeed()); // gravity
    ball_position_x += ball_movement_x;
    ball_position_y += ball_movement_y;
    // ground
    if (ball_position_y > 195) {
      ball_position_y = 195;
      ball_movement_y = -0.5 * ball_movement_y; // reflect and dampen
    }
    oBall.X = ball_position_x - 5; // 10 pixel sprite
    oBall.Y = ball_position_y + 5;
  }


When the ball is pitched, set its position and initial movement and ball_is_flying to true and you'll get a nice arc.

Crimson Wizard

I know I am nitpicking, but Drakmyre should also define whether it is side-view or top-down 2d space ;).

selmiak

chances are 50% he doesn't have to anymore ;)

Calin Leafshade

Also nitpicking but that would mean that 1px = 1m since you define gravity as 9.81px/m^2

This is an unlikely scale for any game.... except maybe gorillas.bas

Eric

Off to play Gorillas for five hours. Thanks a lot, Calin.

Khris

Sorry, I also forgot air resistance and the weak nuclear force.

Snarky

I'm sort of sorry Khris gave a helpful (well, a 50% likely helpful) answer to this question, since the thread is a prime example of how NOT to ask a question on the forum:
  • Thread title completely useless. No indication of WHAT the question is about, just that you have one.
  • The question itself is impossibly vague:

    • What is a "standard pitch and hit"? Are you talking about baseball, or what? Most people around here aren't American, you know.
    • What are you trying to make? Is this going to be an interactive game, an animation (perhaps dynamically rendered with random variation), something else?
    • Which aspects do you want to simulate? How realistic do you want it? 2D (front-view, side-view, top-down-view...?) or 3D?
    • Are there factors you need to be able to adjust? (Speed, for example?)
    • If it's interactive, how is that supposed to work? Do you play as pitcher or hitter? Real-time or some sort of turn-based? What sort of input do players get?
    • Do you need to determine the result at all (hit/miss, distance, where the ball lands, I don't know)?
Without details like that, we're pretty much left to guess at what you want. You need to describe what the experience should be like as a player, or you're really just asking us to design your game for you. You might as well ask "How do I script driving a car?" or "How do I script a fight?" or "How do I script rescuing a princess?" Give a specific, detailed description of what you're trying to achieve, and which bits you're not sure about how to code, and we might be able to help.

Please read forum rules and all that. Apart from that, a grumpy welcome to the forum!

Drakmyre

#10
Snarky,

     I don't know how to ask a question. Since I am new I don't know how to phrase a question.

But to answer your questions:
    What is a "standard pitch and hit"? Are you talking about baseball, or what? Most people around here aren't American, you know. Baseball. Now I know about the International makeup of the forum.
    What are you trying to make? Is this going to be an interactive game, an animation (perhaps dynamically rendered with random variation), something else? Just a top down Baseball game.
    Which aspects do you want to simulate? How realistic do you want it? 2D (front-view, side-view, top-down-view...?) or 3D? Top-Down. Hit and depending on where it goes you either score or get an out. Three outs and it's game-over or next turn if you have two players.
    Are there factors you need to be able to adjust? (Speed, for example?) Multiple speed levels would be nice but for now one will do.
    If it's interactive, how is that supposed to work? Do you play as pitcher or hitter? Real-time or some sort of turn-based? What sort of input do players get? Pitcher would be automatic in that he(it) would only have one speed. Players just guess when is the best time to swing.
    Do you need to determine the result at all (hit/miss, distance, where the ball lands, I don't know)? All of them. The only thing to add to "where the ball lands" is whether it is in(scores) or out(does not).

Edited by mod: Don't double post within a short period of time. You can edit your posts.

Khris

Quote from: Drakmyre on Thu 21/08/2014 10:52:24Snarky,

     I don't know how to ask a question. Since I am new I don't know how to phrase a question.
(Can't tell if sarcastic or not...)
AGS is a 2D adventure game engine; there's obviously going to be more than one way to implement a standard pitch and hit (which I googled to make sure it was a baseball term when I read the question). It just felt like you could've put a leeeetle more effort into the question, you know? :) Alright then.

So basically a ball comes flying from the top of the screen and the player takes control of the hitter and is supposed to swing at the right time. How exactly is this going to work? IRL the flight path of the ball is determined by the angle of the bat when it connects and the point on the lateral circumference where it connects, right? The former can be determined by the timing, should the latter be randomized? What about the force?

There are lots of possibilities here; one could use the standard mechanic of an indicator quickly moving back and forth along a colored bar, or even a mouse gesture. Do you know of a similar game we could use as reference?

(Also: it's not too late to edit your first post and change the thread title into something meaningful.)

Slasher

#12
Hi,

I know naff all about the game.

Here goes:

You may also wish to consider the angle of the bat and the power that you hit the ball: these will determine where the ball goes to and how (straight or Khris's ball curving maths). These can easily be implemented with a power bar/slider and an angle bar/slider on a gui along with an if collide script (ball touches bat for example).

Food for thought.



Drakmyre

Khris,

I wasn't being sarcastic. Since I am new I don't know how to phrase a question other than "how can I do this?"

As for a reference the only things I can think of are:
Baseball video games-Basically all of the work on the premise that when the ball is "close enough" you swing. Depending on how early or late you swing it hits a different part of the bat and that, along with the pitch and other variables, determines where it goes. Since I'd like to keep it to a minimum it is basically going to be when in the swing it hits. For a Right-handed player, the earlier you hit the more likely it will go right. MLB2K14 and Extra Innings on SNES are two similar, but differing in key parts, swing mechanics.

Golf games always have interesting but basic swing mechanics. Hot Shots Golf has one where depending on how close to the markings you press the button determines how well you hit something.

As for force, one could keep it basic and keep it constant and just have the angle of the bat determine where it goes. Or you could use the aforementioned Golf Mechanic where one determines contact and the other determines how hard you swing. But I would prefer to keep it as basic as possible for now.

As for what Slasher suggested: any of the GUIs mentioned would work.

NickyNyce

Actually, for a right handed hitter, the earlier you swing you would pull the ball and it would go to left field, not to the right. Swing late and the ball would go to right field. A perfectly timed swing would be up the middle.

Drakmyre

It depends on the perspective. It's late in the swing when you connect but an earlier swing relative to the ball being thrown.

Snarky

Quote from: Drakmyre on Thu 21/08/2014 10:52:24
     I don't know how to ask a question. Since I am new I don't know how to phrase a question.
Quote from: Drakmyre on Fri 22/08/2014 11:27:16
I wasn't being sarcastic. Since I am new I don't know how to phrase a question other than "how can I do this?"

It's not a really a forum-specific skill, though. Realizing that you need to explain your problem and be specific in what you ask for is a matter of common sense. Five-year-olds might ask a policeman for the way to "grandma's house", but (to multiply the analogy) reasonable adults don't go into a restaurant for the first time and ask for "my favorite", or file support tickets asking only "why isn't my computer working?"

Anyway, I think there are two ways to go with what you describe. You can do a fairly realistic simulation, or you could do Pong.

Realistic
If you want to model force, height (determines apparent speed), distance and angle, the most straightforward thing is to just do the ballistics vector calculation as the ball is flying through the air. This is basically as Khris proposes, except in three dimensions (and I would uncouple the calculations from the particular coordinates, do everything in floating point and then convert to screen coordinates at the end; that reduces the rounding error and makes the whole thing seem smoother). This also lets you show it from any direction, whether top-down or side-on or even in 3D if you want.

Essentially, the ball has a direction of motion, represented as how far it moves in the x, y and z directions every game cycle (call this the motion vector). Then there are forces acting on the ball, mainly gravity and friction (air resistance), which affect the motion vector each cycle. Gravity is a constant pull down (you add/subtract a constant value to the vertical component of the vector, whether that's Y or Z), while friction is proportional to speed (you multiply each component by a friction coefficient < 1). You can play with the gravity and friction values to give the right feel. If you want to get really fancy you can also add in the effect of spin, but I'd leave it out for now.

When the ball lands, you have to decide what should happen. If you want it to bounce, Khris has provided a simple version of that (though you'll want to set some minimum values so it stops bouncing at some point). To determine the outcome, you'll probably divide the playing field into different zones and just check which one the ball ends up in. (Obviously if you were going to have outfielders or whatever they're called chasing the ball, things would get a lot more complicated).

Now the only tricky thing left to do is how to calculate the effect of the hit with the bat, to set the initial motion vector of the ball. Basically, you want the force (how fast it's going), the horizontal angle (the direction it's heading), and the vertical angle (how high it goes) â€" this is what's called polar coordinates in 2D and spherical coordinates in 3D. From those three it's a matter of simple trigonometry to convert to the x,y,z of the motion vector.

Horizontal angle pretty much depends on the angle of the bat, which in turn is determined in this simulation by the timing of the hit (if the earliest possible hit is at t_min and the latest at t_max, and the possible angles go from theta_min to theta_max, you can probably assume the bat rotates uniformly and simply scale from t to theta). I haven't played the games you reference, so I don't really know how those determine force and vertical angle, but basically you take some sort of input value (or a random value) and scale it to the right range. Or just keep them constant. Not much to it.

Pong
With "Pong", you ignore all the physics, you work only in 2D, and the ball simply moves in a straight line with fixed speed for a certain distance (or until it reaches some barrier). Use a 2D motion vector to move the ball, and either keep track of the distance/number of cycles moved or check whether it's reached the "edge" to stop it...

So, that's how I would "script a standard hit and pitch". Of course, there are dozens of other things you need to do to have a working game: displaying it, taking user input (and the UI for that), transitioning between the different states of play, keeping score, etc., but that's the basics of it.

Drakmyre


SMF spam blocked by CleanTalk