LibGDX + Ashley, Entity is stationary (Mostly)

  Ok so I have just started using Ashley and have a visible entity working, for the most part. The problem is, its 'Movement' system for some reason is making my entity move back and forth. Can someone explain what the diddly is going on?

Player Movement Class -

public class SystemPlayerMovement extends EntitySystem {
	
	private ImmutableArray<Entity> entities;

    private ComponentMapper<ComponentPosition> pm = ComponentMapper.getFor(ComponentPosition.class);
    private ComponentMapper<ComponentVelocity> vm = ComponentMapper.getFor(ComponentVelocity.class);

    public SystemPlayerMovement(){}
    
    
    @SuppressWarnings("unchecked")
	public void addedToEngine(Engine engine) {
        entities = engine.getEntitiesFor(Family.all(ComponentPosition.class, ComponentVelocity.class).get());
    }

    public void update(float deltaTime) {
        for (int i = 0; i < entities.size(); ++i) {
            Entity entity = entities.get(i);
            ComponentPosition position = pm.get(entity);
            ComponentVelocity velocity = vm.get(entity);

            position.x += velocity.velocityX * deltaTime;
            position.y += velocity.velocityY * deltaTime;
            
            System.out.println(position.x);
            
        }
    }
}

Example of output of entities x position -

400.13358
400.26715
400.20633
400.41266
400.2035
400.40698

Check where your velocityX is modified. Also make sure that your deltaTime is correct.

This looks perfectly fine, it’s the default code from github ;D.
Did you try printing the currently selected entityID + speedX/Y + positionX/Y simultaneously within the loop?
The info you are providing does not have any real data that could help with debugging.

Something could be funky with the velocity or delta value passed to the function.
Did you reuse position/velocity component or Entity objects or something?

This is where I create my entity

entityPlayer.add(new ComponentPosition(400,400));
entityPlayer.add(new ComponentVelocity(200, 0));

My Stuff is being updated in the render() method of my game screen


// Update Stuff
		engine.update(delta);
	
		
		spriteBatch.begin();
		systemRender.update(delta, spriteBatch);
		spriteBatch.end();

This is my position component class

public class ComponentPosition implements Component {
		public float x;
		public float y;
		
		public ComponentPosition(float x, float y){
			this.x = x;
			this.y = y;
		}

}

And this is my velocity component class

public class ComponentVelocity implements Component {
	public float velocityX;
	public float velocityY;
	
	public ComponentVelocity(float VelocityX, float VelocityY){
		this.velocityX = VelocityX;
		this.velocityY = VelocityY;
	}

}

I have tried both using Gdx.graphics.getDeltaTime();, and the provided delta time in the render method of the screen.

What is the unit of delta time here? If the unit is in seconds, then I’m sure that the delta is a fraction, and hence multiplying the velocities by it will only cause a negligible movement. If that is the case, just increase the velocity values.

His values change in both directions, that does not sound like a problem that usually arises when the fps gets too high (=>very small delta going towards zero).
You are limiting your fps I hope?

Also, reading does not hurt ::slight_smile:

[quote]Did you try printing the currently selected entityID + speedX/Y + positionX/Y simultaneously within the loop?
[/quote]
Do this, I’m pretty sure the issue will jump towards your face as soon as you take a look at the whole debug data, not just one isolated value.

Do this, I’m pretty sure the issue will jump towards your face as soon as you take a look at the whole debug data, not just one isolated value.
[/quote]
Doing this results with this

com.badlogic.ashley.core.Entity@4801d256
 X Position : -33.20369
 Y Position : 50.0
 Velocity X : 2000.0
 Velocity Y : 0.0
 Delta Time : 0.017101845
com.badlogic.ashley.core.Entity@284a391a
 X Position : -37.87075
 Y Position : 50.0
 Velocity X : 2000.0
 Velocity Y : 0.0
 Delta Time : 0.019435376
com.badlogic.ashley.core.Entity@38388ce5
 X Position : -37.358574
 Y Position : 50.0
 Velocity X : 2000.0
 Velocity Y : 0.0
 Delta Time : 0.019179286

There is no way this is because of the float multiplication/addition: 2000*0.019 = 38. The whole calculation is well in the accurate range.
There must be something wrong under the hood, your Entity should be somewhere around (60,50) within 3 iterations with that velocity and delta value, but it isn’t.
Seems like something is f**ky with ashley or how it’s set it up in your project, I can’t help you there.
Edit: Is your Entity list in the system getting updated? it’s an ImmutableArray…

I would recommend using Artemis-odb or cheking if your ashley-setup differs from what they tell you in the libgdx/ashley wiki.

Should I be using something OTHER than an immutable array. Also, there IS something else going on. I removed the delta time multiplication, and now its still doing the same, its almost like it just teleporting back and forth. In the below output it looks like it never moved, but in my screen its blinking.

