Jump to content

How do you push a 'crate' in a locked directions?


mdgunn
 Share

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

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