Jump to content

Script to make boat sail


DanceTweety
 Share

Recommended Posts

Hi all,

 

I trying to make a script to make a boat sail.

 

This script works. But to steer it I want to put the script in engine, and put it at the back of the boat(box2 is test box I us to simulate engine) and use a hinge to steer it with y axis.

 

Problem is the hinge dont seem to work in this script I made:

 

Script.enabled = true --bool "Enabled"

Script.gravity = false --bool "Gravity"

Script.movespeed = 10

Script.originPosition = nil

Script.currentPosition = nil

Script.oldMass = nil

Script.window = Window:GetCurrent()

Script.move = Vec3(0,0,0)

Script.Parent = boat

 

function Script:Start()

self.originPosition = self.entity:GetPosition()

 

self.oldMass = self.entity:GetMass()

 

if self.entity:GetShadowMode()>0 then

self.entity:SetShadowMode(2)--Light.Dynamic

end

 

if self.enabled then

if self.entity:GetMass() == 0 then

Debug:Error("Entity mass must be greater than zero.")

end

else

self.entity:SetMass(0)

end

 

self.entity:SetGravityMode(self.gravity)

self.entity:SetFriction(0,1)

self.move = Vec3(0,0,0)

 

self.parent = Model:Box(self.Parent)

self.child = Model:Box(self.entity)

self.joint = Joint:Hinge(0,10,0, 0,0,1, self.child, self.parent)

self.joint:SetLimits(-45,45)

self.joint:EnableLimits()

 

 

 

end

 

 

 

function Script:UpdatePhysics()

 

self.move = Vec3(0,0,0)

local movespeed = self.movespeed

 

if (self.window:KeyDown(Key.Down)) then self.move.x= movespeed * Time:GetSpeed() end

if (self.window:KeyDown(Key.Up)) then self.move.x= -1 * movespeed * Time:GetSpeed() end

if (self.window:KeyDown(Key.Right)) then self.move.y= movespeed * Time:GetSpeed() end

if (self.window:KeyDown(Key.Left)) then self.move.y= -1 * movespeed * Time:GetSpeed() end

 

 

self.entity:AddForce(self.move.x,0,self.move.z)

self.entity:AddTorque(0,self.move.y,0)

 

 

Time:Update()

end

 

 

Does someone have some tips?

Link to comment
Share on other sites

  • 3 weeks later...

Hi All,

 

I made changes to the script but it still does not make a normale circle when I steer it to left or right

 

And help is welcome. I test it with a box. What I try to do is get the boot to sail in direction of the Y angle:

 

Script.enabled = true --bool "Enabled"

Script.gravity = false --bool "Gravity"

Script.movespeed = 5 --Define self.movespeed

Script.originPosition = nil --Define self.originPosition

Script.currentPosition = nil --Define self.currentPosition

Script.oldMass = nil --Define self.oldMass

Script.window = Window:GetCurrent() --Define self.window

Script.move = Vec3(0,0,0) --Define self.move

Script.rotation = "" --Define self.rotation

Script.ry = 0 --Define self.ry

Script.speed = "" --Define self.speed

 

function Script:Start()

self.originPosition = self.entity:GetPosition() --Get position of object

 

self.oldMass = self.entity:GetMass() --Get mass of object

 

if self.entity:GetShadowMode()>0 then

self.entity:SetShadowMode(2)--Light.Dynamic

end

 

if self.enabled then

if self.entity:GetMass() == 0 then --Get mass and check if bigger then 0

Debug:Error("Entity mass must be greater than zero.")

end

else

self.entity:SetMass(0)

end

 

 

self.entity:SetGravityMode(self.gravity) --Set gravity to false

self.entity:SetFriction(0,1) --Set Friction

self.move = Vec3(0,0,0) --Set self.move to vector

 

end

 

function Script:UpdatePhysics() --Loop to update Physics

 

self.move = Vec3(0,0,0)

local movespeed = self.movespeed --Set movespeed to self.movespeed

self.rotation = self.entity:GetRotation():ToString() --Set self.rotation and make it a string (else it is userdata and will not work)

self.ry = string.sub(self.rotation,11,15) --Set self.ry with y of rotation (if cuts of x and z)

