Scale Point, Rotate Point, Separate X & Y scale

  ... the case you feel the need for a new feature or want to submit one.

Scale Point, Rotate Point, Separate X & Y scale

Postby theturtleboy » Fri Jul 02, 2010 5:39 pm

Hey Nicholas,

For my game I sometimes want to scale and rotate objects around points other than their centers. I looked up how you do scaling and rotation in your code, and it seems like it would be a pretty easy fix to add these features (this is from RectangularShape):

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.         protected void applyRotation(final GL10 pGL) {
  3.                 // TODO Offset needs to be taken into account.
  4.                 final float rotation = this.mRotation;
  5.                 if(rotation != 0) {
  6.                         final float halfWidth = this.getBaseWidth() / 2;
  7.                         final float halfHeight = this.getBaseHeight() / 2;
  8.  
  9.                         pGL.glTranslatef(halfWidth, halfHeight, 0);
  10.                         pGL.glRotatef(rotation, 0, 0, 1);
  11.                         pGL.glTranslatef(-halfWidth, -halfHeight, 0);
  12.                 }
  13.         }
  14.  
  15.         @Override
  16.         protected void applyScale(final GL10 pGL) {
  17.                 final float scale = this.mScale;
  18.                 if(scale != 1) {
  19.                         final float halfWidth = this.getBaseWidth() / 2;
  20.                         final float halfHeight = this.getBaseHeight() / 2;
  21.  
  22.                         pGL.glTranslatef(halfWidth, halfHeight, 0);
  23.                         pGL.glScalef(scale, scale, 1);
  24.                         pGL.glTranslatef(-halfWidth, -halfHeight, 0);
  25.                 }
  26.         }
  27.  
Parsed in 0.012 seconds, using GeSHi 1.0.8.4


It looks like you would just need to change the arguments for glTranslatef and glScalef. I was also wondering if you could add separate scales in the x and y direction. Let me know!
theturtleboy
 
Posts: 12
Joined: Tue Jun 29, 2010 4:39 pm

Re: Scale Point, Rotate Point, Separate X & Y scale

Postby Nicolas Gramlich » Sat Jul 03, 2010 2:05 pm

Hi,

this.mScale :arrow: this.mScaleX and this.mScaleY :check:

Changeing the rotation center should be not much more than:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. final float halfWidth = this.getBaseWidth() / 2;
Parsed in 0.010 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. final float rotationCenterX = this.getRotationCenterX;
Parsed in 0.011 seconds, using GeSHi 1.0.8.4


Will add all this asap. Not sure if I can do it this weekend, because today I host a party as tomorrow is my birthday :)

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

Re: Scale Point, Rotate Point, Separate X & Y scale

Postby Nicolas Gramlich » Sat Jul 03, 2010 2:07 pm

Or if you might have the time to, you could try to change (and test them in a new Example maybe?) those functions, so I could drop them into AndEngine as soon as I have the time to. 8-)

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

Re: Scale Point, Rotate Point, Separate X & Y scale

Postby theturtleboy » Mon Jul 05, 2010 4:32 pm

I'm playing around with this now, I'll let you know how I do.

Also, HAPPY BIRTHDAY! Thanks for all of the hard work you've put into this engine, it's amazing.
Last edited by theturtleboy on Mon Jul 05, 2010 9:33 pm, edited 1 time in total.
theturtleboy
 
Posts: 12
Joined: Tue Jun 29, 2010 4:39 pm

Re: Scale Point, Rotate Point, Separate X & Y scale

Postby theturtleboy » Mon Jul 05, 2010 8:03 pm

Alright, I think I got it working. I cloned the repository and added my changes to my clone. I altered some of your examples for testing purposes, but there were a few funny things that I had to fix (for example, I had to add some weird arguments to the doAsync in BaseBenchmark). Also, I just altered existing examples instead of making new ones, but of course feel free to use my example code if you really want to :-). Some of the examples will have to be modified to account for scales in both directions, but that should be an easy enough fix.

From SpriteExample, shows the rotate and scale point works:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         @Override
  2.         public Scene onLoadScene() {
  3.                 this.getEngine().registerPreFrameHandler(new FPSLogger());
  4.  
  5.                 final Scene scene = new Scene(1);
  6.                 scene.setBackgroundColor(0.09804f, 0.6274f, 0.8784f);
  7.  
  8.                 /* Calculate the coordinates for the face, so its centered on the camera. */
  9.                 final int x = (CAMERA_WIDTH) / 2;
  10.                 final int y = (CAMERA_HEIGHT) / 2;
  11.                
  12.                 /* Create the face and add it to the scene. */
  13.                 final Sprite face = new Sprite(x, y, this.mFaceTextureRegion);
  14.                 face.setScaleX(4);
  15.                 face.setScaleY(4);
  16.                 scene.getTopLayer().addEntity(face);
  17.                 face.setRotatePointX(0);
  18.                 face.setRotatePointY(0);
  19.                 face.setScalePointX(0);
  20.                 face.setScalePointY(0);
  21.                
  22.                 face.addShapeModifier(new RotateByModifier(60, 3600));
  23.  
  24.                 return scene;
  25.         }
  26.  
