AndEngine and smooth rotate sprite - android

I want to smooth rotate my spirtes. I have 6 sprites and I want to rotate them on 360.
This is my code:
unactiveTimerWaitingForPlayersTxtSprite
.registerUpdateHandler(timerToUnactiveTimerWaitingTxt);
TimerHandler timerToUnactiveTimerWaitingTxt = new TimerHandler(0.1f, true,
new ITimerCallback() {
public void onTimePassed(TimerHandler pTimerHandler) {
unactiveTimerWaitingForPlayersTxtSprite
.setRotation(unactiveTimerWaitingForPlayersTxtSprite
.getRotation() - ANGLE_WAITING_TXT);
}
});
This is code for one sprite. For all my sprites I do that in the same way. When I run application on Asus transformer 700 rotate of objects is not smooth. I add this code to engine:
engineOptions.getRenderOptions().setDithering(true);
engineOptions.getRenderOptions().setMultiSampling(true);
but still the same effect. There is any way to smooth rotate objects?

Use Modifiers: In this case you are looking for the RotationModifier.
If you want to rotate the sprites using custom TimerHandlers then you have to multiply the angle change (ANGLE_WAITING_TXT) with the elapsed time in order to have a smooth animation.

Related

Panning the view of a gameObject instead of the camera in Unity3d?

I'm having a hard time to pan a view of a gameObject in Unity3d. I'm new to scripting and I'm trying to develop an AR (Augmented Reality) application for Android.
I need to have a gameObject (e.g. a model of a floor), from the normal top down view, rendered to a "pseudo" iso view, inclined to 45 degrees. As the gameObject is inclined, I need to have a panning function on its view, utilizing four (4) buttons (for left, right, forward(or up), backward(or down)).
The problem is that, I cannot use any of the known panning script snippets around the forum, as the AR camera has to be static in the scene.
Need to mention that, I need the panning function to be active only at the isometric view, (which I already compute with another script), not on top down view. So there must be no problem with the inclination of the axes of the gameObject, right?
Following, are two mockup images of the states, the gameObject (model floor) is rendered and the script code (from Unity reference), that I'm currently using, which is not very much functional for my needs.
Here is the code snippet, for left movement of the gameObject. I use the same with a change in -, +speed values, for the other movements, but I get it only move up, down, not forth, backwards:
#pragma strict
// The target gameObject.
var target: Transform;
// Speed in units per sec.
var speedLeft: float = -10;
private static var isPanLeft = false;
function FixedUpdate()
{
if(isPanLeft == true)
{
// The step size is equal to speed times frame time.
var step = speedLeft * Time.deltaTime;
// Move model position a step closer to the target.
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}
static function doPanLeft()
{
isPanLeft = !isPanLeft;
}
It would be great, if someone be kind enough to take a look at this post, and make a suggestion on how this functionality can be coded the easiest way, as I'm a newbie?
Furthermore, if a sample code or a tutorial can be provided, it will be appreciated, as I can learn from this, a lot. Thank you all in advance for your time and answers.
If i understand correctly you have a camera with some fixed rotation and position and you have a object you want to move up/down/left/right from the cameras perspective
To rotated an object to a set of angles you simply do
transform.rotation = Quaternion.Euler(45, 45, 45);
Then to move it you use the cameras up/right/forward in worldspace like this to move it up and left
transform.position += camera.transform.up;
transform.position -= camera.transform.right;
If you only have one camera in your scene you can access its transform by Camera.main.transform
An example of how to move it when someone presses the left arrow
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
transform.position -= camera.transform.right;
}

AndEngine and rotate object

