Skip to content

Incorrect behavior when used with js backend #21

@pietroppeter

Description

@pietroppeter

I have tried using polymorph with js backend and I am getting incorrect behavior.

The example I am using is taken from simple physics example (see here for a document showing this behavior).
When the following code is compiled using nim js backend:

  • it starts with Pos (0, 0) instead of (1, 1) (and wrong velociy)
  • at each step neither move nor gravity are applied, only bounce is applied.
import polymorph

# Parse some types as components.
register defaultCompOpts:
  type
    Pos = object
      x, y: float
    Vel = object
      x, y: float
    Gravity = object
      strength: float
    Bounce = object   # A dataless 'tag' component.

makeSystem "move", [Pos, Vel]:
  # Calculate basic movement.
  all:
    echo "move"
    pos.x += vel.x
    pos.y += vel.y

makeSystem "gravity", [Vel, Gravity]:
  # Apply a gravity force to 'Vel'.
  all:
    vel.y -= gravity.strength

makeSystem "bounce", [Pos, Vel, Bounce]:
  all:
    # Correct 'Pos.y' to never goes below zero, enacting a simple bounce
    # to 'Vel.y' if this occurs.
    if pos.y <= 0.0:
      pos.y = 0.0
      vel.y = abs(vel.y) * 0.5

# Generate the framework and system procedures.
makeEcsCommit "run"

let
  ball = newEntityWith(
    Pos(x: 0.0, y: 0.0),
    Vel(x: 1.0, y: 1.0),
    Gravity(strength: 1.0),
    Bounce()
  ) 

for i in 0 ..< 4:
  run()
  echo ball

Here are the results in js console (truncated at the end):

Schermata 2022-11-13 alle 21 56 28

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions