I have made a game where a ball goes in a random direction and a player(not you) has to go and "fetch" it. If you dont understand I this is what it is like below.
(FETCHER) ball (goes in random direction past fetcher)
instead of the fetcher just moving towards the ball which doesnt work unless the ball is going straight to the fetcher(the ball just bypasses them and the fetcher just runs after it), I want him to move sideways into the direction of the ball just like you would in real life in a cricket game or baseball etc.
thankyou so much if you can help me.
P.S. i am using flash 8 proffesional if that makes a difference.
Are you still looking for a solution? I've got one but the final answer is really nasty because it has to be solved symbolically (no numbers). I can take the time to post it if you like.
i am still looking for a solution and it would be great if you could post it.
here are two variables that i know(if it helps?).
speed of the ball
direction of ball
thankyou so much.
OK, I finally got something together. This is an interesting take on a pretty standard mechanics problem. You have two functions to worry about: the position of the ball b(t), the position of the fetcher f(t). We'll treat these positions as vectors.
You have the following knowns:
b(0) - initial position of the ball
f(0) - initial position of the fetcher
b' - "b prime", the velocity vector of the ball
|f'| - "length of f prime", the speed of the fetcher
You want to solve for f', the velocity vector of the fetcher. As an intermediate step we'll find tf, the time of collision.
We can define b(t) and f(t) in terms of their velocities (from high school physics: x = x0 + v*t):
b(t) = b(0) + b' * t
f(t) = f(0) + f' * t
We can write the end case as b(tf) = f(tf). In other words, their positions are equal at the time of collision.
Using this equality and some algebra, we can remove f(t) and b(t):
b(tf) = b(0) + b'*tf
f(tf) = f(0) + f'*tf
f' = b' + (b(0) - f(0))/tf
At this point, it would be easy to solve if we had numbers to plug in -- but we don't, and there are still two unknowns (f' and tf) so we'll take the length of both sides to turn f' into a known. I'm also going to use K instead of (b(0)-f(0)) because it makes the equations way shorter.
|f'| = |b' + K/tf|
Now square both sides to free up tf and solve for it.
|f'|^2 = |b' + K/tf|^2
|f'|^2 = |b'|^2 + 2b'.K/tf + |K|^2/tf^2
tf = (-b'.K +/-sqrt((b'.K)^2 - (|b'|^2 - |f'|^2)*|K|^2))/(|b'|^2 - |f'|^2)
+/- means "plus or minus"
. means "dot product"
tf is quadratic so it can have 0, 1, or 2 solutions. One thing to watch out for is negative solutions - those don't have any meaning since we aren't reversing time here. Once you've picked your favorite tf, plug it back in to find f'.
f' = b' + (b(0) - f(0))/tf
I tried a couple different ways to find a simpler solution, but I couldn't get anything. This one's pretty cool because it works in any dimensionality (1D, 2D, 3D, etc) but it sounds like you're working in 2D. I've attached a flash 8 file with all the code worked out.
Let me know if you're confused/interested in anything here.
ok, so ive had a look at the attached fla file but i am no proffesional programmer. lol. it would help (a lot, eventhough i admire your programming skills) if you could post the code that makes the fielder move towards the ball. I dont need any of the "if" statements like if the ball is being dragged or not because that just confuses me, a lot.
thankyou so much for all of your time and effort you have taken to help me.
Almost all of it makes the fielder move. If your project involves complicated controls like this, you really need to learn a bit about if statements and functions - or find somebody else to do the code for you.
Scripting really isn't that tough. If you post in the actionscrpit area someone will definitely help you out.