#Accurate and Snappy "hover to mouse" of a 2D Physics Object

1 messages · Page 1 of 1 (latest)

solemn kernel
#

Hello!
I am using Godot 4.2.2.

I am trying to replicate the Character movement from games like Arcanorum (Online Flash game) or Hammerfight (Steam). The movement is aiming to imply a flying machine that follows the mouse cursor.

Because the character is supposed to "Ragdoll" after being struck (Rotate around it's own axis as it's flung by the force of the blow), and its means of attack is a physics action of winding up what's effectively a weight on a chain attached to your character, to smack other characters with it by pulling at the weight with your ship and making it spin around it, I've assumed a RigidBody2D would be the most effective choice.

I've managed to make the created body follow the mouse, as well as apply a "righting" torque to make the 'flying machine', or character, right itself, for me to disable when the character is going to be ragdolling (Whenever I work on that).

I've come across a problem, however, as now, the Character Orbits around the mouse, rather than closely following it. And With just barely enough force being applied, to reduce the potential for Orbiting, it not only still happens, but when it's 'on mouse', it bobs up and down anyhow. I can't come up with an obvious solution to do this.

I've thought about making the "Speed" or Force i apply to the ship be variable based on the distance from the mouse, But as it's a physics object, I believe i'd need to apply an opposite Force to slow down the character as it approaches within a certain distance of the Destination/mouse? (As i'll probably want to use the same code for both npc and player character movement, with AI defining points/paths for themselves to follow over time, while the player has their mouse to do the same)

All this is quite complicated for my brain, and i'd appreciate advice on subjects to research or concepts i should look into for solving this issue myself. I won't say no to more direct explanations, either.

#

Current Code

extends RigidBody2D

# A measure of maximum force applied to push the ship to the destination
@export var max_speed = 50000
# A Measure of force applied to righting the ship back to vertical.
var angular_force = 700

# Force applied to the ship.
var speed = max_speed
var screen_size
var mouse_position = null

# Called when the node enters the scene tree for the first time.
func _ready():
    screen_size = get_viewport_rect().size



func _integrate_forces(delta):
    
    # Get the Current Destination position
    mouse_position = get_global_mouse_position()
    # Figure out what direction it is from the position of this object.
    # (I have no clue why i'm Normalizing it or what it does for me)
    var move_direction = (mouse_position - position).normalized()
    
    # Making the Force Vector that's going to be applied Big. because Speed.
    var force_to_mouse = speed*move_direction
    
    # Applying Torque to the ship multiplied by the angular force,
    # with 0 being "Vertical" and rotation_degrees being how off-vertical it is.
    apply_torque(mass*(0-rotation_degrees)*angular_force)
    
    # Apply the force that moves the ship to the destination to the center of the ship. NO rotation then.
    apply_central_force(force_to_mouse)

solemn kernel
#

the situation going on

rare forge
#

Possibly dumb but could you just add some friction? Something like multiplying the current velocity of the object by 0.99 every tick?

#

(0.99 maybe high so try something lower if that has no noticable effect)

solemn kernel
#

it DOES actually help, though i think the issue is with how i'm trying to handle the movement - With 0.85 dampening on the linear velocity, I now notice the issue of the object 'flying' at a previous position of the mouse when i'm moving it - following the mouse with a delay, I guess? which makes sense when you think about the method of acting on the object. I'm wondering if i should reset the velocity every time _integrate_forces(delta) is called, before the movement is made

#

it does not help with the object jittering on the mouse whenever that gets to happen eventually

rare forge
#

Hmm. I don't then but I hope that leads you to a solution.