libgdx/libgdx

Suggestion to modify code in Sprite.rotate() method in com.badlogic.gdx.graphics.g2d.Sprite package

AnotherCoder1 opened this issue · 4 comments

regarding the method of Sprite.rotate() as mentioned, did you forgot to modulate the angle given with fElapsedTime i.e deltaTime?
When I see the code, I notice that you would just add the current angle with the given angle without any integration with deltaTime.
This is critical, because the function was called every frame. Without this modulation, the angle would increase by a large amount every frame.

I had to integrate two times in my code, so that to establish a fairly acceptable movement of my game object.

Also, that angle that supposed to be supplied to the function need to be in degree as per the documentation.
When I print the value of the angle I got a weird critical value of 1.4370242. It was supposed to be 360 degree but it printed very weird value.

Was this supposed to be a comment somewhere else? It looks like gibberish out of context.

It is indeed an issue at least for me and potentially for some one else that are new to this library.

To give some perspective on this weird behaviour of Sprite.rotate() for my case though, you need to instantiate the minimal
Sprite class and SpriteBatch class.
That's it.

Forgot to mention this phenomena happen in both Android app and desktop app I am trying to build.

Then you draw it in the render() method,
SpriteBatch.begin()
Sprite.draw(SpriteBatch)
SpriteBatch.end()

Then, when handling user input by polling technique, we handle it also in render() method.For example if the user presses right key, we would like to increment the angle by one degree per second in real time.
To do that, we integrate with delta time like so:

(For the sake of this discussion, angle is just one of the property of Game or Screen class being implemented.
And initialize it with 0.0f with float data type.)

angle+=1*deltaTime

Then we pass this angle variable to that Sprite.rotate() method like so:

Sprite.rotate(angle)

Then we try to print the angle's value at runtime:

println(angle)

After compiling the code, I realized that when I pressed the right key it rotates to the left or anti-clockwise.

I noticed two things that are weird.
Firstly, the sprite seems to get faster and faster as as time passes by. Initially, the rotation is not that fast but it gets faster and faster the longer the duration of the key being pressed.

Second, is that, when the angle's value is being monitored in the console for the first full rotation.
It is the value that I mentioned before 1.4xxx is printed out when the sprite is completely being rotated 360 degree anti clockwise.

The value should be 360 degree right? since the documentation says that you pass the angle in degree.

I don't know if Sprite.rotate() is not supposed to be manually called without using the OrthographicCamera instance.

In my case, I don't use that class.
Hopefully, this clear things up.

Regarding this post, is not this post should be in the comment section or an issue?
I don't know for sure.
Because I am not a professional programmer and I do not know too much about GitHub.
If you think it should be in the comment section than it's up to you.

But , I still want to send this message out so that something could be done?
As it is an open source library?
Later tomorrow I will post the code.
So you can see it yourself. I will even give you the image file.

I don't really follow, but if I understand what you're suggesting, Sprite should absolutely not consider delta time for a rotation. If you tell it to rotate by 20 degrees, it rotates by 20 degrees, not some fraction of 20 degrees that depends on the user's machine and workload. There are lots of uses for libGDX that would be fundamentally incompatible with this change, not to mention that it would break all code using Sprite.rotate() currently. Briefly delving into your second comment, you're using rotate(), which is relative, not setRotation(), which is absolute. You're also increasing angle every frame, which would be correct for an absolute change, but not a relative one (unless you want uncontrolled speedup). What you're printing is angle from your code, not the actual angle the Sprite is rotated at. You could use Sprite.getRotation() to find that out. In your code, angle is the relative change between the last rotation and the current rotation, not the current angle of the Sprite.

We have a Discord for this type of question where there isn't actually an issue in libGDX. Here's the link. I'm closing this.

For example if the user presses right key, we would like to increment the angle by one degree per second in real time.
To do that, we integrate with delta time like so: [...]
angle+=1*deltaTime
Then we pass this angle variable to that Sprite.rotate() method like so:
Sprite.rotate(angle)

Sprite#rotate(float) takes the existing rotation into account, so in your case you're applying the angle multiple times. You should use Sprite#setRotation(float) instead. Also, for questions like this, our Discord server is the better place to seek help.