Polygon rotation is too fast compared to objects rotation
#22 Members - Reputation: 130
Posted 29 November 2012 - 04:14 PM
Edited by manderin87, 29 November 2012 - 04:18 PM.
#24 Members - Reputation: 130
Posted 29 November 2012 - 04:31 PM
public void rotate(float x0, float y0, double angle) {
Log.i("Polygon", "Degree: " + angle);
for(int i = 0; i < mPoints.size(); i++) {
Point point = mPoints.get(i);
float x = (float) (x0 + (point.x - x0) * Math.cos(Utilities.toRadians(angle)) -
(point.y - y0) * Math.sin(Utilities.toRadians(angle)));
float y = (float) (y0 + (point.x - x0) * Math.sin(Utilities.toRadians(angle)) +
(point.y - y0) * Math.cos(Utilities.toRadians(angle)));
point.x = x;
point.y = y;
}
}
The sprite rotation is done through a libgdx spritebatch and I am certain its correct.
public void draw(SpriteBatch batch, float x, float y, float degree) {
batch.draw(mImage, x - width() / 2, y - height() / 2,
pivotX(), pivotY(),
width(), height(),
mScale, mScale,
degree,
0, 0,
(int) width(), (int) height(),
false, false);
}
The delta time comes from libgdx and is the time since last frame.
float deltaTime = Gdx.graphics.getDeltaTime();
I am almost certain its something in the polygon rotation code... I was testing it... it would rotate until a certain point and then go the other way.
Edited by manderin87, 29 November 2012 - 04:34 PM.
#25 Members - Reputation: 1683
Posted 29 November 2012 - 04:42 PM
public void update(float deltaTime) {
mAccumulatedTime += deltaTime;
while(mAccumulatedTime > FIXED_FRAME_TIME) {
mAccumulatedTime -= FIXED_FRAME_TIME;
if(isActive()) {
rotate();
mBounding.rotate(mPosition.x, mPosition.y, mDegree);
drawBounding();
}
}
}
What does rotate() do? Because by your explanation, mBounding.rotate handles the collision object rotation, and the sprite for the object is rotated by the spritebatch call.
If the sprite is being rotated by a spritebatch call, you should be giving it the same degree value that mBounding.rotate receives. Is that the case?
Edited by BCullis, 29 November 2012 - 04:43 PM.
#27 Members - Reputation: 265
Posted 29 November 2012 - 04:49 PM
Could you show us how SpriteBatch uses mDegree?
Is this the same SpriteBatch method as referred to here?
http://tylerforsythe.com/2011/11/xna-spritebatch-draw-and-icecream-use-a-rotation-value-in-radians/
On second thoughts I see that is a different SpriteBatch
Edited by de_mattT, 29 November 2012 - 05:09 PM.
#28 Members - Reputation: 130
Posted 29 November 2012 - 05:22 PM
its located here https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/g2d/SpriteBatch.java
#33 Members - Reputation: 1677
Posted 29 November 2012 - 09:04 PM
This method is better for fixing the degree but it didnt change the behavior that I am experiencing
if(mDegree >= 360) { mDegree -= 360; } else if(mDegree < 0) { mDegree += 360; }
Try:
[source lang="java"]while(mDegree >= 360) {mDegree -= 360;}while(mDegree < 0) {mDegree += 360;}[/source]
Edit - Aagh, derp. Sorry for double post.
Edited by Khatharr, 29 November 2012 - 09:05 PM.
There are ten kinds of people in this world: those who understand binary and those who don't.
#35 Members - Reputation: 130
Posted 29 November 2012 - 09:11 PM
while(mDegree >= 360) {mDegree -= 360;}
while(mDegree < 0) {mDegree += 360;}
Didnt fix it. I know it has to be something wrong with the polygone rotation, its not working correctly. Ive stared at it for hours and i dont know what the problem is. Ive checked several places and the algorithm seems to be correct, but its not rotating correctly.
#36 Members - Reputation: 1677
Posted 29 November 2012 - 09:12 PM
Didnt fix it. I know it has to be something wrong with the polygone rotation, its not working correctly. Ive stared at it for hours and i dont know what the problem is. Ive checked several places and the algorithm seems to be correct, but its not rotating correctly.
Yeah, that wasn't intended as a fix. Just a more complete solution for rotating something into range without modulation.
Edited by Khatharr, 29 November 2012 - 09:13 PM.
There are ten kinds of people in this world: those who understand binary and those who don't.
#37 Members - Reputation: 130
Posted 30 November 2012 - 01:11 PM
Here is the polygon constructor
public Polygon(Point... points) {
if(points.length < 3) {
throw new IllegalArgumentException("polygons must contain at least 3 points.");
}
for(Point p : points) {
mRotatedPoints.add(new Point(p));
mOriginalPoints.add(new Point(p));
}
}
And the rotation code.
public void rotate(float x0, float y0, float degree) {
for(int i = 0; i < mOriginalPoints.size(); i++) {
Point point = mOriginalPoints.get(i);
float x = (float) (x0 + (point.x - x0) * Math.cos(Utilities.toRadians(degree)) -
(point.y - y0) * Math.sin(Utilities.toRadians(degree)));
float y = (float) (y0 + (point.x - x0) * Math.sin(Utilities.toRadians(degree)) +
(point.y - y0) * Math.cos(Utilities.toRadians(degree)));
Point p = mRotatedPoints.get(i);
p.x = x;
p.y = y;
}
thanks everyone for the help and forgive my idiocy for not noticing they were they same instead of being seperate.
Edited by manderin87, 30 November 2012 - 01:12 PM.






