0 votes

I know I can rotate any spatial around a point using:

public static void RotateAround(Spatial obj, Vector3 point, Vector3 axis, float angle)
{
    obj.GlobalTranslate(-point);
    obj.Transform = obj.Transform.Rotated(axis, -angle);
    obj.GlobalTranslate(point);
}

But how can I rotate a KinematicBody around a point and still collide? Right now, it just phases through everything. The move_and_collide, move_and_slide, move_and_slide_with_snap, and test_move all only take translate vectors as params, but no rotational vectors.
One of my use cases:

Example
When rotating this shape, the pivot point is at the bottom right of that blue cube, but when it collides with the ground, I want the pivot point to move to be where it collided. Is there any way to do that?

Is there even an easier way to do this?

Godot version Godot Engine v3.4.stable.mono.official [206ba70f4]
in Engine by (16 points)

1 Answer

+1 vote

Collisions come from the Godot's physic's engine and kinematic bodies access that with move_and_slide() (or move_and_collide) that will automatically return a collision which you can pick up in _physics_process with get_slide_collision so you need something along these lines:

func _physics_process(delta):
    if get_slide_count:
        var col = get_slide_collision[0]
        printt("You have hit:", col.collider)
    velocity = -global_transform.basis.z.rotated(Vector3.FORWARD, rotation) * rot_speed
    velocity = move_and_slide(velocity)

So I just take the negative z direction of the basis and rotate it along an axis and scale it up. Obviously you need to create the appropriate member variables.

by (2,156 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.