Getting the center coordinate of a Sprite?

  ... discussions about development with the GLES2 branch of AndEngine.

Re: Getting the center coordinate of a Sprite?

Postby StickFigs » Thu Jun 07, 2012 1:25 am

nazgee wrote:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. mysprite.getSceneCenterCoordinates();
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


For some reason this code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. float[] slotPos = slot.tiledSprite.getSceneCenterCoordinates();
  2. float[] objPos = obj.sprite.getSceneCenterCoordinates();
  3. float dist = (float) Math.hypot(slotPos[0] - objPos[0], slotPos[1] - objPos[1]);
  4. Log.d(this.toString(), "Slot Position = "+slotPos[0]+","+slotPos[1]);
  5. Log.d(this.toString(), "Object Position = "+objPos[0]+","+objPos[1]);
  6. Log.d(this.toString(), "Distance = "+dist);
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Is telling me that the two sprites are at the same exact position which isn't true. Is there something goofy about the getSceneCenterCoordinates() method?

EDIT: x + (width*0.5) doesn't work either because it returns the width of the original, unrotated sprite. Anyone who knows for sure what will work?
StickFigs
 
Posts: 11
Joined: Sun May 27, 2012 12:22 am

Re: Getting the center coordinate of a Sprite?

Postby nazgee » Thu Jun 07, 2012 2:09 am

If something does not work, it usually is a good chance to learn something. You were given two (IMHO perfectly valid) solutions, yet you failed to look at each of them closely enough to make use of them.

StickFigs wrote:Is telling me that the two sprites are at the same exact position which isn't true

AE's source is open. If you are using eclipse, by pressing F3 on a getSceneCenterCoordinates() you can eventually find out, that this method uses returns a static array as a result:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private static final float[] VERTICES_LOCAL_TO_SCENE_TMP = new float[2];
  2.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

This means, that by calling getSceneCenterCoordinates() method twice, you overwrite the result of the first call.

To make it work, you have two options:
1. Do not call getSceneCenterCoordinates() one-after-another.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     float[] slotPos = slot.tiledSprite.getSceneCenterCoordinates();
  2.     Log.d(this.toString(), "Slot Position = "+slotPos[0]+","+slotPos[1]);
  3.     float[] objPos = obj.sprite.getSceneCenterCoordinates();
  4.     Log.d(this.toString(), "Object Position = "+objPos[0]+","+objPos[1]);
  5.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


2. Provide a local arrays which will be used instead of a static one
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     float[] slotPos = new float[2];
  2.     float[] objPos  = new float[2];
  3.     slotPos = slot.tiledSprite.getSceneCenterCoordinates(slotPos);
  4.     objPos = obj.sprite.getSceneCenterCoordinates(objPos);
  5.     float dist = (float) Math.hypot(slotPos[0] - objPos[0], slotPos[1] - objPos[1]);
  6.     Log.d(this.toString(), "Slot Position = "+slotPos[0]+","+slotPos[1]);
  7.     Log.d(this.toString(), "Object Position = "+objPos[0]+","+objPos[1]);
  8.     Log.d(this.toString(), "Distance = "+dist);
  9.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


I'm not saying your question was bad. I am saying that your attitude was- do not expect everything to be solved for ya.
Dirt Rider Mayhem is PUBLISHED now!
Image
User avatar
nazgee
 
Posts: 524
Joined: Fri Oct 21, 2011 10:31 pm
Location: Poland, Wrocław

Re: Getting the center coordinate of a Sprite?

Postby OzLark » Thu Jun 07, 2012 2:58 am

deleted
....
Did this post help you? Feel free to return the gesture by downloading Ninja Bees, creating a level (or more) and sharing it to the Ninja Bees website!

Ninja Bees - Our latest Android game, check it out on Google Play (free) or paid with no ads
OzLark
 
Posts: 1130
Joined: Sun Feb 05, 2012 3:19 am

Re: Getting the center coordinate of a Sprite?

Postby StickFigs » Thu Jun 07, 2012 3:21 am

nazgee wrote:If something does not work, it usually is a good chance to learn something. You were given two (IMHO perfectly valid) solutions, yet you failed to look at each of them closely enough to make use of them.


Okay and what was the other valid solution?

And instead of
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. slotPos = slot.tiledSprite.getSceneCenterCoordinates(slotPos);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
wouldn't just
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. slot.tiledSprite.getSceneCenterCoordinates(slotPos);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
work?

EDIT: Yes it does, thank you.
StickFigs
 
Posts: 11
Joined: Sun May 27, 2012 12:22 am

Previous

Return to GLES2

Who is online

Users browsing this forum: Alexa [Bot] and 34 guests