Jump to content

Walk through walls


codeape
 Share

Recommended Posts

Hello

 

I like Leadwerks biggrin.png ... and have a lot fun with it ... however

 

I am playing around with the Steam Leadwerks (Lua) and try to make a simple "top down shooter". I have watched some tutorials and I thought I did this right.

 

I create my player (currently a cone) like this:

 

--Player
self.coneMat = Material:Load("Materials/Developer/greengrid.mat")
self.cone = Model:Cone()
self.cone:SetMaterial(self.coneMat)
self.cone:SetPosition(0,1.75,0)
self.cone:SetRotation(90,0,0)
local shape = Shape:Cone(0,0,0, 0,0,0, 1,1,1)
self.cone:SetShape(shape)

--Player movement speed
self.moveSpeed = 0.1

 

 

I have created a floor and 4 walls in the editor and the walls have "Physics mode : Rigid Body" and a shape size that corresponds to the wall size (used the "Fit Shape" button in the Physics tab).

 

The player movement code looks like this:

-- Player movement
local xMod = 0;
if self.window:KeyDown(Key.D) then
   xMod = 1 * Time:GetSpeed() * self.moveSpeed
elseif self.window:KeyDown(Key.A) then
   xMod = -1 * Time:GetSpeed() * self.moveSpeed
end

local zMod = 0
if self.window:KeyDown(Key.W) then
   zMod = 1 * Time:GetSpeed() * self.moveSpeed
elseif self.window:KeyDown(Key.S) then
   zMod = -1 * Time:GetSpeed() * self.moveSpeed
end

local curr = self.cone:GetPosition(true)
curr.x = curr.x + xMod
curr.z = curr.z + zMod
self.cone:SetPosition(curr, true)

curr = self.cam:GetPosition(true)
curr.x = curr.x + xMod
curr.z = curr.z + zMod
self.cam:SetPosition(curr, true)

 

The cone go through the walls. What am I doing wrong?

Link to comment
Share on other sites

Ok, added this to App.lua

 

function App:UpdatePhysics()
   System:Print("Hook 1")
   local curr = self.cone:GetPosition(true)
   curr.y = self.camDist
   self.cam:SetPosition(curr, true)
end

 

According to the Scripts reference: "This function will be called once per physics step."

http://www.leadwerks.com/werkspace/page/documentation/_/script-reference/

 

However it looks like it is never called. What am I missing?

Link to comment
Share on other sites

Ahhha I probably need to do my things in Entity::UpdatePhysicsHook and Entity::UpdateHook() to get things smooth:

 

http://www.leadwerks.com/werkspace/page/documentation/_/command-reference/entity/entityupdatephysicshook-r62

 

How do I set a Hook on a Entity?

 

Correct me if I'm wrong but I'd say you have those hooks automatically in LUA. When you create a new script it seems to contain all hooks:

 

--[[
function Script:Start()

end
]]--
--[[
function Script:UpdateMatrix()

end
]]--
--[[
function Script:UpdateWorld()

end
]]--
--[[
function Script:UpdatePhysics()

end
]]--
--[[
function Script:Collision(entity, position, normal, speed)

end
]]--
--[[
function Script:Draw()

end
]]--
--[[
function Script:DrawEach(camera)

end
]]--
--[[
function Script:Release()

end
]]--
--[[
function Script:Cleanup()

end
]]--

 

You just have to un-comment and use them :)

Link to comment
Share on other sites

I got it to work now =)

 

function Script:Start()
   --Player movement speed
   self.moveSpeed = 0.2
   -- Camera distance from player
   self.camDist = 10
   -- Create camera and position it
   local camPos = self.entity:GetPosition(true)
   self.cam = Camera:Create()
   camPos.y = self.camDist
   self.cam:Move(camPos)
   self.cam:SetRotation(90,0,0)
end
function Script:UpdatePhysics()
   local window = Window:GetCurrent()
   local context = Context:GetCurrent()
   -- Player keyboard input
   local xMod = 0;
   if window:KeyDown(Key.D) then
       xMod = 1 * Time:GetSpeed() * self.moveSpeed
   elseif window:KeyDown(Key.A) then
       xMod = -1 * Time:GetSpeed() * self.moveSpeed
   end
   local zMod = 0
   if window:KeyDown(Key.W) then
       zMod = 1 * Time:GetSpeed() * self.moveSpeed
   elseif window:KeyDown(Key.S) then
       zMod = -1 * Time:GetSpeed() * self.moveSpeed
   end
   -- Move player
   local curr = self.entity:GetPosition(true)
   curr.x = curr.x + xMod
   curr.z = curr.z + zMod
   self.entity:SetPosition(curr, true)
   -- Move camera
   curr.y = self.camDist
   self.cam:SetPosition(curr, true)
end

 

I did some other changes. Like not creating my player Entity (a box) in code (for now). And I changed the physics mode to "Character Controller" (the Collition type is "Character" also but I do not think that maters for the problems I had).

 

I also moved all code from App.lua to it's own script that is attached to the player Entity.

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