[New Feature] Sprite Chains!

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

[New Feature] Sprite Chains!

Postby destriaero » Fri Oct 07, 2011 3:11 pm

Hey all.

Just started using AndEngine this month and am loving this project thus far. The following class let's you create a chain of particular sprite, and then move the head chainlink using setPosition. The rest of the chain will follow in a very natural looking way.

The movement is calculated using a unit vector, ensuring that the space between chain links is always equal.

Great for actual chains, ropes, worms...

EDIT: Removed Restrict to X, and fixed delta.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.df.spacecowboy;
  2.  
  3. import javax.microedition.khronos.opengles.GL10;
  4.  
  5. import org.anddev.andengine.engine.camera.Camera;
  6. import org.anddev.andengine.entity.Entity;
  7. import org.anddev.andengine.entity.sprite.Sprite;
  8. import org.anddev.andengine.opengl.texture.region.TextureRegion;
  9.  
  10. public class SpriteChain extends Entity {
  11.         private TextureRegion mTextureRegion;
  12.         private Sprite[] mChainElementArray;
  13.         private int mChainLength = 0;
  14.         private float mX, mY;
  15.         private float DELTA = 7f;
  16.  
  17.         public SpriteChain(TextureRegion pTextureRegion, int pChainLength,
  18.                         float pX, float pY) {
  19.                 this.mTextureRegion = pTextureRegion;
  20.                 this.mChainLength = pChainLength;
  21.                 this.mX = pX;
  22.                 this.mY = pY;
  23.  
  24.                 mChainElementArray = new Sprite[pChainLength];
  25.                 mChainElementArray[0] = spawnLink(pX, pY);
  26.  
  27.                 for (int i = 1; i < mChainLength; i++) {
  28.                         mChainElementArray[i] = spawnLink(mChainElementArray[i - 1].getX(),
  29.                                         mChainElementArray[i - 1].getY() + DELTA);
  30.                 }
  31.         }
  32.  
  33.         @Override
  34.         public void setPosition(float pX, float pY) {
  35.                 super.setPosition(pX, pY);
  36.  
  37.                 mChainElementArray[0].setPosition(pX, pY);
  38.                 for (int i = 1; i < mChainLength; i++) {
  39.                         float dX = mChainElementArray[i - 1].getX()
  40.                                         - mChainElementArray[i].getX();
  41.                         float dY = mChainElementArray[i - 1].getY()
  42.                                         - mChainElementArray[i].getY();
  43.                         final float distance = (float) Math.sqrt(dX * dX + dY * dY);
  44.                         dX /= distance;
  45.                         dY /= distance;
  46.  
  47.                         mChainElementArray[i].setPosition(mChainElementArray[i - 1].getX()
  48.                                         - dX * DELTA, mChainElementArray[i - 1].getY()
  49.                                         - dY * DELTA);
  50.  
  51.                 }
  52.         }
  53.  
  54.         private Sprite spawnLink(float pX, float pY) {
  55.                 return this.spawnLink(pX, pY, 1.0f);
  56.         }
  57.  
  58.         private Sprite spawnLink(float pX, float pY, float scale) {
  59.                 final Sprite chainLink = new Sprite(pX, pY, mTextureRegion.deepCopy());
  60.                 chainLink.setWidth(16);
  61.                 chainLink.setHeight(16);
  62.                 chainLink.setScale(scale);
  63.                 return chainLink;
  64.         }
  65.  
  66.         public void setDelta(float pDelta) {
  67.                 this.DELTA = pDelta;
  68.         }
  69.  
  70.         @Override
  71.         public float getX() {
  72.                 return mChainElementArray[0].getX();
  73.         }
  74.  
  75.         @Override
  76.         public float getY() {
  77.                 return mChainElementArray[0].getY();
  78.         }
  79.  
  80.         @Override
  81.         protected void onManagedDraw(final GL10 pGL, final Camera pCamera) {
  82.                 for (int i = 0; i < mChainElementArray.length; i++) {
  83.                         mChainElementArray[i].onDraw(pGL, pCamera);
  84.                 }
  85.         }
  86. }
  87.  
  88.  
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


Of note of course is the Delta parameter determining the maximum distance. You may want to tweak the positioning using delta in setPosition + constructor, right now it's just rough values.

Also I am initializing the sprite to be 16x16, however you may modify this in the constructor logic to shrink the size based on the position of the chain link.

Let me know if anyone needs an example.

Thanks!
destriaero
 
Posts: 2
Joined: Fri Oct 07, 2011 3:05 pm

Re: [New Feature] Sprite Chains!

Postby enonocfed » Tue Nov 15, 2011 7:22 am

I would love an example if you have it. Thanks for offering.
enonocfed
 
Posts: 11
Joined: Mon Jan 31, 2011 5:45 pm

Re: [New Feature] Sprite Chains!

Postby Benny11 » Fri Nov 18, 2011 7:34 am

Could you please post an example? :D

Thanks
Benny11
 
Posts: 4
Joined: Thu Nov 17, 2011 9:34 am

Re: [New Feature] Sprite Chains!

Postby Siddharth » Wed Apr 10, 2013 9:15 pm

I can able to create rope with sprite using above post but how to attach body in this.

Thanks in advance for your help.
User avatar
Siddharth
 
Posts: 278
Joined: Thu May 03, 2012 12:11 pm
Location: Rajkot

Re: [New Feature] Sprite Chains!

Postby peter20 » Thu Apr 11, 2013 3:13 pm

I would love an example if you have it. Thanks for offering.
peter20
 
Posts: 4
Joined: Thu Apr 11, 2013 3:10 pm
Location: india


Return to Features

Who is online

Users browsing this forum: No registered users and 8 guests