Scaling physics bodies attached to sprites?

As things are, when I scale a sprite instance down, the physics body does not seem to be affected. This results in odd looking collisions. Will this be in Alpha 3 or am I missing something?

I’m doing the following things in this order:

  • set up sprite from sprite sheet
  • create sprite instance
  • set up sprite instance with sprite set
  • scale sprite instance (down)
  • attach physics body

[import]uid: 6259 topic_id: 1563 reply_id: 301563[/import]

Same goes for positioning and rotating the physics bodies attached to sprites/other display objects. [import]uid: 6259 topic_id: 1563 reply_id: 4449[/import]

Correct, scaling a physics body is not currently supported. Box2D bodies literally cannot be resized or distorted, since it’s a rigid-body physics engine.

However, it should be possible to automatically destroy/create a new body in a convenient way – no specific version ETA on that, since it’s still in development.

Rotating/positioning should work normally, though, since those changes are automatically forced back into the physics world. However, note that a “dynamic” body type should mostly be left under control of the physics engine, and not constantly moved around by manually setting its parameters, since it’s probably a bit expensive to continually force manual changes into the engine’s objects. [import]uid: 3007 topic_id: 1563 reply_id: 4452[/import]

Has anyone experimented with creating and destroying the bodies according to scale? [import]uid: 8192 topic_id: 1563 reply_id: 6783[/import]

I am trying to do this but have one question. the object.width is the width of the object at 1 scale or at whatever scale the object is currently in?

for example if my object comes from a 32 px graphic but then I scale it to .1 so say 3 px will the object.width now be 3 px or still 32?

Thanks [import]uid: 8192 topic_id: 1563 reply_id: 6786[/import]

Yes. Scaling to 0.1 means scaling to 10%. [import]uid: 6259 topic_id: 1563 reply_id: 6891[/import]

Yes, but the physics body doesn’t scale at all. It seems to be locked by the sprite sheet. I can’t seem to make it work. Have you had any luck? [import]uid: 8192 topic_id: 1563 reply_id: 6893[/import]

Someone at Ansca. Can you please shine in? Is this a bug or just my poor coding abilities. :slight_smile: [import]uid: 8192 topic_id: 1563 reply_id: 6943[/import]

@amigoni: to be clear, you can’t scale a physics body at all. If you scale its display object, it may look different, but the physical boundaries won’t actually change. Use physics.setDrawMode( “hybrid” ) to see an overlay that will probably demonstrate this better.

On the other hand, you CAN put all the bodies into a big group and scale the entire world at once. The classic iPhone game “Rolando”, also based on Box2D, is a nice example of this.

On the third hand, it sounds like you’re having a different problem, with a sprite not scaling visually? In other words, is this a physics problem or a visual problem? [import]uid: 3007 topic_id: 1563 reply_id: 6978[/import]

@evank. I am trying to scale both the sprite and the body. The sprite scales fine but the body stays the same. I followed your suggestion of removing an creating the body. But it doesn’t work. So as the object scales up from .1 to 1 I am asking to remove and add the body 4 times. At .1 at .25 at .5 at .75 and at 1. This is not working though. The body doesn’t seem to get removed at all. I really don’t know since I don’t get any errors.

Is there any workaround to this? [import]uid: 8192 topic_id: 1563 reply_id: 6983[/import]

You can always declare the body coordinates in addBody() to be whatever size and shape you want, using a series of (x,y) coordinates:

local spriteShape = { -20,-10, 20,-10, 20,10, 20,-10 }
physics.addBody( mySprite, { density=1.6, friction=0.5, bounce=0.2, shape=spriteShape } )

Of course, this is less convenient than using the convenience methods, but it gives you more control.

That said, I’m wondering why the convenience methods aren’t working as expected in your case. Can you zip your project and send it to support@anscamobile.com so we can check this out? [import]uid: 3007 topic_id: 1563 reply_id: 7062[/import]

@evank

Im trying to scale the World group but the boundary of the children do not scale with the group as you said above

Do you know what is happening?

[code]
local physics = require (“physics”)
physics.start()
physics.setGravity (0,0)
physics.setDrawMode( “hybrid” )

local worldGroup = display.newGroup()
local trackGroup = display.newGroup()
worldGroup:insert( trackGroup )

track = display.newImage( “trackStraight.png”)
physics.addBody( track, “dynamic”,
{ density=3.0, friction=0.5, bounce=0.2 }
)
trackGroup:insert( track )

worldGroup.xScale = 0.1
worldGroup.yScale = 0.1
[/code] [import]uid: 5354 topic_id: 1563 reply_id: 7344[/import]

i know the Box2D object can’t be scaled *after* creation, but it would be good if we could at least scale the sprite first and then the physics body takes on the shape of the scaled sprite.

this currently doesn’t work either

it would solve some problems at least

[lua]local s = math.random(1.5)
j = display.newImage(“football.png”);
j.x = 60 + math.random( 160 )
j.y = -100
j:scale(0.3+s,0.3+s)
physics.addBody( j, { density=0.9, friction=0.3, bounce=0.6} )[/lua] [import]uid: 6645 topic_id: 1563 reply_id: 10197[/import]

@jmp909 – hold on, that case should work already. Logging it as internal bug #1820 for investigation. [import]uid: 3007 topic_id: 1563 reply_id: 10219[/import]

@evank Awesome. That’s what I have been trying to describe. I guess I should post more code. [import]uid: 8192 topic_id: 1563 reply_id: 10254[/import]

Did this get fixed? and/or does it have a new bug id # as 1820 didn’t show up at bugs.anscamobile.com

thanks,

ben [import]uid: 9048 topic_id: 1563 reply_id: 29524[/import]

Hello,

+1

It isnt fixed. Tested with stable build and daily builds. Any estimated time ?

Regards.
Francisco. [import]uid: 11749 topic_id: 1563 reply_id: 29959[/import]

Bump! Is this still a logged issue? I cant find it in the bugbase, but it doesnt work as of the latest stable release either, ansca? [import]uid: 67379 topic_id: 1563 reply_id: 43504[/import]

Has anyone found a fix/workaround for this issue? [import]uid: 135394 topic_id: 1563 reply_id: 122859[/import]

Has anyone found a fix/workaround for this issue? [import]uid: 135394 topic_id: 1563 reply_id: 122859[/import]