self.ry = tonumber(self.ry) --Set self.ry to be a number

self.ry = math.ceil(self.ry) --Set self.ry to be a round number

 

if (self.window:KeyDown(Key.Up)) then self.move.x = -1 * movespeed * math.cos(self.ry) * Time:GetSpeed() end --Look if upper arrow key is pressed and Set value in selfmove.x (x-axis)

if (self.window:KeyDown(Key.Up)) then self.move.z = -1 * movespeed * math.sin(self.ry) * Time:GetSpeed() end --Look if upper arrow key is pressed and Set value in selfmove.z (z-axis)

if (self.window:KeyDown(Key.Down)) then self.move.x = movespeed * math.cos(self.ry) * Time:GetSpeed() end --Look if lower arrow key is pressed and Set value in selfmove.x (x-axis)

if (self.window:KeyDown(Key.Down)) then self.move.z = movespeed * math.sin(self.ry) * Time:GetSpeed() end --Look if lower arrow key is pressed and Set value in selfmove.z (z-axis)

if (self.window:KeyDown(Key.Right)) then self.move.y= movespeed * Time:GetSpeed() end --Look if right arrow key is pressed and Set value in selfmove.y (y-axis rotation)

if (self.window:KeyDown(Key.Left)) then self.move.y= -1 * movespeed * Time:GetSpeed() end --Look if left arrow key is pressed and Set value in selfmove.y (y-axis rotation)

 

self.entity:AddForce(self.move.x,0,0) --AddForce with the self.move.x value on x-axis

self.entity:AddForce(0,0,self.move.z) --AddForce with the self.move.z value on z-axis

self.entity:AddTorque(0,self.move.y*50,0) --AddTorque with the self.move.y value on y-axis (rotation)

 

Time:Update() --Update Time value

end

 

 

function Script:PostRender() --Function to print on game screen

local context=Context:GetCurrent()

context:SetBlendMode(Blend.Alpha) --Set blend to alpha so only letters will show

context:DrawText("Rotation :".. self.ry .."",5,130) --Set text and position of text

--context:DrawText("Speed :".. self.speed .."",5,140) --Set text and position of text

end

Link to comment
Share on other sites

Hi All,

 

I made changes to the script but it still does not make a normale circle when I steer it to left or right

 

And help is welcome. I test it with a box. What I try to do is get the boot to sail in direction of the Y angle:

 

Script.enabled = true   --bool "Enabled"
Script.gravity = false  --bool "Gravity"
Script.movespeed  = 5   --Define self.movespeed
Script.originPosition = nil --Define self.originPosition
Script.currentPosition = nil  --Define self.currentPosition
Script.oldMass = nil  --Define self.oldMass
Script.window = Window:GetCurrent() --Define self.window
Script.move = Vec3(0,0,0) --Define self.move
Script.rotation = ""  --Define self.rotation
Script.ry = 0   --Define self.ry
Script.speed = ""  --Define self.speed
function Script:Start()
self.originPosition = self.entity:GetPosition() --Get position of object
self.oldMass = self.entity:GetMass() --Get mass of object
if self.entity:GetShadowMode()>0 then
 self.entity:SetShadowMode(2)--Light.Dynamic
end

if self.enabled then
 if self.entity:GetMass() == 0 then --Get mass and check if bigger then 0
  Debug:Error("Entity mass must be greater than zero.")
 end
else
 self.entity:SetMass(0)
end
self.entity:SetGravityMode(self.gravity) --Set gravity to false
self.entity:SetFriction(0,1)   --Set Friction
self.move = Vec3(0,0,0)    --Set self.move to vector
end
function Script:UpdatePhysics() --Loop to update Physics
self.move = Vec3(0,0,0)
 local movespeed = self.movespeed  --Set movespeed to self.movespeed
 self.rotation = self.entity:GetRotation():ToString() --Set self.rotation and make it a string (else it is userdata and will not work)
 self.ry = string.sub(self.rotation,11,15) --Set self.ry with y of rotation (if cuts of x and z)
 self.ry = tonumber(self.ry)   --Set self.ry to be a number
 self.ry = math.ceil(self.ry)   --Set self.ry to be a round number

 if (self.window:KeyDown(Key.Up)) then self.move.x = -1 * movespeed * math.cos(self.ry) * Time:GetSpeed() end --Look if upper arrow key is pressed and Set value in selfmove.x (x-axis)
 if (self.window:KeyDown(Key.Up)) then self.move.z = -1 * movespeed * math.sin(self.ry) * Time:GetSpeed() end --Look if upper arrow key is pressed and Set value in selfmove.z (z-axis)
 if (self.window:KeyDown(Key.Down)) then self.move.x = movespeed * math.cos(self.ry) * Time:GetSpeed() end    --Look if lower arrow key is pressed and Set value in selfmove.x (x-axis)
 if (self.window:KeyDown(Key.Down)) then self.move.z = movespeed * math.sin(self.ry) * Time:GetSpeed() end    --Look if lower arrow key is pressed and Set value in selfmove.z (z-axis)
 if (self.window:KeyDown(Key.Right)) then self.move.y= movespeed * Time:GetSpeed() end  --Look if right arrow key is pressed and Set value in selfmove.y (y-axis rotation)
		    if (self.window:KeyDown(Key.Left)) then self.move.y= -1 * movespeed * Time:GetSpeed() end --Look if left arrow key is pressed and Set value in selfmove.y (y-axis rotation)

 self.entity:AddForce(self.move.x,0,0)   --AddForce with the self.move.x value on x-axis
 self.entity:AddForce(0,0,self.move.z) --AddForce with the self.move.z value on z-axis
 self.entity:AddTorque(0,self.move.y*50,0) --AddTorque with the self.move.y value on y-axis (rotation)
 Time:Update() --Update Time value
end

function Script:PostRender() --Function to print on game screen
local context=Context:GetCurrent()
context:SetBlendMode(Blend.Alpha) --Set blend to alpha so only letters will show
context:DrawText("Rotation :".. self.ry .."",5,130) --Set text and position of text
--context:DrawText("Speed    :".. self.speed .."",5,140) --Set text and position of text
end

Link to comment
Share on other sites

I can see you are trying to simulate the reality.

 

thing is if you turn your sails the "boat" will go sideways it's same in RL but there's 1 thing you haven't in calculated witch is friction of water.

 

in RL friction of water does actually force for boat to go aerodinamically and not sideways.

 

what you need to do is 2 things

 

make proper parenting and after sails are turned so the boat slowly turnes in right dirrection.

without proper parrenting you'll be turning both instead of just desiered one.

 

well I hope I did help enough and that I did understand the problem.

Learn to obey

before you command

 

when you see very good tactic

sit back and think again.

 

Basics prince Zuko.

Basics are your greatest strength.

Link to comment
Share on other sites

I can see you are trying to simulate the reality.

 

thing is if you turn your sails the "boat" will go sideways it's same in RL but there's 1 thing you haven't in calculated witch is friction of water.

 

in RL friction of water does actually force for boat to go aerodinamically and not sideways.

 

what you need to do is 2 things

 

make proper parenting and after sails are turned so the boat slowly turnes in right dirrection.

without proper parrenting you'll be turning both instead of just desiered one.

 

well I hope I did help enough and that I did understand the problem.

 

It is not a sailboat but a motorboat I just want it to sail in the direction it is pointing even after I turn it.

But it only does that at small angle after that it goes back wards and I dont know why.

Link to comment
Share on other sites

It is not a sailboat but a motorboat I just want it to sail in the direction it is pointing even after I turn it.

But it only does that at small angle after that it goes back wards and I dont know why.

 

still it's kinda same thing except that "motor" is not on the middle but rear.

 

now like others said if there's a point force than use that else do it as I said.

 

experimenting and tweeking and you'll get the numbers right, ...

Learn to obey

before you command

 

when you see very good tactic

sit back and think again.

 

Basics prince Zuko.

Basics are your greatest strength.

Link to comment
Share on other sites

still it's kinda same thing except that "motor" is not on the middle but rear.

 

now like others said if there's a point force than use that else do it as I said.

 

experimenting and tweeking and you'll get the numbers right, ...

 

 

If I us pointforce it still will not get the boat to sail in the direction it is pointing and it makes the boat sink I tryed that.

And what do you mean with "make proper parenting". I dont have a sail or a motor. I am testing with a box.

Link to comment
Share on other sites

If I us pointforce it still will not get the boat to sail in the direction it is pointing and it makes the boat sink I tryed that.

And what do you mean with "make proper parenting". I dont have a sail or a motor. I am testing with a box.

 

by proper parenting I mean making 2+ objects.

 

if you rotate the motor you don't want to rotate the boat.

 

BUT the boat should slowly want to rotate in direction of witch the motor is or is it inverse direction, not sure, ...

how fast it would turn you have to adjust that numbers in script.

Learn to obey

before you command

 

when you see very good tactic

sit back and think again.

 

Basics prince Zuko.

Basics are your greatest strength.

Link to comment
Share on other sites

by proper parenting I mean making 2+ objects.

 

if you rotate the motor you don't want to rotate the boat.

 

BUT the boat should slowly want to rotate in direction of witch the motor is or is it inverse direction, not sure, ...

how fast it would turn you have to adjust that numbers in script.

 

I try to do that with a hinge in my first script but. I am not so good with the hinge command and dont see alot of examples.

Also did not get alot of feedback on that script it is at top of this post. I would like it if you could tell me how to couple 2 boxes one to be motor and one the boat then the newton engine could do its work and it would be simple.

Link to comment
Share on other sites

I try to do that with a hinge in my first script but. I am not so good with the hinge command and dont see alot of examples.

Also did not get alot of feedback on that script it is at top of this post. I would like it if you could tell me how to couple 2 boxes one to be motor and one the boat then the newton engine could do its work and it would be simple.

 

sorry don't know how newton engine works, I ment for you to make your own kind of "phisics engine" I also can't help much with scripts as I don't have tools.

All I have is tools for Unity 3D and knowledge from there.

 

I'm still waiting for 3.1 to be released, ...

 

but anyway let say you make boat parent of motor.

 

now motor is 45° turned

and boat rotates in direction that motor is for 0.45° each frame.

 

but because motor is child of boat the motor will stay at 45° and it will not turn.

 

but boat will turn.

 

 

well if you want to do with complete physics well good luck with that as people wanted to do it for normal walking and ended up using simulations - too much performance effective, ....

Learn to obey

before you command

 

when you see very good tactic

sit back and think again.

 

Basics prince Zuko.

Basics are your greatest strength.

Link to comment
Share on other sites

sorry don't know how newton engine works, I ment for you to make your own kind of "phisics engine" I also can't help much with scripts as I don't have tools.

All I have is tools for Unity 3D and knowledge from there.

 

I'm still waiting for 3.1 to be released, ...

 

but anyway let say you make boat parent of motor.

 

now motor is 45° turned

and boat rotates in direction that motor is for 0.45° each frame.

 

but because motor is child of boat the motor will stay at 45° and it will not turn.

 

but boat will turn.

 

 

well if you want to do with complete physics well good luck with that as people wanted to do it for normal walking and ended up using simulations - too much performance effective, ....

 

Ok I will try to find out how to make parent and child in leadwerks.

Thanks for the help so far.

Link to comment
Share on other sites

Ok I will try to find out how to make parent and child in leadwerks.

Thanks for the help so far.

 

wait you want to say you've never encountered parenting in LeadWerks?

 

Oh my, ...

 

if that's the case you'll need to implement it your self, ... I can't say that's easy, ...

Learn to obey

before you command

 

when you see very good tactic

sit back and think again.

 

Basics prince Zuko.

Basics are your greatest strength.

Link to comment
Share on other sites

wait you want to say you've never encountered parenting in LeadWerks?

 

Oh my, ...

 

if that's the case you'll need to implement it your self, ... I can't say that's easy, ...

 

yeah its superhard

 

SetParent

 

you need to read the docu for this and edit one line of code!

It doesn´t work... why? mhmmm It works... why?

Link to comment
Share on other sites

yeah its superhard

 

SetParent

 

you need to read the docu for this and edit one line of code!

fjufff, ... glad it exists, ...

 

wait so you can't parrent them in scene?

Learn to obey

before you command

 

when you see very good tactic

sit back and think again.

 

Basics prince Zuko.

Basics are your greatest strength.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...