Hello everyone. I have a quick question. Can I somehow prevent rigidbody to be moved by colliding with other while still moving others rigid bodies by collisions and be moveable by setting velocity from code ? Unfortunately, setting axis locks prevent it from being moved by velocity so I'm out of ideas. Thanks -Garrom
How I can prevent rigidbody being affected by colliding with others ?
From my experience, there is a couple ways to do this:
One way is to set the Rigidbody and the object you do not want the RigidBody to collide with on different masks/layers. I can never remember quite how the layer/mask system works, but I've found that putting the RigidBody on one layer and mask, and putting the other object on another layer and mask makes it where they do not collide with each other. The downside with this method (for me personally) is I have to play around with it until I get a mask/layer combination I like.
The other way you can do it is by making a collision exception in your RigidBody using add_collision_exception_with
. Then the RigidBody will not collide with the object passed in to add_collision_exception_with
. The big downside with this is you will have to add a collision exception for each object you want the RigidBody to avoid, which depending on how your project is setup, it may be very impractical if not impossible (for example, this does not really work with randomly generated material).
Hopefully this helps!
@TwistedTwigleg said: From my experience, there is a couple ways to do this:
One way is to set the Rigidbody and the object you do not want the RigidBody to collide with on different masks/layers. I can never remember quite how the layer/mask system works, but I've found that putting the RigidBody on one layer and mask, and putting the other object on another layer and mask makes it where they do not collide with each other. The downside with this method (for me personally) is I have to play around with it until I get a mask/layer combination I like.
The other way you can do it is by making a collision exception in your RigidBody using
add_collision_exception_with
. Then the RigidBody will not collide with the object passed in toadd_collision_exception_with
. The big downside with this is you will have to add a collision exception for each object you want the RigidBody to avoid, which depending on how your project is setup, it may be very impractical if not impossible (for example, this does not really work with randomly generated material).Hopefully this helps!
No, no, you don't understand. I want them to collide but i need one rigid body to collide with others but other rigid bodies cannot push this body away with collision. I need to make it static but it must be rigidbody because I need to use linear velocity. It is part of level, It must appear static but it must be moveable with linear velocity (what is only rigidbody). To clarify, I have part of level what I want move from script by setting velocity. Problem is, it goes down when player standing on it because player is affected by gravity and pushing it downwards. I want it to act like static body but it must be movable using linear velocity what works only on rigidbody.
Oh, I see!
In that case, maybe try changing the RigidBody mode in the rigid body you want to be static? Here's a break down of the modes RigidBody can take (taken from Schuster's post on this discussion)
MODE_RIGID = 0 — Rigid body. This is the “natural” state of a rigid body. It is affected by forces, and can move, rotate, and be affected by user code. MODE_STATIC = 1 — Static mode. The body behaves like a StaticBody, and can only move by user code. MODE_CHARACTER = 2 — Character body. This behaves like a rigid body, but can not rotate. MODE_KINEMATIC = 3 — Kinematic body. The body behaves like a KinematicBody, and can only move by user code.
I would give an example, but I have always used Kinematic bodies for this kind of thing :D
- Edited
I'm only familiar with 2D godot but this might get the job done anyways.
If you don't want your rigidbody's motion to be affected at all by other bodies, make your rigid body node be on MODE_STATIC or just change the node type to static body. Then use the method set_constant_linear_velocity(a_vector)
to set its linear velocity. Then use the method translate(get_constant_linear_velocity() * delta)
in the _process(delta)
function of the staticbody. Indeed, not only rigidbodies can be moved with linear velocity. It will lose the other properties of rigidbodies such as gravity, so you might need to do a bit of math to calculate what it's constant linear velocity will be if it's in your interest to keep gravity.
Great trick and welcome to the forum!