Making a sail, need help with the math calculations.

Alright so, after alot of time spent asking and slamming head towards the wall, all I got working is that my boat would eventually speed up and down in relation to where it was agaisnt the “wind direction”. No matter what I did, it would always go the way the wind was “blowing”.

What I want to achieve is this, (paint ftw):

http://lker.dk/boatpicture1.jpg

What is happening here, is that the wind blows from south, the boat is turning north and the sail is turned agaisnt the wind coming from south, so that the boat is going at full speed towards north.

http://lker.dk/boatpicture2.jpg

What has changed since last, is that the boat is now turning agaisnt west, but the sail is still turned agaisnt the wind that is coming from the south, meaning the boat will now go at full speed towards west.

http://lker.dk/boatpicture3.jpg

This is just to demonstrate that if the sail is perpendicular with the wind, it will not catch any wind, and therefore the boat will not move.

My problem is, how would I achieve this? Will I have to make more than one entity or what?

[lua]
function ENT:PhysicsSimulate( phys, deltatime )
if( nextsim < CurTime() ) then
nextsim = CurTime() + 0.02;
if( self.Entity:WaterLevel() == 0 ) then
if( nextthink < CurTime() ) then
nextthink = CurTime() + 1;
vector = GetGlobalVector( “WindVelocity” );
normal = vector:GetNormalized();
end
local phys = self.Entity:GetPhysicsObject()
if phys:IsValid() then
sail_normal = self.Entity:GetForward();
dot = normal:Dot( sail_normal );
phys:ApplyForceCenter( ( math.abs(dot) * ( mass * vector ) ) );
end
end
end

return SIM_LOCAL_ACCELERATION;

end
[/lua]
That’s my code for the sail entity as of yet, it kinda works, but I’ve explained what’s wrong earlier. That code may not be optimized or anything, but it is only for testing purposes.

Try peeking at the Fin Tool’s Lua. I’m 90% sure there are a few tricks you could use from there, as long as you give due credit to the creator :wink:

Mhm, I’ve had the same idea, sadly I don’t really understand much about what he’s doing with the vectors, and therefore can’t use it. Don’t know much about vectors, hence I’m asking here hehe.

You might want to understand that a boat moves forward because the wind hitting the sail will “try” to turn the boat windward, but the rudder is turned to try to point the boar leeward. The rest is like squeezing a bar of soap equally from both sides, it will pop out of your hand in a direction.
Don’t know if that will help, but you could try something like that instead of applying force based on the angle of the sail to the wind.

I know how a boat works in real life mate, I’m just asking for help here on how to achieve this in gmod.

I think you should make the sail turn-able, and make it so the “wind” pushed it straight forward, then just make the sail turn-able so boats would work smoothly.

Also, what model are you using for the sail? That could be affecting it :stuck_out_tongue:

That’s what I’ve already did… And model, I use different ones. Mostly the doors though. But they shouldn’t be affacting anything.

i had exactly this as a math problem in school, looks like basic vector stuff? I can’t remember how it worked -_-

I don’t know then, my Lua knowledge is too limited. Try making a separate addon that lets the wind change at random, so it will sort of make it even.

Already did that aswell.

And well, I’ve never had about vectors in math lessons yet. Hoping we get to be taught soon though. :slight_smile:

However nobody could help me out though ?

Bump. I still need help with this.