Jump to content
  • 0
Phodex Games

Changing position of physics object disables physics?

Question

So I think this is a bug. When I load a prefab with physics on, it works in the first place and my player can collide with the object (Collision set to scene and physics to rigid body). If I change the position however the physics is disabled and I can walk through the object. Changing the position again did sometimes turn the physics on again. This is some of the weird behavior I every now and then encounter with Leadwerks :(, especially with Prefabs. This did not happen when using the "Move" function instead of SetPosition. How could I fix this? I need to change the positon of an physics object during runtime...

Share this post


Link to post

39 answers to this question

Recommended Posts

  • 0
10 minutes ago, Josh said:

If you are using polygon collision then the object can't be physically dynamic. You need convex decomposition for that.

However, you can call SetPosition or Move once and the collision will work after that. If you are continuously calling it, it resets collision each frame.

 

Could you expand on this a little more.  I don't fully think I fully understand this. I know convex decomposition from the editor but not sure I understand the relationship with 'polygon collision' and 'physically dynamic'.   Also the 'continuously calling' and 'resetting' sounds very bad.  If you move a box a few times and reapply the collision type each time does this lead to checking each frame (and why things sometimes seemed to jiggle about - at least in the past).  Are you saying the rechecking each frame is not possible to stop if you want an object you can move many times?

 

Share this post


Link to post
  • 0
40 minutes ago, mdgunn said:

Try the below

That actually works, BUT the weird is if I change the first SetPosition to self.startPos.x + 5 instead of +1 it does not work 🤔. However the physics are alright if set back to start position...

 

Share this post


Link to post
  • 0
24 minutes ago, Josh said:

However, you can call SetPosition or Move once and the collision will work after that

Yeah this explains why, in my actual setup, the physics breaks when the prefab is loaded the second time and placed in the scene. At one point I release a room and load another one. When a prefab is loaded in the scene the second time and position is changed physics are broke. You are talking about "continuously" calling this, but I am actually not. I just call it once after a prefab is loaded. 

Share this post


Link to post
  • 0
49 minutes ago, Phodex Games said:

Yeah this explains why, in my actual setup, the physics breaks when the prefab is loaded the second time and placed in the scene. At one point I release a room and load another one. When a prefab is loaded in the scene the second time and position is changed physics are broke. You are talking about "continuously" calling this, but I am actually not. I just call it once after a prefab is loaded. 

This is not how I would expect it to behave. If you upload an example I can try then I can determine what's going on.

Share this post


Link to post
  • 0
17 minutes ago, Josh said:

If you upload an example I can try then I can determine what's going on.

Well to see the problem I am explain I would need to upload my whole project, which is relatively large. If I have time I can try reproducing the error in a smaller setup and upload it here. But as the tests with the box results in the same behavior (disabled physics when setting position after an entity is spawned) I though it is actually the same error as with my more complex scene... Sorry I don't understand why the setup I and mdgunn described is not enough to examine the problem...

I mean the question still remains, how to change an objects position with static scene phyiscs after its placed in scene. I see no solution yet. I understand that it has to do with the physics engine not updating the new position of the object, but what to do against it?

Share this post


Link to post
  • 0

I do not want to spend the afternoon trying to recreate a certain situation from a description, when there is a good chance I will not be able to, or will end up creating something other than what was described.

Share this post


Link to post
  • 0
18 minutes ago, Josh said:

or will end up creating something other than what was described.

Well ok I guess you are right. I just meant I need like 5 minutes to setup a scene like this. I know you are busy and I appreciate you take the time... 

I attached a test project. Just open the start.map. If you press "K" the box moves and you can walk through the box. Now the question is how to move it without breaking the physics.

TestLevel.zip

Share this post


Link to post
  • 0
1 hour ago, Phodex Games said:

That actually works, BUT the weird is if I change the first SetPosition to self.startPos.x + 5 instead of +1 it does not work 🤔. However the physics are alright if set back to start position...

 

Strange, I tried self.startPos.x + 5 instead of +1 and it goes back and forth multiple times fine still.

move_csg.lua

start3.map

Share this post


Link to post
  • 0
14 hours ago, mdgunn said:

Strange, I tried self.startPos.x + 5 instead of +1 and it goes back and forth multiple times fine still.

I downloaded your files. First it worked, but when I created a new cube and changed it to +15 it did again not work. I now also experienced, when changing back to +5 with another cube that just the front of the box had physics and I can walk inside the cube from the sides. Play around with different pos values (like +5 +15 +25) and you will see the physics break again...

Share this post


Link to post
  • 0
On 10/1/2018 at 11:08 AM, Phodex Games said:

I downloaded your files. First it worked, but when I created a new cube and changed it to +15 it did again not work. I now also experienced, when changing back to +5 with another cube that just the front of the box had physics and I can walk inside the cube from the sides. Play around with different pos values (like +5 +15 +25) and you will see the physics break again...

I've never yet seen the physics break if I have an item with mass.  In my scene with +15 it would be drop off floor and gain large velocity so went sent back may miss floor collision and appear to act strange.  Maybe this is what you saw but maybe it was something else.  When I extend the floor and set to 15 I can't get a problem no matter how many times I move the cube...as long as the cube has some mass.

I can see the problem in your test level where I can walk through the cube after moving it.  To me this is because you have no mass set. 

Got a couple of questions:-

a) Do you need to move bits of your dungeon after you place them - why would they not be just set in place once?  It is dynamic during level?