There is something what I can use to rotate object without ending? I want start game and start rotate sprite with some speed but I don't want to end rotate. There is any way to use: new RotationModifier(20f, 0, 360), but with no end of rotation? Or there is any other way?
I think you can use LoopEntityModifier, something like (haven't tried it out):
LoopEntityModifier EntityModifier = new LoopEntityModifier(
new RotationModifier(1,0,360));

How to get maximum performance in Libgdx 2D animation?

I have a texture pack made of 9 images.
Out of the 9 images, I am using 3 of them as animation frames. So my animation is made of 3 images.
The images have a fixed resolution of 128x128.
TextureRegion sprite1 = atlas.findRegion("sprite1");
TextureRegion sprite2 = atlas.findRegion("sprite2");
TextureRegion sprite3 = atlas.findRegion("sprite3");
...
...
Animation spriteAnimation = new Animation(.130f,new TextureRegion[sprite1,sprite2,sprite3]);
// similarly I am creating 4 more animation instance using same 3 regions
Animation spriteAnimation2 = new Animation(.130f,new TextureRegion[sprite1,sprite2,sprite3]);
...
...
Game loop :-
{
update();
render();
}
I have following code in update() and render().
update()
{
...
// because images are of fixed resolution 128x128
// I will calculate the scale factor as per the device screen
// so for each device width and height will vary..
// GameUnits is giving scaled width and height need for device
x = get new x;
y = get new y;
width = GameUnits.getWidth();
height = GameUnits.getHeight();
// similarly updating 4 more width & height, each of them are different.
...
...
}
render()
{
...
spriteBatch.begin();
spriteBatch.draw(spriteAnimation.getKeyFrame(totalTime),x,y,width1,height1);
// similarly rendering total of 5 animation
spriteBatch.draw(spriteAnimation2.getKeyFrame(totalTime),x,y,width2,height2);
...
spriteBatch.end();
...
}
Now, update() method is taking 0.045ms on average, whereas render() method is taking 16ms on average, giving me FPS of ~60.
At some instant render() method takes 22ms or high or low and hence creating lags. Render method has same work load every frame, i.e to render 5 animation.
I am new to libgdx without any prior experience in gaming.
May I know what mistake I made in implementing Animations?
The update() code is working well for me, but it looks like there are serious holes in render().

How to create a circular loop of animations in AndEngine?

I want to do an "chain" or circular loop of animations as can be described below:
LABEL start
do Anim1->Anim2->Anim3->Anim4
GOTO start
The above will do a circular loop: Anim1->Anim2->Anim3->Anim4 and back to Anim1 and so on.
I am not able to merge all the PNGs in one Texture because Andengine/Android is limited in loading the resources. However, when I split my initial large tile into 4 smaller tiles, everything works fine.
I tried to use an AnimationListener inside Anim1. When onAnimationFinished() is called, I detach Anim1, and run Anim2 and do this in chain of inner functions. However, when I am in Anim4, I do not know how to go back to the start and attach Anim1.
Note: All this problem could be solved if you know how I can pack a set of 150 PNGs that individually quite large but fit in a tile of 4096x4096 px.
Thank you for your help!
EDIT (following JiMMaR's proposed solution):
I am using Texture Packer and the overall Texture exceeds 4096*4096, causing an OutOfMemory error on Android.
At the moment, I have split the Textures into four tiles and I four PNG tilemaps.
You can use 'Texture Packer' to see if all your images can fit in 4096 x 4096.
You can download Texture Packer from here. (NOTE: Texture Packer supports AndEngine data output too)
You can use "BuildableBitmapTextureAtlas" and "BlackPawnTextureAtlasBuilder" classes to pack your PNGs into one texture atlas.
You should post some code so we can see the implementation.
Try to use several AnimatedSprites with animation listeners in each one. This way you can start the animation of the next sprite in the onAnimationFinished() call.
private class AnimationLooperListener implements IAnimationListener{
private AnimatedSprite nextSpriteToAnimate;
public AnimationLooperListener(AnimatedSprite sprite){
nextSpriteToAnimate = sprite;
}
/** other methods are hidden */
public void onAnimationFinished(AnimatedSprite sprite){
sprite.setVisible(false);
nextSpriteToAnimate.setVisible(true);
nextSpriteToAnimate.animate(100, new AnimationLooperListener(sprite);
}
}
AnimatedSprite sprite1 = new AnimatedSprite(0, 0, tiledTextureRegion, vertex);
AnimatedSprite sprite2 = new AnimatedSprite(0, 0, tiledTextureRegion, vertex);
AnimatedSprite sprite2 = new AnimatedSprite(0, 0, tiledTextureRegion, vertex);
AnimationLooperListener listener1 = new AnimationLooperListener(sprite2);
AnimationLooperListener listener2 = new AnimationLooperListener(sprite3);
AnimationLooperListener listener3 = new AnimationLooperListener(sprite1);
sprite1.animate(100, listener1);
sprite2.animate(100, listener2);
sprite3.animate(100, listener3);
This way, you have an animation loop, between several sprites that can be created using several TiledTextureRegions.

How do I reverse the animation of an AnimatedSprite in AndEngine?

I have an AnimatedSprite that after it finishes the animation I want to reverse animate it. I want to do that continuously. Once the reverse animation is complete I want to play the original one. This may be easy one but I am new to Android and AndEngine.
mFlower1Sprite = new AnimatedSprite(20, 800, this.mFlower1);
mFlower1Sprite.setScale((float) 1.5);
mFlower1Sprite.animate(500, 0, new IAnimationListener () {
public void onAnimationEnd(final AnimatedSprite pAnimatedSprite) {
// reverse animation
}
});
mScene.attachChild(mFlower1Sprite);
return mScene;
Use animate method:
public AnimatedSprite animate (long[] pFrameDurations, int[] pFrames,
int pLoopCount, AnimatedSprite.IAnimationListener pAnimationListener)
Animate specifics frames.
Parameters:
pFrameDurations must have the same length as pFrames.
pFrames indices of the frames to animate.
Simply list the indices in reverse order.
I'm working with sprites myself atm and I came across this jQuery plugin. Check it out.
http://spritely.net/
I'm not aware of any support to reverse the frames of an AnimatedSprite, although it's probably a useful feature so I'd love to find out if I'm wrong.
Your best bet would probably be to create another sprite sheet with the frames reversed and another instance of an AnimatedSprite for that sheet. Then define one or two private IAnimationListeners inside your activity (rather than on the fly when you call .animate()), which alternately detach and attach the two sprites at the end of each animation.

Categories

Resources