Image objects size change (resize)

Hey guys, i need to resize image object by 1 pixel every time it collides with some other object.
I tried using scale, but it resizes the image too much. I need something like:

image-object-in-pixels = image-object-in-pixels + 1 pixel [import]uid: 193035 topic_id: 32870 reply_id: 332870[/import]

Besides the fact that you may run into precision (floating-point math) issues as well as implementation rounding, have you tried something like this:

[lua]local pixels = 1
obj:scale ( (obj.width + pixels) / obj.width, (obj.height + pixels) / obj.height )[/lua]

If you aren’t entirely married to 1-pixel, try 2 to see if it works better. [import]uid: 110228 topic_id: 32870 reply_id: 130629[/import]

Here is a video of the above code in action: http://www.youtube.com/watch?v=477APfUXU7s

The green ships in the video are 52 pixels w/h, the red ships are 48 pixels w/h, and the ‘normal’ ships are 50 pixels w/h

You’ll need to watch at high-resolution to see things best. Even then the red-ship overlap is VERY SUBTLE.

If you want to try this on an android device, you can get the sampler here:

[import]uid: 110228 topic_id: 32870 reply_id: 130634[/import]

Besides the fact that you may run into precision (floating-point math) issues as well as implementation rounding, have you tried something like this:

[lua]local pixels = 1
obj:scale ( (obj.width + pixels) / obj.width, (obj.height + pixels) / obj.height )[/lua]

If you aren’t entirely married to 1-pixel, try 2 to see if it works better. [import]uid: 110228 topic_id: 32870 reply_id: 130629[/import]

Here is a video of the above code in action: http://www.youtube.com/watch?v=477APfUXU7s

The green ships in the video are 52 pixels w/h, the red ships are 48 pixels w/h, and the ‘normal’ ships are 50 pixels w/h

You’ll need to watch at high-resolution to see things best. Even then the red-ship overlap is VERY SUBTLE.

If you want to try this on an android device, you can get the sampler here:

[import]uid: 110228 topic_id: 32870 reply_id: 130634[/import]

Thanks, that will do the job, but now I’m facing another problem, since the object is bigger, i need the object size to work with the collision too. Now even if my object is bigger, it doesn’t affect the collision, it still thinks its on the original size. I’m using physics. [import]uid: 193035 topic_id: 32870 reply_id: 130642[/import]

@alek,

Yes. AFAIK, scaling still doesn’t scale phyics bodies:

http://developer.coronalabs.com/content/game-edition-box2d-physics-engine (see list at bottom of page)

So, you’ll probably have to remove the physics body and then add a new on. Depending on how often you need to do this, it could be… expensive.

I’m sorry I don’t have a better answer at this point, but if I come across one I’ll post it here.

Meanwhile, question to the community: “Anyone out there dealt with scaling and physics bodies in a efficient and effective way yet?”

[import]uid: 110228 topic_id: 32870 reply_id: 130651[/import]

Thanks, that will do the job, but now I’m facing another problem, since the object is bigger, i need the object size to work with the collision too. Now even if my object is bigger, it doesn’t affect the collision, it still thinks its on the original size. I’m using physics. [import]uid: 193035 topic_id: 32870 reply_id: 130642[/import]

@alek,

Yes. AFAIK, scaling still doesn’t scale phyics bodies:

http://developer.coronalabs.com/content/game-edition-box2d-physics-engine (see list at bottom of page)

So, you’ll probably have to remove the physics body and then add a new on. Depending on how often you need to do this, it could be… expensive.

I’m sorry I don’t have a better answer at this point, but if I come across one I’ll post it here.

Meanwhile, question to the community: “Anyone out there dealt with scaling and physics bodies in a efficient and effective way yet?”

[import]uid: 110228 topic_id: 32870 reply_id: 130651[/import]

Hi Alek,
@emaurina suggested the correct approach: remove the current body and add a new one of the proper size/scale. This action is just slightly “expensive” in terms of performance, but not as bad as you might fear. In my experience, the only time it’s noticeable is when you add, for example, 10 new physics bodies in a consecutive process during Runtime, and you’re using an older device, say iPhone 3GS. Certainly you might run into issues if you’re doing this process over and over in your game cycle for many objects, but otherwise it shouldn’t be noticeable.

