5

I have a small example script, where I have a circle (radius:40), which falls down to the ground. But when I increase its radius to 80 then, only the graphics of the circle seem to change, but not the physics:

<html>
<body></body>
</html>
<script src="matter.js"></script>
<script>
var Engine = Matter.Engine,
    World = Matter.World,
    Bodies = Matter.Bodies
engine = Engine.create(document.body,{render:{options:{wireframes: false}}})
engine.render.options.background = "#7f7f7f"
ground = Bodies.rectangle(400,590,800,20,{isStatic:true})
World.add(engine.world, ground)
circle = Bodies.circle(400,20,40,{render:{fillStyle:"#0000ff"}})
World.add(engine.world, circle)
Engine.run(engine)
setTimeout(increaseRadius, 1500)

function increaseRadius(){
  circle.circleRadius = 80
}
</script>

enter image description here

d0n.key
  • 1,318
  • 2
  • 18
  • 39

1 Answers1

3

My guess is that with your approach you also have to call World.add(engine.world, circle) again and possibly remove the previous circle with the smaller radius since the engine works with a copy of the circle. Alternatively, you might be able to call the scale method on the body: http://brm.io/matter-js-docs/classes/Body.html#method_scale

benbo
  • 1,471
  • 1
  • 16
  • 29
  • But then all the other physics are gone, at least if the circle is moving. But I see, if I can restore these things.. – d0n.key Sep 20 '15 at 18:11
  • I assume that there is no pointer to the global circle you created when you pass it to world but that a copy of it is created. So when you change the radius of the circle, it does not affect the copy that is actually being used. – benbo Sep 20 '15 at 18:13
  • You might be able to call the scale method on the body: http://brm.io/matter-js-docs/classes/Body.html#method_scale – benbo Sep 20 '15 at 18:16
  • Thank you!, please update the post, so I can rate for the answer – d0n.key Sep 20 '15 at 18:18
  • By the way, before I open another thread for that, do you know a way to turn of collision for a single body? So that it simply gos through all other objects? – d0n.key Sep 20 '15 at 18:29
  • You should be able to assign a different groupId to bodies that you do not want to collide http://brm.io/matter-js-docs/classes/Body.html#property_groupId – benbo Sep 20 '15 at 20:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90178/discussion-between-unknown-and-benbo). – d0n.key Sep 20 '15 at 21:21
  • 2
    @benbo is right, you should use `Body.scale` if you need to change the size of a body after creation. Note that `body.circleRadius` is only used as a hint for smoother circle rendering. Regarding your question about disabling collisions, see https://stackoverflow.com/questions/32683832/javascript-matter-js-disable-collision-for-one-body – liabru Nov 03 '15 at 20:44
  • Another problem with removing the object during a collision loop is if the object ever collides with two objects at once you might not be able to recognise the object because it has been removed from the world and likely your own references and you might be looking for the new object. – matthew.tuck Jun 10 '16 at 14:54