Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LibGDX + Ashley, Entity is stationary (Mostly)  (Read 3252 times)
0 Members and 1 Guest are viewing this topic.
Offline MrPork
« Posted 2016-07-17 08:51:50 »

      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 -
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
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 -

1  
2  
3  
4  
5  
6  
400.13358
400.26715
400.20633
400.41266
400.2035
400.40698


"f**k it, maybe it'll work." -Me
Offline trollwarrior1
« Reply #1 - Posted 2016-07-17 11:26:50 »

Check where your velocityX is modified. Also make sure that your deltaTime is correct.
Offline VaTTeRGeR
« Reply #2 - Posted 2016-07-17 11:30:33 »

This looks perfectly fine, it's the default code from github  Grin.
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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline MrPork
« Reply #3 - Posted 2016-07-17 14:37:41 »

This is where I create my entity

1  
2  
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
1  
2  
3  
4  
5  
6  
7  
// Update Stuff
      engine.update(delta);
   
     
      spriteBatch.begin();
      systemRender.update(delta, spriteBatch);
      spriteBatch.end();


This is my position component class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
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.

"f**k it, maybe it'll work." -Me
Offline SHC
« Reply #4 - Posted 2016-07-17 14:48:59 »

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.

Offline VaTTeRGeR
« Reply #5 - Posted 2016-07-17 15:05:53 »

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 Roll Eyes
Quote
Did you try printing the currently selected entityID + speedX/Y + positionX/Y simultaneously within the loop?
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.
Offline MrPork
« Reply #6 - Posted 2016-07-17 15:15:27 »

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 Roll Eyes
Quote
Did you try printing the currently selected entityID + speedX/Y + positionX/Y simultaneously within the loop?
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.


Doing this results with this

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
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

"f**k it, maybe it'll work." -Me
Offline VaTTeRGeR
« Reply #7 - Posted 2016-07-17 15:38:04 »

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.
Offline MrPork
« Reply #8 - Posted 2016-07-17 15:44:35 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
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

"f**k it, maybe it'll work." -Me
Offline VaTTeRGeR
« Reply #9 - Posted 2016-07-17 15:55:04 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline MrPork
« Reply #10 - Posted 2016-07-17 16:21:50 »

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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
@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);
            }
           
   
           
        }
    }

"f**k it, maybe it'll work." -Me
Offline VaTTeRGeR
« Reply #11 - Posted 2016-07-17 17:50:47 »

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 Grin
Offline MrPork
« Reply #12 - Posted 2016-07-17 19:16:47 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
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):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
101.5
101.5
101.5
101.5
101.5
101.5
101.5
101.5
101.5
101.5



"f**k it, maybe it'll work." -Me
Offline VaTTeRGeR
« Reply #13 - Posted 2016-07-17 19:33:26 »

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

try this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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!
Offline MrPork
« Reply #14 - Posted 2016-07-17 21:31:06 »

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

try this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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.

"f**k it, maybe it'll work." -Me
Offline MrPork
« Reply #15 - Posted 2016-07-17 21:52:21 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
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?

"f**k it, maybe it'll work." -Me
Offline MrPork
« Reply #16 - Posted 2016-07-18 00:05:26 »

Update : Nothing is working

"f**k it, maybe it'll work." -Me
Offline ags1

JGO Kernel


Medals: 356
Projects: 7


Make code not war!


« Reply #17 - Posted 2016-07-18 11:00:08 »

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.

Offline MrPork
« Reply #18 - Posted 2016-07-24 21:45:58 »

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.

"f**k it, maybe it'll work." -Me
Offline MrPork
« Reply #19 - Posted 2016-07-25 20:42:00 »

<a href="http://www.youtube.com/v/PhXovoaXsSE?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/PhXovoaXsSE?version=3&amp;hl=en_US&amp;start=</a>

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

"f**k it, maybe it'll work." -Me
Offline VaTTeRGeR
« Reply #20 - Posted 2016-07-25 21:52:02 »

It's gotta end someday...
Click to Play
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (110 views)
2017-09-30 02:57:34

theagentd (136 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (241 views)
2017-08-02 12:19:43

cybrmynd (239 views)
2017-08-02 12:18:09

Sralse (254 views)
2017-07-25 17:13:48

Archive (864 views)
2017-04-27 17:45:51

buddyBro (1008 views)
2017-04-05 03:38:00

CopyableCougar4 (1568 views)
2017-03-24 15:39:42

theagentd (1373 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!