Projection in AndEngine - android

I want to move my Sprite in AndEngine in a projectile motion.
Can any one help me in this matter with a piece of code? Unfortunately I didn't see an example about projections in AndEngine.

What you're looking for is the UpdateModifier, I suspect.
How do you want your Sprite to move? Do you want it to find a path to the destination or are you just trying to keep something on the screen? Are you using a Camera? All of that will determine how exactly you should approach getting your Sprite to move.
This is a link for the tut about UpdateModifiers:
Update Handlers - Using their Power!
Is that what you were looking for?

for a projectile move , you can use the physicshandler and then you can manipulate the acceleration and velocity to make it move like projectile...
projectile=new Sprite(X,Y, ProjectileTextureRegion);
physprojectile = new PhysicsHandler(projectile);
projectile.registerUpdateHandler(physprojectile);
physprojectile.setVelocityX(Xvelocity);
physprojectile.setVelocityY(Yvelocity);
pyhsprojectile.setAcceleration(Accelration) //negative acceleration

Basically you can use registerEntityMofidier on a sprite (or on any Entity): Andengine EntityModifierExample
Example:
sprite.registerEntityModifier(new MoveModifier(TIME_IN_SECOND, fromX, toX, fromY, toY));

stakesprite.registerEntityModifier(new RotationModifier(0.1f, 0.0f,(float) ((360.0f/Math.PI)*Math.atan(velY/velX))));
stake=PhysicsFactory.createCircleBody(this.mPhysicsWorld, stakesprite, BodyType.DynamicBody, FIXTURE_DEF);
//stake = PhysicsFactory.createBoxBody(this.mPhysicsWorld, stakesprite, BodyType.DynamicBody, FIXTURE_DEF);
stake.setBullet(true);
stake.setLinearVelocity(new Vector2(velX, velY));
stake.setSleepingAllowed(true);
mScene.attachChild(stakesprite);

Related

Ball sticking to sidewalls in android unity 3d?

I try to run roll a ball game Unity 3d example in android device, The ball is sticking to the sidewalls and also ball is moving very slowly when the ball is in contact with sidewalls. Help me regarding this issue?
Here is my accelerometer code for ball moving
Screen.sleepTimeout = SleepTimeout.NeverSleep;
curAc = Vector3.Lerp(curAc, Input.acceleration-zeroAc, Time.deltaTime/smooth);
GetAxisV = Mathf.Clamp(curAc.y * sensV, -1, 2);
GetAxisH = Mathf.Clamp(curAc.x * sensH, -1, 2);
Vector3 movement = new Vector3 (GetAxisH, 0.0f, GetAxisV);
rigidbody.AddForce(movement * speedAc*2f);
Thanks In Advance
I had a similar problem when building a pinball game. I was not using accelerometer, but the ball behavior was the very same.
Just check out the physic material of yout objects. Ball, walls and either floor has to be checked. As I don't know exactly what kind of game you are building, I recommend you to try out every parameter.

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

Android AndEngine two circles collision perfectly

Android AndEngine two circles collision perfectly. I have two circle and a collision method for them, I want when they touch each other the collision happens, currently when they near each other the collision happens.
I think that it is because of the transparent free space in the .png file of each circle.
In the picture you can see that now they collide from a distance, I want when both touch each other.
My collision method:
if (circle1.collidesWith(circle)){
Score += 1;
}
I am almost sure you are right that transparent places in png causes it. You probably creating BoxBody. In your case you should use circle body like this:
Body circleBody = PhysicsFactory.createCircleBody(pWorld, pSprite, BodyType.StaticBody, FixtureDef);
If it doesn't help there is method overload where you can provide position and size of the body. I can recommend you using DebugRender which you only have to attach to scene:
new DebugRenderer(physicsWorld, vbom)
When u use this you will see how helpful it can be:) Just remember that it may slowdown your phone when you have a lot of bodies on the scene.
PS. You didn't give us a lot of information but you should use contactListener to check colisions. There are plenty of tutorials in the internet for it
PS2. If you don't use Box2D extension - do it. This is great feature of AndEngine and it's pointless to implement that for yourself. It will be hard to detect circle shape collision of 2 objects without Box2D.
If you are not in Box2d , You must use Pixel-Perfect Collision library. Well default AndEngine Library, Does not support pixel perfect collision. To get this support, you need to import this library in eclipse and add this to your project uses library.
Here, I Demonstrate how to use this library. When you define Texture and Atlas for your sprite write as below.
private BitmapTextureAtlas lifeAtlas;
public PixelPerfectTiledTextureRegion life_Texture;
PixelPerfectTextureRegionFactory.setAssetBasePath("gfx/game/");
lifeAtlas = new BitmapTextureAtlas(textureManager, 1280, 128,
TextureOptions.BILINEAR);
life_Texture = PixelPerfectTextureRegionFactory.createTiledFromAsset(
lifeAtlas, activity, "heart_tiled.png", 0, 0, 10, 1, 0);
lifeAtlas.load();
For your custom sprite class,
public class Plane extends PixelPerfectAnimatedSprite {
public Plane(float pX, float pY,
PixelPerfectTiledTextureRegion pTiledTextureRegion,
VertexBufferObjectManager pVertexBufferObjectManager) {
super(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager);
setColor(Color.GREEN);
}
}
You also need some adjustment with your AndEngine library to use it. Follow this thread to go.

