codeape Posted January 10, 2014 Share Posted January 10, 2014 Hello I like Leadwerks ... 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? Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted January 11, 2014 Share Posted January 11, 2014 If you use SetPosition, it is important to make the distinction between object movement en physics movement. When you move with setposition, physics are ignored. Have a look at this one: PhysicsSetPosition(); Quote Link to comment Share on other sites More sharing options...
cassius Posted January 11, 2014 Share Posted January 11, 2014 I think you have to set the physics type and mass for the player too, in order to get collision. Quote amd quad core 4 ghz / geforce 660 ti 2gb / win 10 Blender,gimp,silo2,ac3d,,audacity,Hexagon / using c++ Link to comment Share on other sites More sharing options...
codeape Posted January 11, 2014 Author Share Posted January 11, 2014 Thanks Aggror and cassius Tried PhysicsSetPosition() and added physics type Entity.RigidBodyPhysics and mass 1. Now it works but the movement is not very smoth like when I used SetPosition. I need to trim this Quote Link to comment Share on other sites More sharing options...
codeape Posted January 11, 2014 Author Share Posted January 11, 2014 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? Quote Link to comment Share on other sites More sharing options...
codeape Posted January 11, 2014 Author Share Posted January 11, 2014 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? Quote Link to comment Share on other sites More sharing options...
bandrewk Posted January 11, 2014 Share Posted January 11, 2014 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 Quote Link to comment Share on other sites More sharing options...
codeape Posted January 11, 2014 Author Share Posted January 11, 2014 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.