Jump to content
  • Advertisement
Sign in to follow this  
manderin87

Polygon rotation is too fast compared to objects rotation

This topic is 2182 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am designing a game in android and I have a problem when updating a polygon that is used for collision detection. When the objects update method is called it rotates the object by .5 and then updates the polygon by rotating it by the same amount. The problem is on the screen the polygon is rotating faster than the object when drawn. The polygon is rotating so fast that it looks like a star, eventually it slows down and then speeds up again. It will not stay in step with the objects rotation.

This is the rotation code for the polygon.


public void rotate(float x0, float y0, double angle) {
for(int i = 0; i < mOrigins.size(); i++) {
Point point = mOrigins.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 npoint = mPoints.get(i);
npoint.x = x;
npoint.y = y;

mPoints.set(i, npoint);
}

}


This is the update method for the object


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();
}
}
}


Trying to limit the time has no effect on the polygon and it continues to speed up and slow down.

Can anyone please identify the error or lead me in the right direction to fix this?

thanks

Share this post


Link to post
Share on other sites
Advertisement
Where is mDegree set?
Are they invoking the same rotate()? (The parameters could have default values)
If not, what does the other one look like?
The rest looks good.

Share this post


Link to post
Share on other sites
If mPoints and mOrigins are linked (the same values),
It will exhibit animating behavior when run multiple times (with the same rotation value),
Otherwise it will behave as it is setting the angle.
If it does the former but you give it an angle rather than an angle increment, it will produce an alternating slow to fast rotation.

It seems you don't give it an increment; now are the two point lists bound together outside the rotate(...) function?

Share this post


Link to post
Share on other sites
The mOrigins is just the original points set when the polygon was created, I use mPoints to keep track of the curent points after the rotation is applied. The two lists only exsist inside the polygon class. The only variable that is supposed to change is the degree, to rotate the polygon around the x and y points. The method as it is now is making the alternating slow to fast rotation instead of just rotating the polygon by the degree.

Share this post


Link to post
Share on other sites
Well in that case I'm out.
There's probably a trig genius in here who can point the error to the rotation math, then.

Share this post


Link to post
Share on other sites
I think your code is increasing mDegree by 0.5 each frame, and mDegree is the amount that you're rotating your object each frame. So after 100 frames you're rotating by 50 degrees per frame. Eventually, you're rotating so fast that it's starting to slow down and reverse.

Edit : Never mind - just noticed that you had a separate store for the original positions (mOrigins). Any chance that float deltaTime is incorrect, and is actually the total time elapsed? Edited by C0lumbo

Share this post


Link to post
Share on other sites

I think your code is increasing mDegree by 0.5 each frame, and mDegree is the amount that you're rotating your object each frame. So after 100 frames you're rotating by 50 degrees per frame. Eventually, you're rotating so fast that it's starting to slow down and reverse.

Yes, but how is this possible if mOrigins (or the elements contained within) does not attain state of mPoints?

Share this post


Link to post
Share on other sites
mDegree should be how much to rotate by this frame. It should not grow with time. What rotate() is doing is increase the rotation speed.

EDIT: Wait... I think I see what the others are seeing now. Is it possible that mOrigins and mPoints are actually the same thing?

FURTHER EDIT: What does this line do?
Point npoint = mPoints.get(i);[/quote] Edited by Álvaro

Share this post


Link to post
Share on other sites

Trying to limit the time has no effect on the polygon and it continues to speed up and slow down.

Your framerate is messing with the rotation, which would explain the speed-up and slow-down. I think this is your problem:

mAccumulatedTime += deltaTime;
while(mAccumulatedTime > FIXED_FRAME_TIME) {
mAccumulatedTime -= FIXED_FRAME_TIME;
[/quote]
You add the delta time (elapsed since last frame I'm assuming) and then loop during the frame redrawing and rotating until you get something roughly equivalent to the framerate you wanted? That's a lot of unnecessary work.

Decide on a rotation rate (deg/rad per sec) and multiply that by deltaTime in seconds to get the amount that it should be rotated this frame. Or in milliseconds, or minutes, I'm not trying to make that a particular sticking point, just make sure your units match.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!