• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

making a water current system

Status
Not open for further replies.
Level 14
Joined
Aug 30, 2004
Messages
909
I have a neat idea for a boat-combat level in my campaign. I want the player to control a single boat that is controlled by a custom movement system. In short, the boat moves in its facing direction but also moves in the direction the water current is going. The first is easy to do, but I worry that the second might be too inefficient.

What I need: a system that detects the boat's position, and then sets the water currents direction and strength. In short, I need 2 real variables that change depending on the unit's location on the map.

My plan: cover water area of my entire map with small regions, and whenever the boat enters that region I change the reals.

My questions:
1. Is having many regions inefficient? I will probably have over 100, each with its own trigger to detect entry.
2. Is there a limit to the number of regions you can have?
3. Would it be more efficient to use JASS to get the unit's x and y coordinates every few seconds, and then do a massive string of "if x > 120, x < 250, and y > -1350 and y < -1100; then set CurrentDirection to 250, and CurrentStrength to 5" type triggers? That seems worse to me, but I don't know. I could also use a more intelligent system that zeros in on the location, for example:
- if UnitX > 0 and Unit Y > 0, then run trigger "look in northeast quadrant regions"
- if UnitX > 0 and Unit Y <= 0, then run trigger "look in southeast quadrant regions"
- and so on...
4. Is there a better way that I can't see to set 2 real variables in response to a unit's location on the map?
 
Level 4
Joined
Mar 3, 2010
Messages
68
If I were making a system like this I would use parallel arrays and vector mathematics.

Index every boat in a unit array i.e. boat[1][2][3][4][5]

Have an x-position variable real array to store the x-coordinate of each boat (matching index)
Have a y-position variable real array to store of the y-coordinate of each boat

Have an x-velocity variable real array to store velocity vector of the boat's movement in the x-direction
Have a y-velocity variable real array to store the velocity vector of the boat's movement in the y-direction

Have an x-acceleration boat variable real array to store acceleration vector of the boat's sail force in the x-direction
Have a y-acceleration boat variable real array to store the acceleration vector of the boat's sail force in the y-direction
(use sine and cosine functions to determine how much acceleration the boat should have in the x and y axis based on its facing angle)

Have an x-acceleration current variable real array to store acceleration vector of the current's force in the x-direction
Have a y-acceleration current variable real array to store the acceleration vector of the current's force in the y-direction

your resultant acceleration in the x-direction of both would be x_current_acceleration + x_boat_acceleration
same for y axis

then every 0.03 seconds or so,
-set the boat's position to old_x_position_of_boat + x_velocity_of_boat
-set the boat's position to old_y_position_of_boat + y_velocity_of_boat
-change the boat's velocity to old_x_velocity_of_boat + x_acceleration_of_boat
-change the boat's velocity to old_y_velocity_of_boat + y_acceleration_of_boat
-recalculate the acceleration from the boat's sails in the x and y directions based on its facing angle
-do the same for current's acceleration if it isn't constant

Bingo, you have a working movement system.

I hope this makes sense. Understand that velocity is the rate of change of position over time, acceleration is the rate of change of velocity over time.
The wind on your boat's sails and the water currents are "forces." They cause the boat to accelerate (change its velocity).

Physics 101 in a nutshell.

You won't need regions to represent currents. Set the current acceleration throughout the map based on a function of the x and y coordinates.


If you want to expand this system you can factor in angle, angular velocity, angular acceleration, and the mass of the boat (if you have different types of boats)

I could probably code this rather quickly but I have no time today.
---------------------------------------

Using vector math as stated or similar to the above is THE most efficient and realistic way for anything large scale.
--best regards, an Electrical Engineer
 
Last edited:
Level 14
Joined
Aug 30, 2004
Messages
909
Wow, that was very helpful. I was thinking of using angles, but I've heard x,y vectors is superior. I've done something similar to what you've described in my Star Wars map. I think I will try your system though.

I feel bad pointing it out because your post was very helpful, but my real problem is about locating different currents. The current isn't uniform throughout the map. For example, imagine you start on a stream heading south on the map. The current is pushing you south at that point. Then it curves to the side and heads west. Now I need the current to push west. Then it loops up over and back south, etc. So different parts of the map will need to have very different effects on the ship. Should I just use the x/y coordinates (like point 3 in my original post) or can I use regions?

Thanks for the help (+rep if you didn't notice)
 
Level 4
Joined
Mar 3, 2010
Messages
68
If you use the modulus operator, you have different "regions" in terms of coordinates. i.e. a group of numbers from 0 to n-1. You can combine it with the random number function to make varying currents.

If I did x coordinate modulus 6, I would have something like this:

[0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5]

if i did the same with y I get a ton of blocks of numbers from 0 to 5

{ ----block---- }
[0][1][2][3][4][5] [0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5]
[1][2][3][4][5][0] [1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0]
[2][3][4][5][0][1] [2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1]
[3][4][5][0][1][2] [3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2]
[4][5][0][1][2][3] [4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3]
[5][0][1][2][3][4] [5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4]

[0][1][2][3][4][5] [0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5]
[1][2][3][4][5][0] [1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0]
[2][3][4][5][0][1] [2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1]
[3][4][5][0][1][2] [3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2]
[4][5][0][1][2][3] [4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3]
[5][0][1][2][3][4] [5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4][5][0][1][2][3][4]

This gives you a lot more control over how the currents could work than regions would. Also a lot faster.

You only need to calculate the acceleration at the point that the ship is at. Divide the coordinates by a number (in this case 6) to tell which block you are at (assuming you are simply satisfied with square areas). Once you have determined which block you are at, modulus can help you set individual accelerations for each point.

edit: fixed mistake, modulus operator also returns zero; its late XD

----------------------------
The above is one method.

What I was hinting at earlier was to use a single function or two to make something like this:

250px-VectorField.svg.png


or this:
electricfields.jpg


These are called vector fields
 
Last edited:
Level 14
Joined
Aug 30, 2004
Messages
909
That is an really interesting idea. Remind me to talk to you when I start my SC2 car map! You sound like you have some really creative solutions! I think that's a little too complex for what I"m looking for. I'm just making a mini-game in a campaign that I'd like to finish soon. I considered making a water map similar to my Star Wars map, so I might be back here if I decide to do that.

And thanks to you too Doom, I'll consider that. I might need dummy units for some other effects anyway.
 
Status
Not open for further replies.
Top