Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Polygon rotation is too fast compared to objects rotation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
36 replies to this topic

#21 manderin87   Members   -  Reputation: 130

Like
0Likes
Like

Posted 29 November 2012 - 04:09 PM

While trying to check, i made the rotation manual.... the polygon is still rotating faster. Its not the framerate, its the way the polygon is rotating using the polygon rotation method above or the method used to rotate the sprite.

Sponsor:

#22 manderin87   Members   -  Reputation: 130

Like
0Likes
Like

Posted 29 November 2012 - 04:14 PM

There is something wrong with the polygon rotation method... I watched the degrees subtract and the polygon was still rotating the other way.. as the degree increased it went left as the degree decreased it still went left, instead of right.

Edited by manderin87, 29 November 2012 - 04:18 PM.


#23 BCullis   Crossbones+   -  Reputation: 1813

Like
0Likes
Like

Posted 29 November 2012 - 04:18 PM

Please post both rotation methods and their calling context.

Additionally, what is responsible for calculating deltaTime? Is it the elapsed time since last frame?
Hazard Pay :: FPS/RTS in SharpDX
DeviantArt :: Because right-brain needs love too

#24 manderin87   Members   -  Reputation: 130

Like
0Likes
Like

Posted 29 November 2012 - 04:31 PM

The polygon rotation method is

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 BCullis   Crossbones+   -  Reputation: 1813

Like
0Likes
Like

Posted 29 November 2012 - 04:42 PM

Your update method:

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.

Hazard Pay :: FPS/RTS in SharpDX
DeviantArt :: Because right-brain needs love too

#26 manderin87   Members   -  Reputation: 130

Like
0Likes
Like

Posted 29 November 2012 - 04:46 PM

They both recieve the same mDegree value.

Could it be something with the way i move the degree past 0?

if(mDegree > 360) {
	  mDegree = 0;
  } else if(mDegree < 0) {
   mDegree = 360;
  }


#27 de_mattT   Members   -  Reputation: 308

Like
0Likes
Like

Posted 29 November 2012 - 04:49 PM

Does the SpriteBatch draw method deffinitely take the angle in degrees and not radians?

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 manderin87   Members   -  Reputation: 130

Like
0Likes
Like

Posted 29 November 2012 - 05:22 PM

the draw method i use is above, but spritebatch deffinitely uses degrees

its located here https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/g2d/SpriteBatch.java

#29 manderin87   Members   -  Reputation: 130

Like
0Likes
Like

Posted 29 November 2012 - 05:34 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;
  }


#30 de_mattT   Members   -  Reputation: 308

Like
0Likes
Like

Posted 29 November 2012 - 05:52 PM

Is this a funciton you've defined yourself "Utilities.toRadians()"? If so can you post the code?
Is there a reason why you aren't using "Math.toRadians()"?

#31 manderin87   Members   -  Reputation: 130

Like
0Likes
Like

Posted 29 November 2012 - 06:38 PM

Its the same thing... my Utilities.toRadians() calls Math.toRadians()

#32 Khatharr   Crossbones+   -  Reputation: 3040

Like
0Likes
Like

Posted 29 November 2012 - 09:00 PM

Does SpriteBatch::draw() want degrees or radians?

Edited by Khatharr, 29 November 2012 - 09:01 PM.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#33 Khatharr   Crossbones+   -  Reputation: 3040

Like
0Likes
Like

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.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#34 manderin87   Members   -  Reputation: 130

Like
0Likes
Like

Posted 29 November 2012 - 09:04 PM

Does SpriteBatch::draw() want degrees or radians?


Degrees

#35 manderin87   Members   -  Reputation: 130

Like
0Likes
Like

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 Khatharr   Crossbones+   -  Reputation: 3040

Like
0Likes
Like

Posted 29 November 2012 - 09:12 PM

Okay, looking back over the whole thread, can you post the whole code path for this thing? Seems like it'll be faster than everyone guessing at it.

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.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

#37 manderin87   Members   -  Reputation: 130

Like
0Likes
Like

Posted 30 November 2012 - 01:11 PM

Ive rechecked everything... The rotation code was the problem.... I made two lists to keep track of the points... the original points and the rotated points. Both were pointing to the same object, i thought they were seperate, but they were not... so I just made both lists create a new point when adding to the list. The rotation is now correct.

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.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS