#How would i go about adding collision to this?

4 messages · Page 1 of 1 (latest)

shrewd pecan
#

I have a rope that rotates around a point on the ceiling grabbed by a raycast
only issue is that i'm setting the player's global position directly, breaking the CharacterBody2D's collision detection
I tried setting the player's velocity to the new coordinate - the current global position but that somehow made the player rotate in a sideways 8 shape instead
Here's all the relevant code (all running in the _physics_process():

#

` # Default State
# Ground Movement
if state == 0:
direction = Input.get_axis("left","right")
var slope_mSpeed : float
up_down_hill = sign(ground_normal.x * direction)
slope_mSpeed = max_speed + abs(sin(ground_angle) * slope_modifier/delta) * up_down_hill
if velocity.x <= slope_mSpeed - acceleration and velocity.x >= -slope_mSpeed + acceleration:
velocity.x += acceleration * direction
pass
if sign(velocity.x) != sign(direction):
velocity.x += direction * turn_acceleration
if !direction:
velocity.x -= deceleration * sign(velocity.x)
if abs(velocity.x) <= deceleration:
velocity.x = 0

    # Transition to swing state
    
    if Input.is_action_pressed("swing") and swing:
        var direction = Input.get_vector("left","right","up","down").normalized()
        if direction: raycast.target_position = direction * swing_range
        if raycast.is_colliding():
            grapple_position = raycast.get_collision_point()
            rope_position = self.global_position
            var rope_vector = (rope_position - grapple_position)
            rope_angle = rope_vector.normalized().angle()
            rope_length = sqrt(pow(rope_vector.x,2) + pow(rope_vector.y,2))
            rope_angle_velocity = ((velocity.length() / rope_length) * -sign(direction.x) * delta) / rope_acceleration_rate
            print(rope_angle_velocity)
            state = 1
            velocity.x = 0
            velocity.y = 0
            $Sprite2D.position = grapple_position
        pass`
#

if state == 1: var rope_angle_acceleration = rope_acceleration_rate * -cos(rope_angle) rope_angle_velocity += rope_angle_acceleration * delta rope_angle += rope_angle_velocity rope_angle_velocity *= rope_dampening rope_position.x = grapple_position.x + cos(rope_angle) * rope_length rope_position.y = grapple_position.y - sin(rope_angle) * rope_length print(rope_angle) #velocity = (global_position - rope_position) global_position = rope_position #print(rope_angle_velocity) if !Input.is_action_pressed("swing"): jump(jump_velocity + rope_angle_velocity, rope_angle, true) grapple_position = Vector2(0,0) rope_position = Vector2(0,0) rope_angle_velocity = 0 rope_angle = 0 rope_length = 0 state = 0 move_and_slide()

shrewd pecan
#

Fixed it!