Crash when I try to move object by changing x and y attributes

Hello,

I have an object (Obj1), which collides with a sensor (Sens1).
When I detect the collision in the “began” phase), I stop Obj1 (set linear velocity to 0,0) and change Obj1 to have a “kinematic” body type, in an attempt to be able to move it manually to the center of the sensor by changing its x and y attributes using:

Obj1.x = Sens1.x.

I do this because I want to start an animation after it moves to the center of the sensor.

Every time I try setting the x or y attributes for Obj1, the simulator crashes.
I tried doing it with:

Obj1:translate(deltax, deltay)

but it still crashed.

I am new to Corona but I am a developer. I must be making some kind of basic mistake.
Any help would be greatly appreciated.

Thank you in advance.
[import]uid: 12046 topic_id: 4155 reply_id: 304155[/import]

I think you want to change the object type to static while programmatically changing it’s x and y, then change it to kinematic when you’re done. I’ve had similar problems and always ended up changing to static.

m [import]uid: 8271 topic_id: 4155 reply_id: 12928[/import]

Thank you for your response, horacebury, but that was another thing I tried already.
No matter what I do, as soon as I try to do something with the x or y of Obj1, even if it’s just applying some math function, I get this:

Assertion failed: (m_world->IsLocked() == false), function SetTransform, file /Users/ansca/.hudson/jobs/Pong-DMG/workspace/platform/mac/…/…/external/Box2D_v2.1.2/Box2D/Box2D/Dynamics/b2Body.cpp, line 395.

I don’t know why it’s referring to /Users/ansca/…

Any help would be greatly appreciated.
[import]uid: 12046 topic_id: 4155 reply_id: 12945[/import]

Ok, I’ve had that a few times. I’m fairly certain you’re changing the wrong value or calling a function you’re not allowed to. Its a pig because you’re gonna spend a lot of time checking things which don’t look wrong, but trust me… its there. Try commenting out each line in the code until it works. This one is one of those “it’ll take ages to find and be a real stupid thing when you find it” situations. Sorry, good luck!

m [import]uid: 8271 topic_id: 4155 reply_id: 12987[/import]

This has happened to me before also, and every time, I’ve always had to apply some workaround (instead of changing x/y values).

It always happened the same way as well (from within a collision event listener, or as a result of a collision).

I’d suggest submitting it as a bug report, and attaching an example for the Ansca engineers to see. [import]uid: 7849 topic_id: 4155 reply_id: 12997[/import]

Ah yes, there are certain properties of objects which, if you try to manipulate them within an event handler, will throw up. This is a big problem when using the physics engine. What I’ve managed with in the past is simply setting a very short, one-off timer to execute a change. Fire the timer within the event handler, but don’t actually perform the change within the event handler, do it in the timer handler.

Matt. [import]uid: 8271 topic_id: 4155 reply_id: 13003[/import]

@Matt: Thanks a lot for sharing that! I wish I would have known that a while ago lol [import]uid: 7849 topic_id: 4155 reply_id: 13012[/import]

Similar problem here, it seems: http://developer.anscamobile.com/forum/2010/12/03/how-grow-body [import]uid: 8271 topic_id: 4155 reply_id: 13013[/import]

Thank you, everyone.

My problem is solved.
Just as Matt mentioned, I implemented a timer. I needed to do this anyway because I had to stop my object for a short moment, run some animation and then apply an impulse to it.

Just before I call my timer I stop my object:

obj1:setLinearVelocity(0, 0)
obj1.angularVelocity = 0

Then I make the object static:

obj1.bodyType = “static”

Then I call my timer:

timer.performWithDelay(500, timerImpulse )

and within the timerImpulse function (a local function within my onLocalCollision function) I do the following:

  1. reset the x and y attributes of my object
  2. reset the bodyType of my object to “dynamic”
  3. apply impulse to my object

Remember that my object obj1 has collided with a sensor, so if my new (after reset) x and y position is within the collision area of my sensor, this won’t work because as soon as I set the bodyType back to “dynamic”, a collision will trigger again on the same sensor, crashing or causing and endless loop.

I have notices also what Matt described. Even though my new x and y are outside my sensor’s collision area (the one with which obj1 collided), if the new x and y are within a different sensor’s collision area, my impulse will cause the app to crash. I think it is because the physics engine loses a pointer somewhere between trying to activate another collision and activating the impulse.

I hope this makes sense.
Thank you Matt, and everyone for a great discussion on this issue.

Polo

[import]uid: 12046 topic_id: 4155 reply_id: 13075[/import]

This thread was a life saver – or at least a huge time saver.

After 30 minutes of pulling my hair out (which is hard cause I have none), Polo’s solution worked perfectly. IMHO this has got to be a very common issue and this solution should be in the physics collision docs directly. :wink:

Thanks for the thread.
[import]uid: 13859 topic_id: 4155 reply_id: 17436[/import]

That’s awesome, kenwags!
It’s nice to be able to collaborate with others. I also appreciate all the folks here, from whom I have learned a ton!

Good luck!
Polo [import]uid: 12046 topic_id: 4155 reply_id: 17492[/import]