Sign in to follow this  

Problem with calculating speed based on direction of travel

This topic is 3777 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, I'm in the process of finalizing a sailing game and I need help with my algorithm to calculate a ships speed based on its direction of travel relative to the wind. As it stands my code works like this; If the ship is traveling with the wind, the ship will accelerate up to its maximum speed (based on the speed of the wind). If the ship is traveling perpendicular to the direction of the wind, then the ship will accelerate up to half of its maximum speed. If the ship is traveling into the wind, it goes nowhere. all of the angles in between are also covered, so basically the speed of you ship varies linearly between 100% and 0% based on the angles of the wind and the ship. This is all great for me except for the small fact that when you need to sail somewhere where the wind is not blowing, you can't... and the whole flow of game play grinds to a screeching halt. So, I need help figuring out how to mathematically keep the ship from grinding to a halt when it needs to sail into the wind. I know I could just program something like if(the ship's speed is less than something) speed = something other than zero; but that seems kind of crude to me, and it wouldn't look very smooth either to suddenly see you ship spring to life when you turn so far into the wind. If you would like to see my current code I can post it but I don't want to make this post unnecessarily long. thanks in advance, ---- Stevo

Share this post


Link to post
Share on other sites
I think it would help if your game was more based on reality.
Try making the ship slow down proportional to the speed of the wind.
That way when there is no wind there is no slow down.

Share this post


Link to post
Share on other sites
I'll elaborate a little bit more.

Here is how I would do it (roughly):

I'd have a 2d vector for the wind: windVec
I'd have a float representing the boats travelling speed: boatSpeed
and I'd obtain a 2d vector representing the angle of the boat (normalised): boatVec

To calculate how much the wind affects the speed:

vec2 boatSpeed = boatSpeed * windVec.normalise().dot(boatVec) + boatSpeed;

My maths may not be exactly correct, but I hope that helps.

Share this post


Link to post
Share on other sites
Well, you don't have to do anything if your ship has to sail into the wind. Don't have it so you magically get extra speed when sailing into the wind, just use the real world way of sailing into the wind. Zigzag!

The wind is coming from 0 degrees, you want to sail there, so you sail to +15 degrees for a bit, then turn to -15 for awhile. This way you can still sail by tacking and make decent speed.

Also the design of your ship is the deciding factor in how fast your ship can move. Some designs actually allow you to travel far faster than the wind is blowing.

Share this post


Link to post
Share on other sites
@stevenmarky:
Im trying to make my game as close to reality as possible but sometimes real sailing isn't fun enough, especially when you have to experience it through your computer screen. [smile] The main focus of my game isn't for the sailing to be entirely real, the wind aspect is there to add a feeling of authenticity to the game. Also, the math in my game is centered around angles, not vectors. Is this a problem or just a preference?

@Talroth:
I've tried this tactic but the problem is that sailing at 15 degrees to the wind, the ship crawls along and when you try to turn across the wind, you don't have enough momentum to get to the other side and you are stuck pointed directly into the wind, unable to move. [smile]

I guess what i really want to know is how I can make the ships speed smoothly bottom-out just before it gets to zero when the ship is facing into the wind or as it turns into the wind.

here's the code (MDX c#) if it helps,

float WindAngle = GameWeather.WindAngleAt(MeshPosition);

_Yaw = MathFunctions.MakeValidAngle(_Yaw);

float Distance1 = WindAngle - _Yaw;

float Distance2 = _Yaw - WindAngle;

if (Distance1 < 0)
{
Distance1 = ((2 * (float)Math.PI) - _Yaw) + WindAngle;
}

if (Distance2 < 0)
{
Distance2 = ((2 * (float)Math.PI) - WindAngle) + _Yaw;
}

maxspeed /= (float)(Math.Min(Distance1, Distance2) / Math.PI) * 50;

Share this post


Link to post
Share on other sites
Well, instead of setting the speed based on those calculations, what you should do, and what will look smoother and add your realism to the project is to accelerate / decelerate based on the angle to the wind. This was suggested above in the thread and is probably how you should do it...

Instead of setting your speed to the amount of wind and the angle, simply add an amount based on the wind and angle. Then set you min and max speeds based on a frictional force being the water drag. (So that the faster you are moving the more wind it takes to keep you moving...) This will make the boat seem faster when heading with the wind and slower when heading against it, all while using accelerations for smoother effects... Good luck!

Share this post


Link to post
Share on other sites
Quote:
Original post by blackbird04217
Instead of setting your speed to the amount of wind and the angle, simply add an amount based on the wind and angle. Then set you min and max speeds based on a frictional force being the water drag. (So that the faster you are moving the more wind it takes to keep you moving...) This will make the boat seem faster when heading with the wind and slower when heading against it, all while using accelerations for smoother effects...


At one point I tried adding to the ships speed based on the wind angle but got a weird bug where I would get constant acceleration if I sailed in circles.(it was the weirdest thing I have ever seen...) However I didn't have any calculations for friction like you suggested. (I would have never thought about friction otherwise) I will go implement it and see how it works.

As far as the smooth accelerations and decelerations, I have them already but they are handled in a different part of the code.

EDIT: I implemented the friction, as well as doing some refactoring and it works like I wanted... Its a wonder, when you follow proper physics it actually works... [rolleyes]

[Edited by - Stevo14 on August 11, 2007 2:34:58 PM]

Share this post


Link to post
Share on other sites

This topic is 3777 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this