Set the local position of a rigidbody

Hey, so I have a game where there’s walls that limit the player from moving too much away from the center of the map.

These walls are a child of an Entity which localScale grows depending on the amount of players in the game. So when there’s more players, this parent will have a bigger localScale, which makes the Walls also bigger too, and moves them away from the center as the parent Entity grows up.

Problem is, when attaching a rigidbody to the walls so players can actually collide with them, the rigidbody appears to be at world 0,0,0 instead of the localPosition it must be into.

I have tried rigidbody.syncEntityToBody() but it doesn’t do anything. Looking for some insight at this point.

This is what the positioning of a Wall looks like:

    this.topLeftWall.setLocalPosition(new pc.Vec3(-0.222, 0, -0.396));
    this.topLeftWall.setLocalEulerAngles(new pc.Vec3(0,31.43,0));
    this.topLeftWall.rigidbody.syncEntityToBody();

Does syncEntityToBody not take in count the local position?

I have a silly question. Are the walls rigid body type set to ‘static’, ‘dynamic’ or ‘kinematic’?

If it’s static, it can’t be moved. You have to set it as ‘kinematic’ or ‘dynamic’.

Of course, it’s set to dynamic, with linearDamping capped so It doesn’t move on collision

Set it to ‘kinematic’ as you don’t want it to move on collision or if it is intersecting with another physics object.

Even with it set as ‘dynamic’, I can’t reproduce your issue: https://playcanvas.com/editor/scene/510567

Could there be any other code that is affecting this wall?

syncEntityToBody is not an API function. Not sure where you saw it referenced, but you should be using teleport instead. It doesn’t work with local position/rotation, but you can calculate local to world separately.

How can I calculate local to world? I am totally lost right now.

What I tried now was having a reference child into the parent, and a separate world object which isn’t parented to anything, and tried to set the position to the local position of the reference child, but it doesn’t seem to work.

Could you explain abstractly what you trying to do?

I’m going to explain to you in more detail, I’m sure i’m over-complicating this task.

This game has an hexagon-based environment (background is hexagons, and the whole arena is like a giant hexagon tile), as you can see in this pic:

Where things actually complicate is when this hexagon has to grow up (the arena will be bigger/smaller depending on the amount of online players). If it was static, I could just place the parts of the hexagon collision walls and don’t worry about it, but I can’t.

To understand my exact problem, you have to know that the hexagon walls are made of 6 collision boxes correctly aligned to the texture that you can see above.

So when I started designing this, I had in my mind something super simple; If I made all of these walls a children of the graphical part, they would expand accordingly just by growing up the graphical’s part localScale.

But my problems started when I was testing and clearly saw that the rigidbody wouldn’t get updated, it would stay at 0,0,0 and not follow the rules.

I guess that my idea isn’t meant to work at all, just going to think of another way of doing this. Maybe importing an hexagon mesh and using its collision will work.

I think best will be to split it into two things:

Rendering Hexagon. To render hexagon you need to have a mesh of such shape and texture properly tiled on it. The easiest way I can imagine to change it’s size and to change scaling of tiled texture accordingly. The easiest way would be to use some math of course: each corner of hexagon are on the same distance from center, and there are 6 slices of 60 degrees 360 / 6 = 60. By distance and 2D vectors made out of angle you can put each point at right position. Then you can change tiling property of material accordingly. To avoid sliding of texture on plane when scaling, you want to ensure that center of hexagon is 0,0 on UV. Tiling will be something like: radius / 4 - so every time you resize it, you update tiling.

Moving corners - simply can skin it, and make some bones for it, so in PlayCanvas you can findByName each bone, and move it as described above.

Collision, this is easier, and math again. You calculate middle point of hexagon lines, can be as simple as (pointA + pointB) / 2, that is middle point of edge, if you want to offset a bit from it, then you need to get vector between that point and center of the hexagon, normalize that vector and multiply on distance at which you need to offset rigidbody. You get X,Y vector that you need to move by the rigidbody.

That’s pretty much it :slight_smile:

Actually I ended up working this out in a simpler way, looks good! I will keep this as a note if we end up changing the walls or something.