#Physics movement of worm/snake character

36 messages · Page 1 of 1 (latest)

sage bay
#

Hello, I am trying to create a kind of worm/snake character that has physics with the rest of the body and follows the mouse from the tip(head)

I’ve created rigidbody2Ds and connected them with pinpoints and it seems like it’s working but if I try to move the entire body by only moving the tip, it doesn’t work.

I also tried this with skeleton2D and bones but the skeleton modification stack is a little confusing to me.

(Attached pics for maybe better understanding of what I’m going for)

naive pollen
#

would need to know exaclty what is and what isnt working. what happens if you move the mouse?

Although i can say this: you shouldnt modify positions of rigidbodies directly, because that breaks the internal physics calculations. Its better to apply a force or an impulse ( in direction to the mouse pointer). And you should do it in _phyiscs_process()

https://docs.godotengine.org/en/stable/tutorials/physics/physics_introduction.html#rigidbody2d

sage bay
#

That’s good to know, do you think I’m going about this the right way? Using rigidbodies, or maybe skeleton2D with bones would be better?

To answer your original question, if I use lerp() to move the head towards my mouse it just completely breaks the joint and the head stars following my mouse but the body is just stating still. I’ll put a gif or video here soon I’m away from my pc.

sage bay
#

heres a gif, its the same setup just different sprites. the end goal is that this will be a frogs tongue that will have physics and follow the mouse. I haven't tried changing the code to _phyiscs_process() yet, but i will when i have some time

naive pollen
#

for testing it's also a good idea to disable Can Sleep for all Rigidibodies. Sometimes it can mess with the simulation, especially if its more complex

sage bay
#

so i tried using _physics_process() but i cant find any functions to get the desired outcome.

sage bay
#

okay its working much better but the sprites separating is not the best. Probably a way i can fix it though.

if anyone stumbles across this thread the solution was rigidbody2d with pinjoints inbetween and this script only added to the head of the chain, or whatever youre trying to move.

func _physics_process(delta): var velocity = (get_global_mouse_position() - global_position) * delta * 10.0 move_and_collide(velocity)

#

Physics movement of worm/snake character

naive pollen
#

if this script is on a rigidbody try this:

func _physics_process(delta):
    apply_central_force((get_global_mouse_position() - global_position) * delta * 10.0)
#

move_and_collide will also mess with physics calculations of rigidbodies

sage bay
#

ahhhh much better. that fixed it

#

wait so is move_and_collide only for a characterbody2d?

naive pollen
#

yep, but since godot 4 you can use it on rigidbodies too for some reason

#

but not a good idea if you dont know exactly what you are doing

sage bay
#

oh wait yes, i see now in the doc gddeadinside

#

yeah i misread

naive pollen
#

and not the center of that whole rigidbody

sage bay
#

I have the script only on the head does that change anything whether or not apply_force or apply_central_force are used?

naive pollen
#

is the head just the circle, or the circle plus a line?

sage bay
#

these are the sprites

#

so if apply_force was only applied to the circle end

naive pollen
#

yea currently it would be applied here

#

if that is also the center (origin) in your scene

#

you could of course just make the head a circle only, thats probably easiest

sage bay
#

yeah thats probably what ill end up doing

#

eventually the end goal of this is to have a frog character and then when clicking the tongue comes out of his mouth but i wanted it to have physics properties I guess the next hard part will be somehow instantiating the tongue on click and apply the force, then apply the opposite force on release (to retract the tongue) and hide it 🤔 does this sound like it could work or nah

naive pollen
#

retracting it could get kinda tricky i'd assume but totally possible with some little adjustments i would think

sage bay
#

yeah i guess thats the next challenge

#

ty for all the help though super appreciate it

naive pollen
# sage bay ty for all the help though super appreciate it

you are welcome!
about the retracting: I would try to pull on last segment until it's behind the frogs head, then freeze it or set some high dampening value or something so it wont move (much) anymore and then pull on the second to last segment and so on. that should give a nice retraction visual

sage bay
#

ohhh okay i see, just go one by one