fenomas / noa

Experimental voxel game engine.
MIT License
611 stars 87 forks source link

How to use collideTerrain? #151

Closed Trenki closed 3 years ago

Trenki commented 3 years ago

I wanted to check if entities collided with terrain, so I used collideTerrain like CollideEntities, but it didn't work.

this.entityID = ents.add(
    opts.playerStart, // starting location
    opts.playerWidth, opts.playerHeight,
    null, null, // no mesh for now, no meshOffset, 
    true, true
  )
  // make player entity it collide with terrain and other entities
ents.addComponent(this.entityID, ents.names.collideTerrain,{
  onCollide: (other) => {
    console.log('Entity', this.entityID, 'Terrain onCollide', other)
  },
  callback: (other) => {
    console.log('Entity', this.entityID, 'Terrain callback', other)
  }
})
fenomas commented 3 years ago

Hi, the only caveat on collideTerrain is that it assumes the entity already has the physics component (it's the physics engine that actually emits the collision event). Probably noa should emit a log warning if that's not the case I guess.

However for the code you posted, the entity is getting the physics component and the code works as expected for me. To be clear collideTerrain calls a callback property, and it passes the impulse of the collision and the id of the entity. So the expected usage is like:

ents.addComponent(entityID, ents.names.collideTerrain,{
  callback: (impulse, id) => {
    console.log('Entity', id, 'terrain collision, impulse:', impulse)
  }
})

Anyway for me your code runs as expected, and the callback function gets called normally (as soon as the spawned entity falls to the ground). What are you seeing?

Trenki commented 3 years ago

Sorry, I misunderstood the meaning of the function, which made me think the desired effect was not achieved.The effect I want to know is that Entity hits the terrain while walking forward, how should this state be obtained?

fenomas commented 3 years ago

Hi, the callback should get fired when the entity hits terrain while walking, or it does for me. Note that if the entity walks into a one-block barrier, and has the "autoStep" property, then it will step onto the barrier instead of colliding. What behavior are you seeing?

Trenki commented 3 years ago

Hi,Andy.I see, my question is how to make the Entity walk without using receivesInputs.

MCArth commented 3 years ago

Use noa.ents.setPosition(EId, [1, 2, 3]) for direct position setting.

Alternatively you can modify the state of their movement component (assuming they have one) - source is /src/components/movement.js

noa.ents.getMovement(EId)

Check out the API documentation in /doc/API.md - it's very helpful.