Jump to content

Initial speed of an object


Braqoon
 Share

Recommended Posts

Hi,

 

I'm trying to do a simple mockup of a shmup. Taking controls from marble game tutorial at least for a start all seems to be working fine. Applying force to an object works as desired but for obvious reason force looses it's kinetics and object eventually stops. This is all good, but what if I would like to have a minimal speed of an object that moves in one direction. I have used SetVelocity but that also is not constant and value decreases without any update. If I will be updating velocity on UpdateWorld (currently commented out) then AddForce is not working smoothly. Any advice ?

 

Below my player.lua that controls a box in the world.

function Script:Start()
--Create a camera
self.camera = Camera:Create()
--Update the camera
self:UpdateCamera()
self.health = 100
self.startposition = self.entity:GetPosition()
self.entity:SetMass(1)
self.entity:SetGravityMode(false)
self.entity:SetFriction(0,0)
speed = 5
vel = Vec3(0,0,speed)
self.entity:SetVelocity(vel)
--self.entity:AddForce(0,0,200,true)
end
function Script:UpdateCamera()
self.camera:SetRotation(60,0,0)
self.camera:SetPosition(self.entity:GetPosition())
self.camera:Move(0,0,-20)
end
function Script:UpdatePhysics()
--Get the game window
local window = Window:GetCurrent()
if window:KeyDown(Key.W) then self.entity:AddForce(0,0,50,true) end
if window:KeyDown(Key.A) then self.entity:AddForce(-50,0,0,true) end
if window:KeyDown(Key.D) then self.entity:AddForce(50,0,0,true) end
if window:KeyDown(Key.S) then self.entity:AddForce(0,0,-50,true) end
end
function Script:UpdateWorld()
self:UpdateCamera()
--self.entity:SetVelocity(vel)
end

 

Thanks

Link to comment
Share on other sites

I had to do something kind of similar with a bowling ball. What I did was checked velocity, and if it was below a threshold I flat out set velocity to the minimum for that axis in UpdatePhysics()

 

I'd recommend to update your velocity in UpdatePhysics() if possible, if I recall correctly it always runs at a constant rate regardless of FPS, where UpdateWorld does NOT. if you have to set velocity in update world, scale it by Time:GetSpeed() so its not jittery

Link to comment
Share on other sites

Thanks for reply @Dexter. Had to tinker around but I think this will pass for very simplified controller for shmup. Obviously this is a starting point.

 

function Script:Start()
--Create a camera
self.camera = Camera:Create()
--Update the camera
self:UpdateCamera()
self.startposition = self.entity:GetPosition()
-- Ship Vars
self.entity:SetMass(1)
self.entity:SetGravityMode(false)
self.entity:SetFriction(0,0)
minimal_speed = 5
max_speed = 20
max_pos = 15
pos = 0
speed = 5
throttle = 1
vel = Vec3(pos,0,speed)
self.entity:SetVelocity(vel)
end
function Script:UpdateCamera()
self.camera:SetRotation(60,0,0)
self.camera:SetPosition(self.entity:GetPosition())
self.camera:Move(0,0,-20)
end
function Script:UpdatePhysics()
--Get the game window
local window = Window:GetCurrent()
--Ship movement
if window:KeyDown(Key.W) then
if speed < max_speed then
speed = speed + throttle
end
end
if window:KeyDown(Key.A) then
if pos > -max_pos then
pos = pos - throttle
end
end

if window:KeyDown(Key.D) then
if pos < max_pos then
pos = pos + throttle
end
end
if window:KeyDown(Key.S) then
if speed > minimal_speed then
speed = speed - throttle
end
end
vel = Vec3(pos,0,speed)
self.entity:SetVelocity(vel)
end
function Script:UpdateWorld()
self:UpdateCamera()
end

 

Above script will not allow to drop velocity lower than min_speed, go higher than max_speed and left/right speed bigger than max_pos. I know this is pointing the obvious but for someone new could be useful.

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...