Parsed in 0.012 seconds, using GeSHi 1.0.8.4


From ShapeModifierExample (only real change is the scale values):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         @Override
  2.         public Scene onLoadScene() {
  3.                 this.getEngine().registerPreFrameHandler(new FPSLogger());
  4.  
  5.                 final Scene scene = new Scene(1);
  6.                 scene.setBackgroundColor(0.09804f, 0.6274f, 0.8784f);
  7.  
  8.                 final int x = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
  9.                 final int y = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;
  10.  
  11.                 final Rectangle rect = new Rectangle(x + 100, y, 32, 32);
  12.                 rect.setColor(1, 0, 0);
  13.                 rect.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
  14.  
  15.                 final AnimatedSprite face = new AnimatedSprite(x - 100, y, this.mFaceTextureRegion);
  16.                 face.animate(100);
  17.  
  18.                 final SequenceModifier shapeModifier = new SequenceModifier(
  19.                         new IModifierListener() {
  20.                                 @Override
  21.                                 public void onModifierFinished(final IShapeModifier pShapeModifier, final Shape pShape) {
  22.                                         ShapeModifierExample.this.runOnUiThread(new Runnable() {
  23.                                                 @Override
  24.                                                 public void run() {
  25.                                                         Toast.makeText(ShapeModifierExample.this, "Sequence ended.", Toast.LENGTH_LONG).show();
  26.                                                 }
  27.                                         });
  28.                                 }
  29.                         },
  30.                         new RotateByModifier(2, 90),
  31.                         new AlphaModifier(2, 1, 0),
  32.                         new AlphaModifier(1, 0, 1),
  33.                         new ScaleModifier(2, 1, 0.5f, 1, 0.5f),
  34.                         new DelayModifier(0.5f),
  35.                         new ParallelModifier(
  36.                                         new ScaleModifier(3, 0.5f, 5, 0.1f, 10),
  37.                                         new RotateByModifier(3, 90)
  38.                         ),
  39.                         new ParallelModifier(
  40.                                         new ScaleModifier(3, 5, 1, 10, 1),
  41.                                         new RotateModifier(3, 180, 0)
  42.                         )
  43.                 );
  44.  
  45.                 face.addShapeModifier(shapeModifier);
  46.                 rect.addShapeModifier(shapeModifier.clone());
  47.  
  48.                 scene.getTopLayer().addEntity(face);
  49.                 scene.getTopLayer().addEntity(rect);
  50.  
  51.                 return scene;
  52.         }
  53.  
Parsed in 0.014 seconds, using GeSHi 1.0.8.4
theturtleboy
 
Posts: 12
Joined: Tue Jun 29, 2010 4:39 pm

Re: Scale Point, Rotate Point, Separate X & Y scale

Postby Nicolas Gramlich » Mon Jul 05, 2010 10:22 pm

Hi,

great could you send me the changes to AndEngine or would you like to submit a branch to the Google Code project :?:
I'd prefer to merge it in myself, to keep a clean coding-style.

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

Re: Scale Point, Rotate Point, Separate X & Y scale

Postby theturtleboy » Tue Jul 06, 2010 4:08 pm

If you go under source->clones on the AndEngine google code site you should see my clone at the top (theturtleboy). If you're having trouble finding the specific file changes or anything, let me know!
theturtleboy
 
Posts: 12
Joined: Tue Jun 29, 2010 4:39 pm

Re: Scale Point, Rotate Point, Separate X & Y scale

Postby Nicolas Gramlich » Tue Jul 06, 2010 11:48 pm

Hi,

great :)

While I merged most of your changes, I decided not to add the ones on the BaseSplashActivity and the SplashScene, as I think in 99,9% of the cases developers will want to scale x and y the same.
Also I added some constructors/getters/setters in some of the related classes, i.e. to remain the old possibiliy of scaling x and y the same.
One thing to notice are the new classes RotationAtModifier and ScaleAtModifier which are both pretty handy and are used in the following example...

I made this example which shows all those new feature pretty good:
http://code.google.com/p/andengineexamp ... ample.java

Thanks again for your contribution :)

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


Return to Features

Who is online

Users browsing this forum: No registered users and 5 guests