Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Possible Bug? localToGlobal from rotated sprite returns different result when called repeatedly — Gideros Forum

Possible Bug? localToGlobal from rotated sprite returns different result when called repeatedly

bysregbysreg Member
edited November 2012 in Bugs and issues
for example, i have redbox, bluebox, and yellow box. redbox and bluebox is added directly to stage, but the yellow box is added to the bluebox. the yellow box will try to align itself between the redbox and bluebox.

but then when i moved the red box to be close enough to the blue box, the angle starts shaking. i found out that the angle calculation becomes weird because the global coordinate from bluebox:localtoglobal returned different results each enterframe (value a then value b then value a again, then value b and so on)

here is the project . you can move the redbox using joystick with click and drag on the screen

thanks

edit :
wrong attached files, here is the correct one

Comments

  • looks like by mistake you had uploaded wrong zip as it has just three blank files no code

    :)
  • @hgvyas123

    oops sorry about that, i've corrected my post
  • I've downloaded your zip and had a quick look and my first thought is that this could be because Gideros alters the width and height of a sprite as it's rotated so the local -> global mapping may indeed differ.

    So what you're doing in the onEnterFrame event is basically altering the rotation, which is having the effect of altering the local -> global mapping each time, so the results are likely to be different on each call to onEnterFrame, hence the "wobbling" you're seeing.

    That's my best guess on a quick look.
  • @moopf

    hmm, you're right. localToGlobal should return the global coordinate shouldnt it? then maybe this is a bug?
  • @bysreg Just been taking another look. The problem is that you're trying to get a position outside the bitmap when you're using localToGlobal(-50,0) and, depending on the rotation, this could vary a lot I think. Thing is if I change it to localToGlobal(0,0) the shaking goes away but I'm not sure what you actually want to do with the yellow box when the red box gets closer.
  • ScouserScouser Guru
    edited November 2012
    @bysreg: Would you not get the effect you want if you actually replaced
    	local px, py = blueBox:localToGlobal(-50, 0)
    @ line 71 with
    	local px, py = blueBox:localToGlobal(0, 0)	
    	px = px - 50
  • @Scouser - the problem with that is that the yellow box is then very rarely directly in line between the red and blue boxes.
  • ScouserScouser Guru
    edited November 2012
    Ok I noticed a very strange thing. If you place the red square @ 170,220 and just run the code you will see that the problem appears to occur when you have the red & blue squares closer than the gap between yellow and blue.

    Because the yellow is a fixed distance away from the blue and the red is closer to blue than yellow then the math falls over. It may be that you have to separate yellow from blue and calculate the new position of the yellow yourself using the angle calculated from red & blue.
  • @moopf , @Scouser
    i've revised the project to better illustrate what i want to achieve. So, the reason that i use localToGlobal with the local coordinate of the yellow box(now 45 deg yellow box image) is that i want to make the yellow box to point the red box.

    The idea is that i have certain gun attached to some character shoulder. the gun will be slightly rotated (yellow box represents the gun and the blue box represents the character's shoulder while the redbox is the target). the gun local coordinate and degree is exactly the same as in this project

    zip
    zip
    test.zip
    14K
  • moopfmoopf Guru
    Accepted Answer
    @bysreg I still think you're wrong to try to do a localToGlobal on an arbitrary point outside the bounds of the blue box as you are doing, unless you're also translating that point according to the current rotation of the blue square.

    However, if I change the localToGlobal to 0,0 and then change GUN_ELBOW_ANGLE = 25 it pretty much works as I think you intend it to. Obviously once it gets too close to the yellow box, that can no longer point to the red box but then, if you think about it, if it's a gun, you'd have to start twisting your wrist anyway to point to something that close.

    Does that help at all?
  • @moopf
    well , i tried enlarging the blue box, but it's still get different results each enter frame.

    and yes, that'll solve the problem for now. but it still count as bug right? as it differ with what the documentation state

    but thanks!
  • Not sure, I don't think I'd consider it a bug - you're asking for a point outside the bounds of the sprite - a sprite that's also rotated. I think you need to do your own translation on the point, as I've previously said.
Sign In or Register to comment.