Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

My Matter js doesnt have gravity all Objects are static #1313

Open
Damithedev opened this issue Aug 21, 2024 · 1 comment
Open

My Matter js doesnt have gravity all Objects are static #1313

Damithedev opened this issue Aug 21, 2024 · 1 comment

Comments

@Damithedev
Copy link

`import { useEffect, useRef } from 'react'
import { Engine, Render, Bodies, World } from 'matter-js'

function Comp (props) {
const scene = useRef()
const isPressed = useRef(false)
const engine = useRef(Engine.create())

useEffect(() => {
const cw = document.body.clientWidth
const ch = document.body.clientHeight

const render = Render.create({
  element: scene.current,
  engine: engine.current,
  options: {
    width: cw,
    height: ch,
    wireframes: false,
    background: 'transparent'
  }
})

World.add(engine.current.world, [
  Bodies.rectangle(cw / 2, -10, cw, 20, { isStatic: true }),
  Bodies.rectangle(-10, ch / 2, 20, ch, { isStatic: true }),
  Bodies.rectangle(cw / 2, ch + 10, cw, 20, { isStatic: true }),
  Bodies.rectangle(cw + 10, ch / 2, 20, ch, { isStatic: true })
])

Engine.run(engine.current)
Render.run(render)

return () => {
  Render.stop(render)
  World.clear(engine.current.world)
  Engine.clear(engine.current)
  render.canvas.remove()
  render.canvas = null
  render.context = null
  render.textures = {}
}

}, [])

const handleDown = () => {
isPressed.current = true
}

const handleUp = () => {
isPressed.current = false
}

const handleAddCircle = e => {
if (isPressed.current) {
const ball = Bodies.circle(
e.clientX,
e.clientY,
10 + Math.random() * 30,
{
mass: 10,
restitution: 0.9,
friction: 0.005,
render: {
fillStyle: '#0000ff'
}
})
World.add(engine.current.world, [ball])
}
}

return (


<div ref={scene} style={{ width: '100%', height: '100%' }} />

)
}

export default Comp`

This is my Component but all object are static

@Shaanz-cmd
Copy link

Because your isStatic property is set to true. Set it's value to false, that should fix your problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants