Link to home
Start Free TrialLog in
Avatar of jazjef
jazjef

asked on

How do I make flight of a ball and it's shadow on the ground converge simultaneously at a point on the ground?

I'm using VB6 and I have made a baseball game where a flyball at times will fly to the outfield. I also have a shadow for the ball. I need to make the arcing ball and the shadow that runs along the ground, converge at the point where the ball strikes the ground. I simply can't get this formula right. The shadow should simply travel the same velocity as the ball for the same amount of time, but without a vertical component. Pretty sure there's Cosine etc involved, but I just can't seem to find the right application of it. Can someone provide me with a resource or the detailed mathematical formula to solve this problem?

*I have incuded a drawing of the problem

Many thanks....

User generated image
Avatar of Aaron Tomosky
Aaron Tomosky
Flag of United States of America image

If the shadow traveled the same speed it would get there first because it's long a shorter distance.
So I'd think the velocity of the shadow would be (distance of shadow)/(distance of ball on arc)*(velocity of ball)

Butthis is just my best guess.
I do not have all your conditions so I cannot give an equation but I assume that you have the coordinates of the sun and an equation for the ground. Draw a ray from the sun ti the ball. Place the shadow at the intersection of that ray with the ground
Avatar of jazjef
jazjef

ASKER

Yeah, that's a great point aarontomosky....that's my problem; I'm assuming that because they arrive at the same point that they are traveling the same speed---not true at all. One is an arc and one is a straight line. I've been focusing on the 'arrival' of the objects, and not on the 'path' they have to travel.
When you do use the same velocity for the ball and the shadow, how far off is it?

That might help you working out what's missing from the formula.

Another thing you could look at is the length of the arc.
Not for points (My 2 cents on this LOL)

I have never worked on a project like this but from what I understand if we plot it on an XY axis then the shadow's Y will always be 0 and shadow's X will be the same as Ball's X so if we set the x axis same as the ball then you will have the shadow following the ball on x Axis?

I just amended the program which is available in VB.Net here.

and added the code

        gr.FillEllipse(br, _
            CSng(m_BulletX), 0, _
            CANNON_HGT, CANNON_HGT)
        br.Dispose()

Open in new window


for clearing the shadow

and this for creating the shadow

        gr.FillEllipse(Brushes.Black, _
             CSng(m_BulletX), 0, _
            CANNON_HGT, CANNON_HGT)

Open in new window

Assuming the shadow is directly beneath the ball, you just zero the height from ball position and that tells you where the shadow is.  Similar to SiddharthRout suggestion.
Thinking out of the box for a minute, I've been to many baseball games and I don't ever remember seeing the ball's shadow on the ground. I know it's probably there but being the same size of the ball, and gray, I'm sure it's pretty well invisible so why not just ignore it.
Avatar of jazjef

ASKER

The shadow provides the illusion of 3D depth in a 2-dimensional environment; in this case, the computer screen. At a real ballgame you have other perceptual cues available to your that enhance the 3D environment you are really in---e.g. ambient sound; the ability to actually move within the 3-dimensional space. These are not luxuries available within the 2D environment. The shadow is very helpful in the enhancement of the perceptual experience and adding simulated 'depth' to the game.
Good points. Game development is not something I've ever done except for board games. Is this of any interest? It talks about "sprite shadows".
The velocity of the ball has a horizontal component and a vertical component.  The vertical component changes due to gravity, so it starts off directed UP , reaches 0 the ball achieves its maximum height, and then increases as the ball descends to the ground.  The horizontal componet does not change throughout the flight of the ball.  The shadow of the ball across the ground travesl at the speed of the horizontal component of the ball.
If realism is desired, then the speed of the shadow across the ground will not remain constant unless the sun is directly overhead.

Imagine that the sun is low in the sky just above right-center-field in the diagram. As the ball is rising early in its arc, the shadow will be approximately at home plate, potentially moving to positions behind home plate at first, then moving forward towards the landing spot. Its apparent speed will increase as the ball begins to arc downwards. Eventually, it will speed up enough to reach the landing point at the same time as the ball.

Now imagine the opposite, where the sun is low in the sky behind and to the left of home plate. The shadow will appear to shoot across the field at first and then reach some distance where it will slow. If the ball is high enough, that farthest distance will be beyond the point where the ball will land.

An extreme example will be the shadow cast by a pop-up that goes straight up and down.

Part of the whole concept will be that the sun is so far away and that the distance traveled by the ball is so relatively short, that the rays from the sun can be considered to be parallel to each other along the entire line of flight.

Tom
So ideally you would draw a ray from the sun through the ball and where it intersects the ground draw the shadow.
But the origin of "a ray" will not be a point. That's where it gets tricky.

From any point on the ground, the angle up to the sun will always seem to be the same (within the limits of a rational flight of a baseball). It will be as if the sun moves around on a plane parallel to the ground. The angle of a line from the sun through the ball in flight and down the the ground will not seem to change during the flight of the ball.

You can almost think of the ball as a gyroscope. It will have an axis with north pointing at the sun and south pointing along the shadow to the ground. The angle of that imaginary axis will not change during flight.

The path of the shadow will usually not be a straight line traced on the ground.

 It should only be straight when the sun is directly overhead (and the ball flies straight).

Tom
I thought the definition of a ray is a point and a direction? Maybe I'm getting my terms confused. However you make a line from the sun to the ball and keep going until you hit the ground is how an actual shadow gets made. So I don't see why it wouldn't work for a game.
However if the "sun" is not real, then maybe just pick an arbitrary point up in the sky to be the sun and use that's the start of the line through the ball to the ground. Play around with that point until the shadow looks right. Probably directly overhead.
...the definition of a ray is a point and a direction?

It depends on how much realism is desired. The problem is that the "point" is going to be 93M miles away from the ground and the ball will only end up 300-400 feet maximum from its starting position and perhaps rarely get as high as 200-300 feet above ground. Further, the sun isn't exactly a "point" but rather a disc-shaped area, all of which emits light.

93M miles is some 490 billion feet. The relative magnitudes are such that rounding errors alone might be trouble. The distance is far enough that you can effectively assume that "rays" from the sun are parallel when they reach the Earth.

Now, it's true that the ideal would be actually to assume a true "ray" and calculate it all out every point along the way. But I suggest that any difference will be so small as not to be visible.

Tom
Blimey fellas, sledgehammers and nuts springs to mind.  The question doesn't even mention the direction of the sun.
Yeah, slight tangent there.

So if the path of the ball I the arc of a circle, you should be able to calculate the distance the ball travels.
http://en.m.wikipedia.org/wiki/Arc_(geometry)
Then find the distance the shadow travels (straight line between two poins).
Speed of the ball * distance shadow travels / distance ball travels = speed of shadow
It solving a ratio:
Ball speed/ bal distance = shadow speed / shadow distance
If (x(t),y(t),z(t)) is the position of the ball at time t, assuming z=0 is the level of the field. Then the shadow follows the path:

 ( x(t) - z(t)cos(theta), y(t) - z(t)cos(phi), 0)

where theta is the angle of the component of the Sun to x axis, and phi  to y axis. This path, by definition, will match the ball when it lands.
That formula looks reasonable to me. As long as theta and phi remain effectively constant during the flight, it seems right.

Tom
Blah, I am brain farting here, it is actually

( x(t) - z(t)/tan(theta), y(t) - z(t)/tan(phi), 0)

essentially anything of the form

( x(t) - A*z(t), y(t) - B*z(t), 0)

will have the effect you need for game purposes. A & B would in general be constants of any value.

eg    ( x(t) - 0.5*z(t), y(t) + 0.2*z(t), 0)
Avatar of jazjef

ASKER

There are actually a couple of solutions to this question... I want to comment a little before assigning points...

Solution1:
MartinLiss says the following:
"I've been to many baseball games and I don't ever remember seeing the ball's shadow on the ground. I know it's probably there but being the same size of the ball, and gray, I'm sure it's pretty well invisible so why not just ignore it."

MartinLiss really got me thinking about this. When I go to the ballpark, I usually can only afford upper deck seating---which is the same player view/perspective as my game (see drawing). One of the most interesting things about sitting in the upper deck is that nearly every ball that's hit---especially fly balls---looks spectacular and look like it's 'going out of the park'. The crowd will confirm this if you listen to their initial 'aaahhhhh' that fades to an 'oooohhhhhh' rather quickly. Other types of hits are rather hard to judge immediately as well. I myself could not recall ever noticing a shadow; and I bet it's there somewhere just like MartinLiss says; and the perceptual confusion that briefly results may indeed be due to perspective and lacking the ability to see a shadow that's actually there.

When I remove the shadow altogether from the game, it becomes clear that one can indeed still get enough information to determine what the ball is doing----but not without a moment or two of perceptual confusion!---it's just like being at the ballpark! My ultimate decision is to just 'ignore the shadow' as MartinLiss suggested.

Solution2:
This solution addresses the mathematical nature of the real problem of adding a shadow.

I''m giving points in this area for formulas that I tested that helped me actually add a shadow accurately. I was able to use GwynforWeb's post [Posted on 2012-02-21 at 04:22:29ID: 37622231] to get the necessary math in order to make a shadow. The weird thing was that turning the shadow off and on quite a few times, and playing test games under the different conditions, led me to believe that the actual experience of a ballpark would be best represented by actually allowing for the 'perceptual confusion' I mentioned earlier to occur as part of the gaming environment.

Thanks to everyone who 'weighed in' on the problem. I'll distribute points based upon my post here and how the contributions fed into my solution....
Another bit of weighing in then.  Computer displays are not reality, there are many well understood differences between how people perceive things on a monitor vs. reality.  Some of them are less obvious than others.

It may be that seeing a shadow affects the players ability to play the game.  A hitter can time the swing of his bat because he has a good point of view, depth perception and practice.  A less skilled batter will often get it wrong.  Does that matter to the player?
Avatar of jazjef

ASKER

There's no swinging of the bat for the game. After a button click there is a roll of the dice and the database provides a statistical result. The ball action will mirror the statistical result. So, if it's a double----then the program will randomly select a location, flight type (line drive, fly ball etc) and then a location such as left-center field. So, it's just another statistical baseball game that happens to have far more realistic ball animation than most.
Avatar of jazjef

ASKER

I've requested that this question be deleted for the following reason:

The last post on the question was by me on 03/24/2012...17 months ago. This time I have a sample VB program to offer up that can assist others in understanding my problem. I've never solved this problem; but I want to try again before I die. :) Thanks.
This question appears to have been answered. A number of people suggested solutions, some complex, some simple, The author claims to have used GwynforWeb's post #37622231 and got a suitable result. I would suggest the answer goes to GwynforWeb, possibly give a few points to other contributors. If the author wishes to reframe the problem he can always start another question.
ASKER CERTIFIED SOLUTION
Avatar of DropZone
DropZone
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
satsumo is completely correct. The author said the question was answered. The reason that there was not a response for 17 months was because the author did not distribute the points as promised.
Avatar of jazjef

ASKER

Here you go DropZone.....

VB6 sample program is attached...

I wanted to create a new question, but if you think that you can get it all calibrated, then a new question won't be necessary. "Showing me" code solutions has not worked for me, but I'll try yours and see if I can get it to work. I abandoned the project about 15 months ago----to let my hair grow back after pulling it out. :)

Description:
This is a 'baseball game' where a click of a button 'launches' a baseball (projectile) and the ball hits a target placed somewhere in the stands (home run) or on the field of play. The target is draggable on-screen, and can be placed at any position on the field. In addition, the shadow of the ball needs to behave naturally as if there were a lighting source directly above the stadium. This would be similar to many baseball video games that have a similar design. I already have source code and a program up and running that is about 60% accurate---but the mathematics are not entirely accurate and the program needs to be tweaked/calibrated so it is accurate and consistent. This will be used experimentally in a lab where participants will view the flight of the ball on different stadium backgrounds with different architectural etc characteristics.

Specifics:
1) I would like the ball to launch and hit the target called the 'DX' marker every time with 100% accuracy
2) I would like the shadow of the ball to follow the ball exactly each time, and always converge exactly with the ball's landing point with 100% accuracy
3) I would like #1 and #2 above to occur no matter where on the field or in the stands the 'DX' marker is located

Many thanks in advance for the assistance.
Baseball-Projectile-Project-v012.zip
Avatar of jazjef

ASKER

I tried to post a NEW question, but EE would not let me and deleted it.

EE does not care if I said my problem was 'solved' as per this questions..... they will not let me post a similar question until I give points for this one, or get a 'real' solution to my problem within this thread.

I am open to comments as to what members would like me to do.... thanks.
In comment #37760892, you mention that the solution given by GwynforWeb was producing the answer correctly. How is the problem not solved in that case?
Avatar of jazjef

ASKER

@ satsumo:

I made the post before testing thoroughly. I made a mistake by indicating it was accurate----I hope that's adequate for an explanation. I then abandoned the project afterwards due to frustration. Simple enough? I can't do more than say I was wrong.
Avatar of jazjef

ASKER

Note to all:  
I have posted code with details of what I want it to do. I have also admitted that my previous indication in March of 2012 that the problem was solved was incorrect due to my not testing thoroughly with the provided solution I said was "correct". In addition, I abandoned the question improperly----I should not have done that.

*** At this point, having apologized and have posted the source code of the program with the issue----I feel I have made a reasonable attempt to move this question forward. I am kindly asking for direct assistance with the code that has been posted. The program is posted; if you can help by analyzing the code, then that help is greatly appreciated; if  you cannot, then there's little reason to post to this thread ***
@jazjef

Please don't take offence. If you appeared to have an answer, then it makes no sense for other people to contuine responding. If there was something wrong with the answer, its useful to know what the problem is so that the answer can avoid that.

Anyway, back to the question. I can give all the math required for this, its pretty standard 3D vector math and projecting onto an axis.  However, its a long time since I wrote VB. If I describe the solution, are you willing to implement it?

First question. I assume you have some sort of projection from 3D to 2D? So that you can draw the ball and the shadow? Is that correct?

Do you want me to describe the ball moving code or just the shadow?
Consider that the shadow cast by the ball travels on the ground, which is flat and perpendicular to the elevation of the ball.

Whatever the "correct" (awarded) answer turns out to be, it needs to include the fact that the quote above is simply wrong in most cases. The shadow will follow a 'perpendicular' path only if the ball travels directly towards the light source. ('Directly' along the 2D plane as seen from above.)

But I partially agree with earlier thoughts of an imperceptible shadow. A visible shadow would be a great addition, but perhaps simply unneeded.

Tom
Tliotta,

I didn't get the sense from the original post (and from some of the poster's replies) that this was a hyper-realistic simulation with ray tracing.  Look at the sample diagram, forchrissake.

He wanted a ball to follow an arc, and the shadow to follow the ball in a straight path that converges at the landing point.  All of you over complicated the problem, confused the poster, and caused this question to languish in limbo.

I don't want the points, as I said in my post.  As a matter of fact, I was expecting the question to be closed.  I only wanted to offer a simple solution to the more simple problem that the original question suggested.

All this question needed was a single-axis vector rotated on an angle.  That's it! Simple.  He's mentioned time and time again that he's not simulating the sun, and I don't think he cares about an arbitrary light source.  I provided the maths for this, which solves his problem.

He can go implement it if he wishes, and close the question up as abandoned, or give the points to someone else.

Cheers.
    dZ.
Apologies, dZ. That just didn't make sense to me. The diagram seemed just an example for discussion. I couldn't see how someone could successfully program the curve of the flight of the ball from a point A to a point B over the projected playing field, yet was unable to program a straight line path from the same points A and B. Since the first is more difficult, it just didn't make sense that the second wouldn't be relatively trivial.

But if that is indeed what is desired, I can't argue otherwise. The OP knows what's needed as a result.

Tom
Avatar of jazjef

ASKER

***** The VB6 code is posted ****

Either someone can add the shadow or they can't. I myself cannot get it to work. Simply posting the equations and stating it's 'simple to do'----WITHOUT DOING IT IN THE CODE---- does nothing to solve the problem. I need the solution "implemented in the code"..... I'm not sure how to say that any clearer or in a way that's more simple. This is not my projectile code. It's code I modified to a degree to get it where it is at right now.... I did make the stadium graphic though....
I have no dog in this fight, but ...

In your original question you asked
     Can someone provide me with a resource or the detailed mathematical formula ...

You certainly got help along those lines.  The simplest solution (suggested by several people) was to assume sun overhead and plot the ball (calculating the x,y,z coordinates) and the shadow (same x,y coordinates with z=0).

Close that question, awarding points to the people who tried to help.  You should still be able to do that without waiting for a moderator.

Then you should be able to start a new question about the programming.
Sorry I was away for a few weeks and I missed the aftermath of this saga.

To Expert tliotta (Comment ID: 39521196), it's not that he couldn't plot a straight line, is that the velocity of the shadow needed to follow that of the ball's ballistic trajectory, and converge at the target landing spot--that's the trick:  you're not drawing a line, you are animating the movement of both the ball and its shadow to match a somewhat simplistic simulation.

Anyway, it seems my answer was chosen as correct and I was given the points.  I appreciate that, but I really wasn't looking for it.  I hope the poster was able to get his program to work, though.

      -dZ.
@DropZone:

you're not drawing a line, you are animating the movement of both the ball and its shadow to match a somewhat simplistic simulation.

Understood, but it seems to miss my point. The path of the ball is apparently a projection of (X,Y,Z) coordinates onto a 2D surface at each point in time. The path of the shadow should be little more than a similar projection of (X,Y,0) coordinates at the same points in time onto the same 2D surface. As long as it's a simplified simulation, it's not clear why one is possible and the other is not... unless it's not as simplified a simulation as thought.

But as long as the Q is closed, it's moot anyway.

Tom
Avatar of jazjef

ASKER

dorpzone and tliotta;

Returning to a closed question to further discuss the problem tells me a lot about your dedication to helping people like me solve problems. I'd give you each 500 on this if I could. Thanks guys....