How to update node position while it's "lerping"? (C#)
Asked Answered
P

5

0

Hello!

I'm a beginner and i got stuck on my project trying to smooth the movement of my character using the lerp function. I read the documentation again and made a new simple project with one simple objective : create a circle following the mouse using smoothed motion (exactly like in the doc ; https://docs.godotengine.org/en/stable/tutorials/math/interpolation.html).

The problem is : the circle seems ancred to the origin of its parent node and i do not understand why.

Here is a gif to illustrate my problem:

And here is the code (i'm using C#).

`
using Godot;
using System;

public partial class Balle : CharacterBody2D
{
[Export]
public float speed = 4.0f;

// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
}

// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _PhysicsProcess(double delta)
{

	Godot.Vector2 mousepos = GetLocalMousePosition();

    Position = Position.Lerp(mousepos, (float)delta * speed);

}

public override void _Draw()
{
	Color ballColor = new Color(0.941176f, 0.972549f, 1, 1);

    DrawCircle(Position, 100f, ballColor );

}

}
`

I feel like the line " Position = Position.Lerp(mousepos, (float)delta * speed); ", should update the position of the node during its movement but apparently not.

Can someone please help me? Thank you!

Pointillism answered 16/11, 2023 at 19:54 Comment(0)
M
0

Pointillism Get global mouse position instead of local and draw circle at (0,0).

Maury answered 16/11, 2023 at 20:44 Comment(0)
P
0

So I made some progress but I have not resolved the issue.

I detached my script from the CharacterBody2D node and re-attached it to the first node of the scene (the one that contains all the nodes).

Here is the result :

Which is better. But as you can see, there is still the same issue where te position of the ball does not correspond with the position of the mouse. And it also raises another question... Why do I get different results when I attach the script to another node?

I'm sorry if this is obvious, I'm still struggling with the basic stuff.

Pointillism answered 16/11, 2023 at 20:24 Comment(0)
M
0

Pointillism Get global mouse position instead of local and draw circle at (0,0).

Maury answered 16/11, 2023 at 20:44 Comment(0)
P
0

Maury Thank you for your answer! It works perfectly but if I understand it correctly, it is only moving the drawn circle but not the node position.

For example, if I change the drawn circle by node 2D with the sprite of a circle. I don't think this method would work for moving the sprite in the same way. Am I wrong here?

EDIT: So after a few tries, I realised that your suggestion works perfectly for what I wanted to do. I added a child node with a sprite and it is following the mouse. I'm not sure I completely understand why but it's progress!

Thank you again!

Pointillism answered 17/11, 2023 at 19:26 Comment(0)
M
0

Pointillism For example, if I change the drawn circle by node 2D with the sprite of a circle. I don't think this method would work for moving the sprite in the same way. Am I wrong here?

Yes you are 🙂. Try it on a sprite and see.

Drawing commands work in coordinate space relative to node's position. In other words, node's position is the origin for drawing. Your code updates both (drawing position and drawing origin) at the same time causing weird looking results.

You should either update the node position and always draw at the same place, or keep the node position intact and draw at updated place. Drawing position and node position are two separate but dependent things and you mixed them up because you're using the same term "position" to refer to both.

Example. If you draw a circle with center at (100, 100) on a node whose position is (100, 100), the circle will appear at (200, 200) in global space.

Maury answered 17/11, 2023 at 19:38 Comment(0)
P
0

Maury Oh I get it now! Thanks for taking the time to explain this. 🙂

Pointillism answered 18/11, 2023 at 19:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.