Discord
Login
Community
DARK THEME

I still dont understand how to use Matter.Body.applyForce()

update = function() Matter.Engine.update(engine,1000/60) if keyboard.W then Matter.Body.applyForce(box, box.position.x, box.position.y, 0.01,0) end end

I have this bit of code that is supposed to move a box in some direction using Matter.JS physics, but if I try using it, the box disappears and never returns. the main thing I don't understand is how to format the force value. the Microstudio documentation is very vague about how to format it, and I'm pretty sure that's what I'm doing wrong. Thanks!

update = function() 
  Matter.Engine.update(engine,1000/60) 
  if keyboard.W and cooldown == 0 then 
    Matter.Body.applyForce(box, 
      object x = box.position.x y = box.position.y end,
      object x = 0.001 y = 0.009 end )
    cooldown += 60
  end 
  if cooldown > 0 then 
    cooldown -= 1
  end
end

init = function()
  cooldown = 0
  engine = Matter.Engine.create()  
  engine.world.gravity.y = -1
  box = Matter.Bodies.rectangle(0,50,20,20)
  Matter.Composite.add(engine.world,box)
  ground = Matter.Bodies.rectangle(0,-50,200,10,object isStatic=true end)
  Matter.Composite.add(engine.world,ground)
end



draw = function()
  screen.clear()
  screen.drawRect(ground.position.x,ground.position.y,200,10,"rgb(255,0,0)")
  screen.setDrawRotation(box.angle/PI*180)
  screen.drawRect(box.position.x,box.position.y,20,20,"#FF0")
  screen.setDrawRotation(0)  
end

Alright, I see what I was doing wrong, thanks!

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community