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
[import]uid: 4795 topic_id: 15359 reply_id: 315359[/import]

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? [import]uid: 19626 topic_id: 15359 reply_id: 56694[/import]

no :slight_smile: 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 :slight_smile:

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 :slight_smile:

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 :slight_smile:

– 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 :slight_smile:
testcircle = display.newCircle( dummyobject.x, dummyobject.y, 10 )
physics.addBody(testcircle, { radius = 10} )

[import]uid: 4795 topic_id: 15359 reply_id: 56697[/import]

It drew it at x=10 for me. [import]uid: 19626 topic_id: 15359 reply_id: 56705[/import]

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 :frowning:

[import]uid: 4795 topic_id: 15359 reply_id: 56714[/import]

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} )
[import]uid: 4795 topic_id: 15359 reply_id: 56718[/import]

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 :slight_smile:

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 :frowning: any solutions for that? [import]uid: 4795 topic_id: 15359 reply_id: 56719[/import]

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. [import]uid: 19626 topic_id: 15359 reply_id: 56722[/import]

i know what you mean but its a bug… as the old object is already removed :slight_smile:
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 !:slight_smile:
thx
chris
[import]uid: 4795 topic_id: 15359 reply_id: 56725[/import]

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
:slight_smile: [import]uid: 12482 topic_id: 15359 reply_id: 56811[/import]

@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… :slight_smile:
    :slight_smile:

chris
[import]uid: 4795 topic_id: 15359 reply_id: 56836[/import]

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
:slight_smile: [import]uid: 12482 topic_id: 15359 reply_id: 56846[/import]

for sure, thats the way how it works… :slight_smile:

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

cheers
chris
[import]uid: 4795 topic_id: 15359 reply_id: 56847[/import]

I’m still struggling with part of this.

Either:

dummyobject = testcircle  

dummyobject is a copy of testcircle or its a pointer to the object referenced by testcircle.

If it is a copy, then only testcircle was removed and dummyobject still exists and would cause the new testcircle to move.

If it is a pointer, then why does dummyobject.x and dummyobject.y still hold values?

I’m thinking that dummyobject should no longer exist but since it wasn’t nilled out and garbage collection hasn’t run yet, the dummyobject is still pointing to that chunk of memory.

I just ran a test using this code:

-- create the original object and add a body  
testcircle = display.newCircle( 10,100, 10 )  
testcircle.x = 10  
physics.addBody(testcircle, { radius = 10} )  
  
-- copy all values from that object into a dummy object  
dummyobject = testcircle  
print (dummyobject.x) -- is perfectly 10 :)  
  
-- remove the old Object & Body  
physics.removeBody (testcircle)  
testcircle:removeSelf()  
testcircle = nil  
collectgarbage("collect")  
-- set the new object at the saved values  
print (dummyobject.x) -- is now \*\*nil\*\*  

And sure enough, dummyobject doesn’t exist any more.

But even changing the code to:

dummyobjectX = testcircle.x  
dummyobjectY = testcircle.y  
  

And using those variables later, the circle still ends up at x = 15 or so. I suspect that the reason putting it in a timer.performWithDelay() function works is that the physics are running in their own thread (as @hgvyas123 suggested above) and that the remaining lines are possibly firing off before the physics.removeBody() is finished and the timer, even though its 0 is enough time.
[import]uid: 19626 topic_id: 15359 reply_id: 56881[/import]

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

@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 :slight_smile:
how and why… and don’t know… walter may tell us.

cheers
chris
[import]uid: 4795 topic_id: 15359 reply_id: 56883[/import]