com.badlogic.ashley.core.Entity@5ee67271
 X Position : -1999.0
 Y Position : 50.0
 Velocity X : 2000.0
 Velocity Y : 0.0
 Delta Time : 0.015977852
com.badlogic.ashley.core.Entity@366eb53d
 X Position : -1999.0
 Y Position : 50.0
 Velocity X : 2000.0
 Velocity Y : 0.0
 Delta Time : 0.020056264
com.badlogic.ashley.core.Entity@57098421
 X Position : -1999.0
 Y Position : 50.0
 Velocity X : 2000.0
 Velocity Y : 0.0
 Delta Time : 0.012612802

You can use the ImmutableArray, but keep in mind that it needs to be queried again every time you add or remove entities, so that the stuff you add actually gets worked on.

https://libgdx.badlogicgames.com/ashley/docs/com/badlogic/ashley/core/EntitySystem.html#addedToEngine-com.badlogic.ashley.core.Engine-

Right now you query the Entities only once when your system is added to the simulation, this means you can only work with what’s there is the beginning, that’s bad.
You could try getting that list at the beginning of every iteration.

Is this what you mean? Doing this gives the same result.


@SuppressWarnings("unchecked")
	public void update(float deltaTime) {
		entities = engine.getEntitiesFor(Family.all(ComponentPosition.class, ComponentVelocity.class).get());
        for (Entity entity: entities) {
            ComponentPosition position = pm.get(entity);
            ComponentVelocity velocity = vm.get(entity);

            if(Gdx.input.isKeyPressed(Keys.W)){
            position.x -= velocity.velocityX;
            System.out.println(entity.toString());
            System.out.println(" X Position : "+position.x);
            System.out.println(" Y Position : "+position.y);
            System.out.println(" Velocity X : "+velocity.velocityX);
            System.out.println(" Velocity Y : "+velocity.velocityY);
            System.out.println(" Delta Time : "+deltaTime);
            }
            
    
            
        }
    }

Well… that’s as far as I can guide ya.
My tip: use artemis-odb, it’s way faster and well organized with a lot of functionality ;D

ok so I started using the Iterating system and now it’s acting slightly different. Now it’s moving the entity once and just stopping, the loop itself is running just fine but it feels like theres something stopping it from doing anything.

public class SystemPlayerMovement extends IteratingSystem {
	
	@SuppressWarnings("unchecked")
	public SystemPlayerMovement() {
		super(Family.all(ComponentPosition.class, ComponentVelocity.class).get());
		
	}
	
	@Override
	protected void processEntity(Entity entity, float deltaTime) {
		float posX = entity.getComponent(ComponentPosition.class).x;
		float velocityX = entity.getComponent(ComponentVelocity.class).velocityX;
		posX += velocityX;
		System.out.println(posX);
	}
}

What it outputs (I changed the velocity to 100.5, the player starts at 1):

101.5
101.5
101.5
101.5
101.5
101.5
101.5
101.5
101.5
101.5

Variables do not behave like objects, you need to assign the new position to the field inside ComponentPosition.

try this:

public class SystemPlayerMovement extends IteratingSystem {
   
   @SuppressWarnings("unchecked")
   public SystemPlayerMovement() {
      super(Family.all(ComponentPosition.class, ComponentVelocity.class).get());
      
   }
   
   @Override
   protected void processEntity(Entity entity, float deltaTime) {
      entity.getComponent(ComponentPosition.class).x += entity.getComponent(ComponentVelocity.class).velocityX * deltaTime;
      entity.getComponent(ComponentPosition.class).y += entity.getComponent(ComponentVelocity.class).velocityY * deltaTime;
   }
}

This should work now!

The good news : It’s moving
The bad news : Its moving back and forth again.

I’m gonna die.

public class SystemPlayerMovement extends IteratingSystem {
	
	@SuppressWarnings("unchecked")
	public SystemPlayerMovement() {
		super(Family.all(ComponentPosition.class, ComponentVelocity.class).get());
		
	}
	
	@Override
	protected void processEntity(Entity entity, float deltaTime) {
		entity.getComponent(ComponentPosition.class).x = Gdx.input.getX();
		//entity.getComponent(ComponentPosition.class).x += entity.getComponent(ComponentVelocity.class).velocityX * deltaTime;
	   // entity.getComponent(ComponentPosition.class).y += entity.getComponent(ComponentVelocity.class).velocityY * deltaTime;
	    System.out.println(entity.getComponent(ComponentPosition.class).x);
	}
}

Making it follow my mouse works perfectly fine, maybe I should change the format of my velocity component, or position?

Update : Nothing is working

Summary: this Ashley thing is complicated and you don’t know what it is doing.

Question: What compelling reason do you have to use Ashley?

A ton of code from somebody else usually does not solve problems you have not actually encountered yet.

I just wanted to test out Ashley to learn it m8, theres not really a ‘reason’. I just want to know why something as simple as this is going berserk.

PhXovoaXsSE

Still haven’t found a solution, should I just end it guys?