Rotation + RotationAtModifer

  ... in the unlikely case you discovered a bug, post it here.

Rotation + RotationAtModifer

Postby photek » Fri Sep 10, 2010 1:35 am

Hi, I thought it'd be kinda cool to create a sprite that moves around like those Ferris wheels (excuse the fatman and the one bloke dangling on a rope) that you see at a fair. So the first thing that came to my mind is to use a Shape Modifier. I thought I could use two shape modifiers: one modifier to rotate the sprite around a center-point on the screen, and another modifier rotate it in the opposite direction to make sure the top of the sprite always points up.

The desired effect is to make the sprite rotate in a circle like one of those carts in the ferris wheel (i.e. the sprite moves around in a circle, but the sprite does not rotate upside down and dump the people out of the ferris wheel ).

However, when I run my code (below) it looks like only the RotationAtModifier takes effect. So you only see the sprite rotate in 360 degrees around a certain x y coordinate, and the Sprite becomes upside down at the 180' point. I'm hoping to be using these two modifiers in parallel for a lot of things, so I was hoping this is just me mis-using the system.

Here's the code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Ferris extends Sprite {
  2.     public  Ferris(final float pX, final float pY, final TextureRegion pTextureRegion) {
  3.         //...
  4.         this.addShapeModifier(
  5.             new ParallelModifier<IShape>(
  6.                 new RotationModifier(
  7.                     10,                 // duration
  8.                     0,                  // angle start                
  9.                     -360                // angle end
  10.                 ),
  11.                 new RotationAtModifier(
  12.                     10,                 // duration
  13.                     0,                  // angle start
  14.                     360,                // angle end
  15.                     screenCenterX,      // coordinates relative to sprite
  16.                     screenCenterY,      // coordinates relative to sprite
  17.                 )
  18.             )  
  19.         );
  20.         //...
  21.     }
  22. }
Parsed in 0.013 seconds, using GeSHi 1.0.8.4

screenCenter can be anything non-zero, if you want to test out the above code..

A quick description of the code, the following shape modifiers are used:
- RotationModifier to rotate -360 degrees (in place)
- RotationAtModifier to rotate 360 degrees (at the screen center)
- The above two modifiers are placed in a ParallelModifier, so that both effects are applied to the shape simultaneously.

I did take a peak at the internals, and it looks like the RotationAtModifier object sets the Sprite's mRotationCenter coordinates, which will affect the RotationModifier when he has his turn at updating the Sprite's position for each engine tick. But again, hopefully its just my misunderstanding and I used these components incorrectly.

Please let me know and thanks in advance.

Cya~
:)
photek
 
Posts: 32
Joined: Fri Sep 03, 2010 8:24 pm

Re: Rotation + RotationAtModifer

Postby Nicolas Gramlich » Mon Sep 13, 2010 10:43 am

Hi photek,

the Modifiers only modify the values of the Shapes on every frame, so you can only use one Modifier for one value at a time.

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Re: Rotation + RotationAtModifer

Postby Nicolas Gramlich » Mon Sep 13, 2010 10:44 am

To do sth like that you'd have to subclass Sprite(or whatever class you are using and @Override onApplyRotation)

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Re: Rotation + RotationAtModifer

Postby photek » Tue Sep 14, 2010 12:18 am

Hi Nicolas and thanks for your response. :)

In order to achieve the desired effect, I'm quite uncertain of how best to override onApplyRotation. Could you kindly suggest what could be done in the onApplyRotation method?

If I had to take a shot at it, I wouldn't touch onApplyRotation. I would have subclassed the Sprite class to have the following parameters:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class SpriteFreeRotation extends Sprite {
  2.         //...
  3.         private float mRotation;
  4.         private float mRotationAt;
  5.         private float mRotationAtCenterX;
  6.         private float mRotationAtCenterY;
  7.         //...
  8. }
Parsed in 0.010 seconds, using GeSHi 1.0.8.4

Where:
mRotation - specifies the current angle of rotation WRT sprite center
mRotationAt - specifies the current angle of rotation WRT mRotationAtCenter* coordinates
mRotationAtCenterX - specifies the X coordinate used to rotate mRotationAt around
mRotationAtCenterY - specifies the Y coordinate used to rotate mRotationAt around

However, if I go this route, how do I allow AndEngine to recognise the parameters and render the SpriteFreeRotation to respect these new parameters? This added flexibility, I think, would improve the expressiveness of sprite movement in the AndEngine, so I'm wondering how to safely modify things so that it could be pushed back to the main repo.

Or is there another motion mechanism we could rely upon?

Thanks in advance.
photek
 
Posts: 32
Joined: Fri Sep 03, 2010 8:24 pm

Re: Rotation + RotationAtModifer

Postby timothyleerussell » Tue Sep 14, 2010 1:51 am

You could do it with the physics engine. Create a big wheel and pin the buckets to it with a joint at the top middle of each bucket.

The buckets should stay parallel to the ground.
timothyleerussell
 
Posts: 19
Joined: Wed Sep 08, 2010 5:57 pm

Re: Rotation + RotationAtModifer

Postby Nicolas Gramlich » Wed Sep 15, 2010 11:44 am

Hi,

you would do like Timothy said or like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         protected void applyRotation(final GL10 pGL) {
  2.                 super.applyRotatio()
  3.  
  4.                 final float rotation2 = this.mRotation2;
  5.  
  6.                 if(rotation2 != 0) {
  7.                         final float rotationCenterX2 = this.mRotationCenterX2;
  8.                         final float rotationCenterY2 = this.mRotationCenterY2;
  9.  
  10.                         pGL.glTranslatef(rotationCenterX2, rotationCenterY2, 0);
  11.                         pGL.glRotatef(rotation2, 0, 0, 1);
  12.                         pGL.glTranslatef(-rotationCenterX2, -rotationCenterY2, 0);
  13.                 }
  14.         }
Parsed in 0.010 seconds, using GeSHi 1.0.8.4


Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Re: Rotation + RotationAtModifer

Postby photek » Thu Sep 16, 2010 12:01 pm

Thanks. I'm working on an unrelated project ATM, but I'll get to implement your suggestions sometime this week. Thank you both for your help! You guys rule. :)
photek
 
Posts: 32
Joined: Fri Sep 03, 2010 8:24 pm

Re: Rotation + RotationAtModifer

Postby yatuor » Wed Jun 13, 2012 4:42 am

this method really works, thanks nico!!! it helps much in my current project.
i was trying to parallel 2 RotationAtModifier, but it did not work.

Nicolas Gramlich wrote:Hi,

you would do like Timothy said or like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         protected void applyRotation(final GL10 pGL) {
  2.                 super.applyRotatio()
  3.  
  4.                 final float rotation2 = this.mRotation2;
  5.  
  6.                 if(rotation2 != 0) {
  7.                         final float rotationCenterX2 = this.mRotationCenterX2;
  8.                         final float rotationCenterY2 = this.mRotationCenterY2;
  9.  
  10.                         pGL.glTranslatef(rotationCenterX2, rotationCenterY2, 0);
  11.                         pGL.glRotatef(rotation2, 0, 0, 1);
  12.                         pGL.glTranslatef(-rotationCenterX2, -rotationCenterY2, 0);
  13.                 }
  14.         }
Parsed in 0.010 seconds, using GeSHi 1.0.8.4


Best Regards,
Nicolas
yatuor
 
Posts: 10
Joined: Sat Aug 06, 2011 10:23 am


Return to Bugs

Who is online

Users browsing this forum: No registered users and 8 guests