mdgunn Posted September 3, 2015 Share Posted September 3, 2015 Q: What's the best way to push objects (like a crate) around and have them lock to fixed directions? E.g Push a 'crate' an undetermined distance along a path/paths but only allow it to move in fixed directions either 1 axis as if on a track or 2 axis ( e.g. 'crate' pushing puzzle). I have something working (see below) but I don't think it's the best way to go. Are there any example maps/code of pushing a crate around fixed tracks on a level. It seems like the sort of thing someone must have done but I've yet to see any examples. The problem seems to break down like this: 1) Have an object to apply forces to (the 'crate') with a script to detect collision. 2) Detect the direction of push - currently faking this with 'trigger boxes' on the side. Maybe I need some trigonometry or something to work out the side of impact between the bodies? 3) Apply a force to the object limiting it's effect to a single axis (perhaps something in PhysicsUpdate?) At the minute I have a working example where I can push a box in 1 direction using an invisible trigger box sticking out of it on the side I want the player to push from but I think this is probably not the best way to do it and am looking for someone who knows better! function Script:Collision(entity, position, normal, speed) -- System:Print("Collision speed = "..speed) -- Speed will be zero if 'trigger box' is a trigger as it needs mass 0 to avoid dropping down through floor/level. -- if speed>self.threshhold then if self.entity:GetParent() ~= nil then local parent = self.entity:GetParent() parent:AddForce(0,0,200,true) end -- end end Thanks, Michael Quote Link to comment Share on other sites More sharing options...
Josh Posted September 3, 2015 Share Posted September 3, 2015 You could create a slider joint so it can only travel along a certain distance in one direction: http://www.leadwerks.com/werkspace/page/api-reference/_/joint/jointslider-r737 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
mdgunn Posted September 3, 2015 Author Share Posted September 3, 2015 Thanks Josh, I had considered a joint slider. I checked out the docs and can see it is designed to limit movement in 1 direction but I wasn't sure if it was the best way to go for controlling crate movement in more than one axis. Sounds like I would still need to detect which way I'm trying to push and then setup (or disable) slider joints to allow me to push only in the direction I'm trying. I will take a deeper look. Cheers Quote Link to comment Share on other sites More sharing options...
mdgunn Posted September 3, 2015 Author Share Posted September 3, 2015 I've hacked away at the sliding door script to create a script that moves the attatched object away as you bump into it (well you actually bump into a trigger collider currently) Problem is, it will only do it twice then stop. I think maybe the slider joint is not actually resetting when I am trying to reset it and has reached it's limit so not resetting properly. I am trying to reset it each time a move completes but I think I don't understand either how the slider works, or the vector information it needs to alter it's position or something. Can anyone take a look at the associated map and script and point out the probably obvious error? NOTE: The script is a real hack job and not meant to be complete so please forgive the mess! Here is a Dropbox link which I hope works OK. Unsure if I can link a file to this post. https://www.dropbox.com/s/3ctogtsxjf239qh/PushAwayJoint.zip?dl=0 (contains map, script and screenshot) If there's a better way to supply a package then please let me know. Maybe I should try another way altogether? Thanks, Michael Quote Link to comment Share on other sites More sharing options...
BluHornet Posted September 9, 2015 Share Posted September 9, 2015 What you could do is make the crate and then add invisible csg cubes around the crate and make them a child of the crate. Make a short script for each that when the crate is moved a set distance in a set direction from the player when used. you would need to set the direction and distance. something like when used take the crates position and add a set amount to the x and z coordinates of the crate then save that as a set position. then if the set position is not the current position move the current position closer. If I wanted to move "South" then I would change the set position to the current position's x value to the value -2. This is a good application for lerp (linear interpolation). Also some smoothing could make this better. One last thing if this is not done in update physics you will need a time factor for reliability. Quote Link to comment Share on other sites More sharing options...
mdgunn Posted September 10, 2015 Author Share Posted September 10, 2015 Yeah the CSG 'trigger' cubes and manual intervention in the movement is the direction I was heading when I wondered if I was heading the right way, so it's reassuring that you seem to think it's a doable solution. Thanks for the extra pointers. I felt sure someone had probably done a 'cube pusher' but so far someone that has completed such an implementation doesn't seem to have passed by yet. Thanks for the help. 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.