b) Why not have the bits HAVE mass and sit them on an underlying 'scene floor' that does not move? Are you worried these bodies are being computed all the time?  I think the engine handles this and treats them as 'at rest'. Debug mode has a counter for active items and it should go to 0.

I tried a few things to try to explore the problem such as trying to re-apply a physics shape object and changing mass to 1 then 0 or setting gravity to 0 or not affecting the item but unless I allowed gravity to actually affect the item (e.g. mass over 0 and some gravity) the item would not regain collisions with it's physic shape (beyond the 1 allowed move). 

Seems like the engine should allow this but unless Josh knows a way if this is now any clearer as to what you want then you may be a bit stuck.

 

Share this post


Link to post
  • 0
8 hours ago, mdgunn said:

I've never yet seen the physics break if I have an item with mass

Ah yeah the new cube I created in your scene had 0 mass actually. It does actually work with mass > 0. The problem is if I set mass to my scene object then it sometimes just falls through the ground (which by the way is why I did not set a mass), or it behaves like a dynamic physics object, which I not want, I maybe have some wodden planks at the ceiling and don't want them to fall on the ground ^^.

8 hours ago, mdgunn said:

Do you need to move bits of your dungeon after you place them

Well I have different room layouts & different prebuild furnishing sets. A few rooms are preloaded and then everytime you exit a room an new one gets loaded and placed at the right position. This works in the first place, but I experienced that after a certain number of rooms, the collison of some objects are broken (so far just the furnishing objects, not the room itself were affected).

8 hours ago, mdgunn said:

Why not have the bits HAVE mass and sit them on an underlying 'scene floor' that does not move?

Could you explain this a little more? As explained about arent my objects flying around with mass enabled? How would you created the scene floor and not make him move?

By the way, thanks for you help :D its actually one of the last things I need to get done before I can release it...

Share this post


Link to post
  • 0
On 10/3/2018 at 8:44 AM, Phodex Games said:

Could you explain this a little more? As explained about arent my objects flying around with mass enabled? How would you created the scene floor and not make him move?

Sorry been away a couple of days.  I didn't fully understand the second sentence above.

My idea of 'scene floor' may not be a solution as I think you are having issues I haven't seen yet but the idea was that if physics can be harmed by moving things around then have a static object/cube or a number of them on which the other moving level parts would sit. This way if the underlying level floor is never moved then physics would not break for it and other parts on top can be moved as they will regain their physics collision after the move if they have mass.

Just an idea.   

In the image below the orange block would be the static floor and the blue bits would be the actual bits you want of your level which, hopefully, could be moved. You may never even have the user able to see/reach the underlying orange supporting floor. 

It sounds like you have other issues I have not seen yet. I think the supplied project seemed to work in the way Josh intended so maybe you need to supply one or more further examples.

 

level_floor.jpg.8d77c92505242d55caf5c218c388c80e.jpg

 

  

 

Share this post


Link to post
  • 0

Ok so I found a workaround for the issue. It actually works pretty good so far, still its not ideal, and I just tested it for a single non-prefab object with no parent, but it is at least something. A Physics:Refresh() function maybe would be a good idea? But thats just my unprofessional opinion.

function Script:Start()
	self.startPos = self.entity:GetPosition(true)
end

function Script:UpdateWorld()
	if self.moved then
    	--Move the object one frame after I gave it mass then remove the mass and set it back to scene collision
		self.entity:SetPosition(self.startPos.x + 15, self.startPos.y, self.startPos.z, true)
		self:SetMoveMode(false)
	end
	if keyHit.K then
		self:SetMoveMode(true)
		self.moved = true
	end
end

function Script:SetMoveMode(state)
	if state then
		self.entity:SetCollisionType(Collision.Prop)
		self.entity:SetMass(10)
	else
		self.entity:SetCollisionType(Collision.Scene)
		self.entity:SetMass(0)
	end
end

Future will show if this will also work for my more complexe scene...

 

EDIT: @mdgunn Just saw your reply. Yeah my sentence was a little weird. I meant that with this method, all of my objects will have dynamic physics, and objects (like wodden planks) I place on the roof of a room will fall to the ground when mass is enabled. However as you can see above I found a solution, at least for the moment.

Share this post


Link to post
  • 0

I get it about the planks now.  I suppose light fittings on wall or ceiling might be a similar thing. Sometimes you really just want something to be STATIC.

Yeah this was about where I got to with my quick tests.  There might be a slight improvement where you also set gravity to 0 (or near zero if zero is a problem) to further minimise movement between frames when mass applied if there is some movement. 

Seems like your use case is not that strange and there should be a way for the engine to not require the developer to do this additional setup.  Maybe Josh can chip in and tell us a different way that wasn't apparent before, or add support for this to work without the workaround.

 

 

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

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

×
×
  • Create New...