How should I go about doing this?

BlitzMax Forums/BlitzMax Programming/How should I go about doing this?

Arabia(Posted 2009) [#1]
Not sure if this should be in the beginners section or not.

I'm stuffing about writing a sports type game.

I've got the code right to make my player run from point1 (x1,y1) to point2 (x2, y2)

I've got the code right to make the ball go from point1 (x1,y1) to point2 (x2, y2) <--- yeah, same sort of code, but with simple bounce, ball slow down physics.

The code for each is in two functions:
BallTo (x1,y1,x2,y2,speed,angle)
PlayerTo (x1,y1,x2,y2)

Now this all works fine, but now I need to have both running simultaneously. i.e. as the ball heads in one direction, I need the player to be chasing after it.

Do I need to re-work everything into one single function?

Any suggestions?

Hope this isn't too stupid a question, still learning very very slow :)


Sokurah(Posted 2009) [#2]
Wouldn't you basically do this;

BallTo (BallX1, BallY1, BallX2, BallY2, speed, angle)
PlayerTo (PlayerX1, PlayerY1, BallX1, BallY1)

Assuming of course that the BallX1 & BallY1 vars are updated as the ball moves.

Although if I was doing it my code would probably look more like;

BallTo (angle, speed)
PlayerTo (BallX, BallY)


Digital Anime(Posted 2009) [#3]
You could do the following to calculate where the ball should be.

if you would take the players values and do x2-x1 and y2-y1 you'll end up with the distance on the x and y axis your player is gonna travel. And with those 2 values you can start calculate the offset of the ball.

Sokurah's idea is good as well, but it also depends a lot on how the player is controlled if you can use that.
Is the player controlled directional with just keyboard/joystick input or do you use the mouse to click somewhere on the screen to get the players x2 and y2 values?


Arabia(Posted 2009) [#4]
The players or the ball are not controlled as such - it's just a random thing for the ball, and the player then chases after it.

I suppose it's a bit hard to explain without me writing a novel on the game I am trying to re-write (improve on) - it's an old CPC464 cricket game called Test Match Cricket (written by CRL) <-- I use APE as an emulator and you can download the ROM image if anyone is interested in retro cricket games.

Okay - short explanation, hope this helps (but you will need to know cricket, sry don't know what country you are all from):

In the original game - the player would hit the ball, and it would go to it's final destination. The batters would then decide if they want to run or not.

If they decide to run, then they start running and the nearest fielder runs to where the ball is.

Now, I've got the functions written for both ball moving and player running, I just need both functions to run at the same time.

What I want is for the fielder (player) to start running after the ball while it is on it's way to the destination. I know how to do this, calculate the final location of the ball and make the player run to this position.

I hope that makes sense.

Basically, I've got two images moving (the ball and the player - will be four or more images later on) - do I need to include these all in one function - or should I be using Types (in which case I need to muck around more learning about them before I get too far down this path)

In all honestly, I probably should start with something a little more simple as a programming task, but oh well :)


Arabia(Posted 2009) [#5]
I'm beginning to think that to do this properly, the maths could get rather complex.

To have a player (running one direction at a fixed speed X) to cut off a ball going in anther direction (at a variable speed and sometimes in the air - i.e. no point cutting off the ball if it's 10m above their head) might be a little involved.

Hmmmm, I'll keep thinking about it.

If I can get it working (very basic version), I'll post my code, surely it will be helpful to someone :)


Muttley(Posted 2009) [#6]
Sounds like you need to add some pursuit behaviour to the player object.

http://www.red3d.com/cwr/steer/PursueEvade.html


Arabia(Posted 2009) [#7]
@muttley

Had a quick look at that....

Sounds like I need to go back to school to redo advanced mathematics to even being to understand that LOL

I'm far too old to be learning such things :)


FlameDuck(Posted 2009) [#8]
Warning! Unoptimized code follows: http://code.google.com/p/ascr/source/browse/trunk/csr/arise.mod/bline.mod/bline.bmx

You need to do something like this. It's basically a Bresenham line algorithm with a built-in iterator, so you can go through each point one at a time. This doesn't address your specific problem, but illustrates the technique, and you should be able to take it from there.


Fry Crayola(Posted 2009) [#9]
Regarding how you move the players and the ball simultaneously, you'll want to look into things like delta timing or fixed rate logic - there's plenty of information on both and more on the forums. The basic principle though is that you have a logic loop which will update the positions of the ball and player based on the amount of time that has passed since the last time you updated the display.


On the issue of the routines though:

You don't want your fielder to move to the ball's final location. What you want is for the fielder to move to the nearest point at which he can intercept the ball. Otherwise your guys aren't going to catch anyone out unless they get pretty darn lucky.

I had a similar problem some years back (I'm making a football game), so hopefully this will be of use to you.

In order to calculate the point of interception, I initially figured that I could use some simple enough ballistic mechanics to determine where the ball and player would meet. Unfortunately, it turned out to be a lot more complex than I had envisaged and I never did work it out.

Instead, what I did was I'd have the game work out where the ball was going to go ahead of time. I calculated its future position every 1/8 of a second, for a period of about ten seconds - more than enough time. I stored each of these locations in a TList together with the time it takes for the ball to get there. You can calculate its position using even smaller time steps for greater accuracy at the expense of speed, but it'll still be pretty fast. There's an added provision for height - I didn't store any position and time for when the ball was too high for a player to realistically reach. This will speed things up for the next step.

Now, each player would run at a set speed. So for each player in a given team, I'd run through the list of ball positions to find the earliest possible time, if any, that the player would reach the position before the ball. The time taken is equal to the distance to travel, divided by the velocity of the player. If this time is less than that stored for the ball to get there, then the player would arrive first. Once found, I stored that location for the player, as he'd need it later.

Once done for all players, I selected the player who would intercept it in the least amount of time, and told him to run towards the position where he'd intercept it.


_Skully(Posted 2009) [#10]
Your best bet is to give all moving objects vector based movement...

Basically, you take the angle between where the player is and where the player needs to be to intercept the ball based on its maximum speed and the current speed/direction of the ball.

Its one of those "where does a train A moving at x speed and a train B moving at y speed meet" type math questions you got in school, except that now you have directional factors in play (vector math)

After that you will need to add tactics in the form of AI but thats an entirely different animal.


Arabia(Posted 2009) [#11]
@Fry - Cheers, I'll look into Delta timing a bit now.

Thanks everyone, I think I've got a fair idea of how to do the maths (in my head anyway) now and the things that I need to take into account.

I'll see if I can work on it more today.

If I can work something out, I'll try and upload an executable and possibly some code. What is a good free site for hosting small files & pics?


Arabia(Posted 2009) [#12]
I've been messing about with this all day, and here's what I've come up with - I think it's interesting, maybe some others will to.

I'm still very new to BM, so I'm not using any OOP or even anything very advanced, so yes, this could be rewritten a lot better I'm assuming. I do think I've got the logic right, and maybe that will be more help to others than my lack of more advanced coding techniques :)

Controls are shown at the bottom of the window. Variables shown at the top.

Basically, run the program. You have a ball (red) on the left, and player (blue) on the right.

Click where you want the ball to travel to.

Now press "P" to generate the path that the player will need to run to to get the ball (if they can)

Press "R" to run the objects (the ball & player) at their respective speeds to see that they do infact meet at the same time.

You can modify the ball speed and player speed (roughly between 1 & 5) to see the difference it makes.

Got some more stuff I want to work on with this yet such as:
- Ball Height (player can't intercept the ball if it is in the air
- Have the ball slow down gradually as it runs on the ground.

Let me know if you are interested, and I'll post again when this is done

Comments/Suggestions if you find bugs/logic errors.

Enjoy :)