How to get an objects real/current position?
Last post 05-08-2008 2:43 AM by Abhi319. 14 replies.
Sort Posts:
10-05-2007 7:18 PM
How to get an objects real/current position?

I have a few objects (Ellipses) being drawn on a canvas.

Each object is being rendered within a <TransformGroup>.

They are being translated as well as being rotated.

All of these objects are defined in the Xaml and have the same Canvas.Left and Canvas.Top defined (let's say 0, 0).

 

Here's the issue:

When I click on an object to retrieve an object's position (Canvas.Left, Canvas.top) via its respective sender object, I end up with the object's defined coordinates, not its current coordinates.

 How can i get an objects real/current coordinates?

Thanks.
 

edward_494

Joined on 10-05-2007
Posts 35
10-05-2007 7:38 PM
Re: How to get an objects real/current position?

As far as I know, you can't.  I think you would have to calculate it yourself.  In the case of translate tranforms, you can just add the TranslateTransform x and y values to Canvas.Left and Canvas.Top.  RotateTransforms are going to be harder, but that's also do-able.

swirlingmass

Joined on 05-02-2007
Posts 384
10-05-2007 8:11 PM
Re: How to get an objects real/current position?

 Oh my god. you've got to be kidding me.

I find this to be a huge hindrance and a flaw in the silverlight object model. 

edward_494

Joined on 10-05-2007
Posts 35
10-05-2007 8:12 PM
Re: Re: How to get an objects real/current position?

 You've got to be kidding me.

I find this to be a huge hindrance and a flaw in the silverlight object model. 

edward_494

Joined on 10-05-2007
Posts 35
10-05-2007 9:15 PM
Re: Re: Re: How to get an objects real/current position?

 Ok, I tried doing just this, re-calculating coord's and rotation rather than retrieving them from the object I clicked.

 I am not sure if even this can be done? Suppose I want to calculate the clicked object's rotation? How on earth can I calculate that object's rotation? Based on elapsed time????

 There has got to be an elegant solution to this. I can't imagine Silverlight not supporting this.
 

edward_494

Joined on 10-05-2007
Posts 35
10-08-2007 12:01 PM
Re: Re: Re: How to get an objects real/current position?

Why do you want to know its "real" coordinates?

swirlingmass

Joined on 05-02-2007
Posts 384
10-08-2007 12:30 PM
Re: Re: Re: How to get an objects real/current position?

Let's suppose I want to draw something else on top of the translated and rotated object.

 Example: An Ellipse is drawn after it has been translated and rotated. When the user clicks on the Ellipse I want to draw something else (like a few more shapes) RELATIVE to the Ellipse's real-screen coordinates.

Since my last post I have switched over to Silverlight 1.1 SDK so that i can use C# rather than Javascript. I am finding Silverlight development/learning much smoother now, but I still havent addressed this real-coordinate issue.
 

edward_494

Joined on 10-05-2007
Posts 35
10-08-2007 1:33 PM
Re: Re: Re: How to get an objects real/current position?

Wouldn't applying the ellipse's transformations to your new drawing make the new drawing relative to the ellipse?  If that doesn't work for you, I suppose you could try using the mouse coords and the dimensions/rotation of your object to calculate its position (again, kind of mathy with the rotation calulation stuff).

swirlingmass

Joined on 05-02-2007
Posts 384
10-08-2007 3:04 PM
Re: How to get an objects real/current position?

If you have applied translate transformation on object .

Then you can find the current object current X and Y co-ordinate with respect to  its parent .

if (Obj.RenderTransform != null)

{

 var transformGroup = Obj.RenderTransform.children ;

var Object ;  // This is translate or Rotate Transform wat ever you applied on that particular object

for (var i = 0 ; i <  transformGroup.count ; i++)

{

 Object = transformGroup.getItem(i);

}

if (Object.toString() == "TranslateTransform")

{

ObjectPosX =   Object.X ;

ObjectPosY =   Object.Y ;

}

 

hope this is useful for you Big Smile

Thanks

Kartikey

kartikey

Joined on 10-08-2007
Posts 2
10-08-2007 4:08 PM
Re: Re: Re: How to get an objects real/current position?

edward_494:

Suppose I want to calculate the clicked object's rotation? How on earth can I calculate that object's rotation? Based on elapsed time????

 You wouldn't calculate it.  You would give a name to the RotateTransform, say rtrans, and then do something like

sender.findName("rtrans").angle;

to get its current rotation angle.  Similar syntax for the centerX and centerY properties.

swirlingmass

Joined on 05-02-2007
Posts 384
10-08-2007 4:49 PM
Re: Re: Re: How to get an objects real/current position?

Turns out the math isn't too bad.  Given a point x,y and a center of rotation centX,centY, and a rotation angle rAngle, you can find the new x,y by:

newX = centX + (Math.cos(rAngle) * x - Math.sin(rAngle) * y);
newY = centY + (Math.sin(rAngle) * x + Math.cos(rAngle) * y);

Where rAngle is in radians.  To convert from degress to radians:

radianValue = degreeValue * Math.PI / 180;

swirlingmass

Joined on 05-02-2007
Posts 384
10-08-2007 5:49 PM
Re: Re: Re: How to get an objects real/current position?

thanks for the many input here. i'm going to have to try some of these (from simplest/cheapest to the most complicated/expensive).

i hope these work though. but from what i saw in 1.0 (javascript) i was unable to retrieve the actual object coordinates as they were not relative to their parent.

i'll have to sit down and go thru the properties and debug it in C#. this should be more productive than using js. 

edward_494

Joined on 10-05-2007
Posts 35
10-09-2007 11:53 AM
Re: Re: Re: How to get an objects real/current position?

 Okay guys. I havent been able to get an object's "real" screen-coordinates (i doubt this can be done), but I was able to get the job done by getting the object's parent (which is the object's very own Canvas).

Now i dont perform any transformations on objects directly, rather I perform transformations on the object's Canvas object itself. So now I dont have to worry about transformations/coordinates...etc. Now I just render directly to that respective Canvas which will draw everything with respect to that Canvas' properties.

Thanks.
 

edward_494

Joined on 10-05-2007
Posts 35
03-05-2008 11:09 PM
Re: Re: Re: How to get an objects real/current position?

Is there a way to do this in Beta1 now? I need to get the coordinates of a silverlight element within the plugin taking into consideration any transforms

Sam747

Joined on 05-26-2007
Posts 14
05-08-2008 2:43 AM
Re: Re: Re: How to get an objects real/current position?

 Hello....I have made a Drag n Drop Application in silverlight1.0...Now I want to retrieve the new position(Coordinates) of the object whenever it is being moved at runtime using javascript.

 

Can u please tel me if its possible n how??

Thanks & Regards
 

Abhi319

Joined on 05-08-2008
Posts 1