Brent [import]uid: 9747 topic_id: 32870 reply_id: 130668[/import]

I don’t understand that approach. How can i add new body?
Is it somehow possible to make it work using this approach:

local c = display.newImage(“circle.png”) – my object is circle

– code for scaling the circle

physics.addBody(c, “static”, { radius = updateBiggerRadius(theSameRaidusForTheScaledImage)}) – somehow update the circle radius for the physics to work with that radius [import]uid: 193035 topic_id: 32870 reply_id: 130676[/import]

Hi Alek,
@emaurina suggested the correct approach: remove the current body and add a new one of the proper size/scale. This action is just slightly “expensive” in terms of performance, but not as bad as you might fear. In my experience, the only time it’s noticeable is when you add, for example, 10 new physics bodies in a consecutive process during Runtime, and you’re using an older device, say iPhone 3GS. Certainly you might run into issues if you’re doing this process over and over in your game cycle for many objects, but otherwise it shouldn’t be noticeable.

Brent [import]uid: 9747 topic_id: 32870 reply_id: 130668[/import]

I don’t understand that approach. How can i add new body?
Is it somehow possible to make it work using this approach:

local c = display.newImage(“circle.png”) – my object is circle

– code for scaling the circle

physics.addBody(c, “static”, { radius = updateBiggerRadius(theSameRaidusForTheScaledImage)}) – somehow update the circle radius for the physics to work with that radius [import]uid: 193035 topic_id: 32870 reply_id: 130676[/import]

Hi Alek,
You need to remove the current physics body before adding the new one, using physics.removeBody( c ). Then, you plug in the new “scaled” radius value into the “addBody” function and it should reflect the change.
[import]uid: 9747 topic_id: 32870 reply_id: 130823[/import]

Hi Alek,
You need to remove the current physics body before adding the new one, using physics.removeBody( c ). Then, you plug in the new “scaled” radius value into the “addBody” function and it should reflect the change.
[import]uid: 9747 topic_id: 32870 reply_id: 130823[/import]

Meanwhile, question to the community: “Anyone out there dealt with scaling and physics bodies in a efficient and effective way yet?”

After hours of playing around, I figured out a solution to this issue. Physics bodies don’t recognize scaling relating to what the engine **thinks** the size of the object is. So if you physics.addBody to an object that has been scaled, physics thinks it is still the original size and treats it as such. It causes objects that have been scaled down, for example, to collision with other physics objects as if it was larger. The illusion is that it repels object farther away from it.

The functional solution I found is that physics DOES recognize objects that have new width and heigh parameters defined. So changing the scaling to the following fixed everything for me. Instead of scaling (xScale/yScale) use:

object.width = object.width*.2

object.height = object.height*.2

In this case I’m essentially scaling the object down to 20% and now physics works properly on that object. You replace “object” with your object name.

Hope this helps some!

Thanks, i will try that, if it works it could improve my game a lot :slight_smile:

Meanwhile, question to the community: “Anyone out there dealt with scaling and physics bodies in a efficient and effective way yet?”

After hours of playing around, I figured out a solution to this issue. Physics bodies don’t recognize scaling relating to what the engine **thinks** the size of the object is. So if you physics.addBody to an object that has been scaled, physics thinks it is still the original size and treats it as such. It causes objects that have been scaled down, for example, to collision with other physics objects as if it was larger. The illusion is that it repels object farther away from it.

The functional solution I found is that physics DOES recognize objects that have new width and heigh parameters defined. So changing the scaling to the following fixed everything for me. Instead of scaling (xScale/yScale) use:

object.width = object.width*.2

object.height = object.height*.2

In this case I’m essentially scaling the object down to 20% and now physics works properly on that object. You replace “object” with your object name.

Hope this helps some!

Thanks, i will try that, if it works it could improve my game a lot :slight_smile: