What is the best way to manipulate a StaticBody?

Icaro-LimaIcaro-Lima Posts: 4Member

So I have a plunger who pushes a ball. When you click a button on the GUI, the plunger should move to the left.


Here's how I'm doing:

onready var pin = get_node("Pin")

var start_rotation = false
var count = 0
var total = 2.5
var speed = 5

func _physics_process(delta):
    if start_rotation and count < total:
        count += speed * delta
        pin.translate(Vector3(-speed * delta, 0, 0))

I thought I was doing everything correctly, until I increased speed and simply plunger crossed the ball.

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 1,049Admin

    You’ll probably need to add a if check after you add to count to check whether speed * delta is more than count. If you do not need to do anything special when count is over total, then you could also just use clamp.

    Either way, something like the following should hopefully fix the problem:

    # other code above...
    if start_rotation and count < total:
        # Method one, use clamp
        count = clamp(count + speed * delta, 0, total)
        # Method two, use a if check
        # count += speed * delta
        # if (count > total):
        #   print (“count is more than total. Do any processing needed here!”)
        #   count = total
        pin.translate(Vector3(-speed * delta, 0, 0))
    

    Hopefully this helps :smile:

  • Icaro-LimaIcaro-Lima Posts: 4Member

    Thanks for the answer!

    Anyway I think my problem is more related to the method I used to move a static body, not how I used it. The movement is working exactly as I expect, the problem is how objects that collide with the plunger react.

    For example, let's assume my plunger was an arrow and hit a very strong cube. The cube should fly away, but what is happening is that the arrow is going through the cube.

  • TwistedTwiglegTwistedTwigleg Posts: 1,049Admin

    @Icaro-Lima said:
    Thanks for the answer!

    Anyway I think my problem is more related to the method I used to move a static body, not how I used it. The movement is working exactly as I expect, the problem is how objects that collide with the plunger react.

    For example, let's assume my plunger was an arrow and hit a very strong cube. The cube should fly away, but what is happening is that the arrow is going through the cube.

    My apologizes, I should have looked closer at the title and original post :sweat_smile:

    As for the problem of the plunger going through things, is your plunger a RigidBody? If so, you may be able to fix the problem by turning on continuous collision detection. This should make it where the RigidBody is better at colliding with objects at high speeds, as it tries to predict where moving bodies will collide rather than moving and the correcting. You can turn on continuous collision detection by setting the continuous_cd property of the RigidBody to true

    I think the issue is happening is because the plunger is moving at a fast enough speed that it goes through the object, and then when it tries to correct for any collisions, it doesn't see a collision because the plunger is fully through the object. Turning on continuous collision detection should make it where it will not go through objects at high speeds, as it first checks to see if there is a collision before moving.

    Hopefully this helps :smile:

  • Icaro-LimaIcaro-Lima Posts: 4Member
    Answer ✓

    The solution I found was to use KinematicBody instead of StaticBody. Thank you anyway.

Sign In or Register to comment.