Cannot update position while in physical use

Specifically, my character is at point (0,0,0) I apply applyImpulse(0, -100,20), while the ball is moving down physically, I cannot change the value of the axis x (left or right) by setPosition.

Why can’t I change that value as in unity.

1 Like

I think what’s happened here is that you’re setting position directly, instead of using the rigidbody or physics system. In Unity, you are able to do something like this, but if you translate an entity in playcanvas, physics are usually not called. Or if you are using the physics system, any direct change to an entity or entities will be ignored. Try using this.entity.rigidbody.teleport if you’re trying to reset or change the position of the entity. Or if you’re trying to make it move along the x-axis, use this.entity.rigidbody.applyForce. Also, it’d help a lot if you shared your project so people can try it out and see what the problem is for themselves. It can also help people understand your mistake much faster.

1 Like

Thank you for the quick support.

I will try to apply your advice. If I continue to have problems, I will share my project so people can see the problem

1 Like

No problem. I had a similar problem once, so I thought I share what helped me.

please let me ask, how can I change the halfExtents value of CollisionComponent with the code

I don’t see a function that resets its value in the documentation.
when I use this.entity.collision.halfExtents.z = 200; It seems to have no effect

1 Like

Maybe try this.entity.collision.halfExtents = new pc.Vec3(0, 0, 200);

1 Like

great, it worked,

However creating new pc.Vec3 can cause performance problems, because I have many objects, and they are constantly resized.

Is there any way to solve this problem

Try using Concatenate Scripts

image

If you need further help with performance, I suggest asking @will or @yaustar, because I’m not really good with performance issues.

1 Like

OK thank you very much

1 Like

No problem.

To avoid creating new pc.Vec3 objects during playtime you can create a single one in your initialize method:

this.vec = new pc.Vec3();

And when you need it, you can use it like this:

this.vec.set(0, 0, 200);
this.entity.collision.halfExtents = this.vec;

Though if you are resizing collision components on runtime and you use physics, Ammo is destroying/creating new physics shapes all the time. That will have a bigger performance hit anyhow.

It’s better to pre-generate a number of hidden objects in the various sizes your game requires (pooling) and enable/disable as needed. That will increase performance by not putting a heavy load on the garbage collector.

2 Likes

Good thinking

1 Like