why is object.x / y set wrong ??

Hi there,

i need to create my objects always new (as i have to change its size after a collision)
My problem right now is, that the object is not set at the exactly position from the old object,
its always somehow set a few pixels beside.(this is just an excerpt i need all object values, so i work with that dummy object)

Here my test code:

-- create the original object and add a body
testcircle = display.newCircle( 10,100, 10 )
physics.addBody(testcircle, { density=10, friction=0.1, bounce=0.0, radius = 10} )

-- copy all values from that object into a dummy object
dummyobject = testcircle

-- remove the old Object & Body
physics.removeBody (testcircle)
testcircle:removeSelf()

-- set the new object at the saved values
testcircle = display.newCircle( dummyobject.x, dummyobject.y, 10 )
physics.addBody(testcircle, { density=10, friction=0.1, bounce=0.0, radius = 10} )

as you may see, first object is created exactly at 10,100 but the new one kind a 15, 100

thx
chris

have you printed out the values for dummyobject.x and dummyobject.y

Also does the:

dummyobject = testcircle

actually make a duplicate of test circle or is it just a pointer to the same data that testcircle is pointing to? My understanding is that its the same circle, so I'm surprised that after doing removeSelf() on testcircle that you have anything left in dummyobject. Though I suppose that since you didn't "nil" it, and garbage collection may not have ran, dummyobject may still be pointing to the same place in memory.

Any way print out the values of dummyobject.x and dummyobject.y

Could testcircle have moved due to the physics and you're getting the value's of its new position?

no :) for sure i tested that before
x is saved perfect.

its very simple you can just copy/paste the whole code bellow to test
i made a small modification in the meantime and found something funny.
when the addBody just include {radius = 10} than it jumps on the x coordinate around
5 px to the right. when I remove also the radius from both objects, so they appear as body squares, they jump in the y coordinate around 5px up :)

also i just can't say in the new object x-1 in that moment it jumps to the original x - 1 ... it really looks like a bug.. you may test.. could be helpful if its just on me :)

all the talks that we can't change an object in a collision and should remove/create new later, doesn't help as long i can't set it at the exactly same place as before!

thx
chris

local physics = require("physics")
physics.start()
physics.setGravity(0,0)
physics.setDrawMode( "hybrid" )

-- create the original object and add a body
testcircle = display.newCircle( 10,100, 10 )
physics.addBody(testcircle, { radius = 10} )

-- copy all values from that object into a dummy object
dummyobject = testcircle
print (dummy object.x) -- is perfectly 10 :)

-- remove the old Object & Body
physics.removeBody (testcircle)
testcircle:removeSelf()

-- set the new object at the saved values
print (dummy object.x) -- is still perfectly 10 :)
testcircle = display.newCircle( dummyobject.x, dummyobject.y, 10 )
physics.addBody(testcircle, { radius = 10} )

It drew it at x=10 for me.

so it also wrote for you x = 10

but did it placed the new circle at the same place as the old circle?
you could just test by removing all after the first print

its just crazy.... even when i make at the second (new) circle y +1
than its fine... its just if I like to place the circle at the exactly same position as before the x value does jump :(

and now its kind a proof that have to be kind a bug
as you see

1. i don't copy even in a dummy variable..
2. i use a different variable name for the new circle

.. but in the moment it would be set to the old coordinates
the new object is jumping as it would do when the old object its still there

-- create the original object and add a body
testcircle = display.newCircle( 10,100, 10 )
physics.addBody(testcircle, { radius = 10} )

physics.removeBody (testcircle)
testcircle:removeSelf()

-- set the new object at the saved values
testcircle2 = display.newCircle( 10, 100, 10 )
physics.addBody(testcircle2, { radius = 10} )

ok.. i may solved the prob by a hack... but i am not sure if that should be.. also i am sure that will slow down my app

-- create the original object and add a body
testcircle = display.newCircle( 10,100, 10 )
physics.addBody(testcircle, { radius = 10} )

physics.removeBody (testcircle)
testcircle:removeSelf()

local listener = {}
function listener:timer( event )
-- set the new object at the saved values
testcircle2 = display.newCircle( 30, 100, 30 )
physics.addBody(testcircle2, { radius = 30} )
end
timer.performWithDelay(0, listener )

just with a 0 delay... it does the trick.
that seems really kind a timing prob in corona... as i do need that trick quiet often
(director scene changes for example after a group clean etc)

so anyone with the same prob.. could always ad a listener with delay :)

greets
chris

now i don't have a jumping.. but i guess through the delay (even if its withdelay(0...
the objects start flickering while transforming :( any solutions for that?

Well I see them physically move and the first circle is no longer at x=10, but x=0. It seems like the 2nd one is pushing against the other and they both move 10 pix apart from each other. So you end up with first.x = 0 and second.x=20, but if you print the x after its done it still says 10.

I'm not sure this is a bug. In 2D physics, there is no depth, so in theory the two circles can't occupy the same space, ergo they push themselves apart.

i know what you mean but its a bug... as the old object is already removed :)
it should be there... and in praxis my hack with the listener delay proofs that.

so actualy (without my listener) the old object is still there and so the new one is
moved away... but funny also... when the circle is 30 radius.. it also just moves 10 px.

there is a serious bug in it. and i hope it will be found.

for now my hack does work.. but with the delay i get that flickering (when the circle is 10 times per second removed and new created at the same place... any idea how i could avoid that?

in old days was kinda.. wait for horizontal refresh.. but i guess thats not anymore !:)

thx
chris

i dont think so its a bug basically when you remove any objects it will not remove at the same time as your line of code for removing that object is written especially when that object is a physics object as it might be possible that it is being used by some internal process so it will be removed on update thread not sure if corona is doing same way its a standard solution to remove object as on update thread none of objects are being used and it's a very small amount of time in milisecs but i think that is creating prob for you try to set x and y position to something out side of screen like 10000 before removing object and see if it can solve or not

:)

@hgvyas123
i know what you mean, but also for sure i would need some confirmation if its removed or not.. anything that gives me 'control' about the app. if its just done whenever , than not proper coding is possible.

So before we come in a discussion... bug or not bug...

i would just like to have a solution (demo) how to:
- proper remove an object with a body, and place it at the same point without flickering or delay

EDIT: ps: Hi HGVYAS123 .... your idea with setting the x to lets say 10000 before removing.. did helped great.. .looks much better.. i came able to remove the listenerdelay and its not jumping.... :)
:)

chris

i had told you this just as my thought i am not sure how corona is removing object it is based on generally how we remove object in java and as you are saying that idea of 10k works i think so corona is also doing the same way still cant bet on this.

and to remove any object it's display.remove() is perfect just in some situation like this we need to work smartly

:)

for sure, thats the way how it works... :)

we don't have to understand always everything... as long it does work ... thats coding :@

cheers
chris

I'm still struggling with part of this.

Either:

1
dummyobject = testcircle

i gave up to find out why its how it is :)

@robmiracle
exactly as you did say.
on one side.. it looks its a pointer and not a new object.

at all the solution to make x= 1000 helped to remove it

but still after that i use values from dummy object to my new object :)
how and why... and don't know... walter may tell us.

cheers
chris

views:1726 update:2011/10/15 21:01:16
corona forums © 2003-2011