Unity2D Android Touch misbehaving

I am attempting to translate an object depending on the touch position of the user.
The problem with it is, when I test it out, the object disappears as soon as I drag my finger on my phone screen. I am not entirely sure what's going on with it?
If somebody can guide me that would be great :)
Thanks
This is the Code:
#pragma strict
function Update () {
for (var touch : Touch in Input.touches)
{
if (touch.phase == TouchPhase.Moved) {
transform.Translate(0, touch.position.y, 0);
}
}
}
The problem is that you're moving the object by touch.position.y. This isn't a point inworld, it's a point on the touch screen. What you'll want to do is probably Camera.main.ScreenToWorldPoint(touch.position).y which will give you the position inworld for wherever you've touched.
Of course, Translate takes a vector indicating distance, not final destination, so simply sticking the above in it still won't work as you're intending.
Instead maybe try this:
Vector3 EndPos = Camera.main.ScreenToWorldPoint(touch.position);
float speed = 1f;
transform.position = Vector3.Lerp(transform.position, EndPos, speed * Time.deltaTime);
which should move the object towards your finger while at the same time keeping its movements smooth looking.
You'll want to ask this question at Unity's dedicated Questions/Answers site: http://answers.unity3d.com/index.html
There are very few people that come to stackoverflow for Unity specific question, unless they relate to Android/iOS specific features.
As for the cause of your problem, touch.position.y is define in screen space (pixels) where as transform.Translate is expecting world units (meters). You can convert between the two using the Camera.ScreenToWorldPoint() method, then creating a vector out of the camera position and screen world point. With this vector you can then either intersect some geometry in the scene or simply use it as a point in front of the camera.
http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html

Attach Sprite on another Sprite ANDENGINE

I am working on andengine and i have two sprite one is plate and the other is an apple . My plate sprite move form point 1 to point 2 and my apple sprite is jumping up and down.
Now i want to make apple jump on plate. I tried it with attched child apple with plate but the apple not place on the plate. Apple place below the plate i used zindex but its not working.
Actually problem is to move apple and plate at the same time. Any help would be appriciated. I am stuck with that that why this is happening and what will be solution .Here is my code:
plateDisplay = new Sprite( 250, 300, this.plate, this.getVertexBufferObjectManager());
appleDisplay = new Sprite( 250, 140, this.apple, this.getVertexBufferObjectManager());
plateDisplay.registerEntityModifier(new LoopEntityModifier(new PathModifier(20, path, EaseLinear.getInstance())));
appleDisplay.registerEntityModifier(new LoopEntityModifier(new ParallelEntityModifier(new MoveYModifier(1, appleDisplay.getY(),
(appleDisplay.getY()+70), EaseBounceInOut.getInstance()))));
this.appleDisplay.setZIndex(1);
plateDisplay.setZIndex(0);
plateDisplay.attachChild(this.appleDisplay);
scene.attachChild(plateDisplay);
The issue you are having is that there are different coordinate systems for each object. The Plate sprite has its own X and Y in the scene coordinates. But when you add the apple to the plate object you are now using the plates local coordinates. So if the apple was on the scene's 50,50, when you add it to the plate, it will now be 50,50 as measured from the transform center point of the plate.
There are LocaltoScene and ScenetoLocal coordinate utilities in andengine to help you make this conversion. But underneath they are not super complex - they just add the transforms of all the nested sprites. Both utilites are part of the Sprite class, so you call them from the sprite in question. In your case probably
// Get the scene coordinates of the apple as an array.
float[] coodinates = [appleDisplay.getX(), appleDisplay.getY()];
// Convert the the scene coordinates of the apple to the local corrdinates of the plate.
float[] localCoordinates = plateDisplay.convertSceneToLocalCoordinates(coordinates);
// Attach and set position of apple
appleDisplay.setPosition(localCoordinates[0], localCoordintates[1]);
plateDisplay.attachChild(appleDisplay);

Categories

Resources