UnderwaterApps / overlap2d-runtime-libgdx

Overlap2D - UI and Level Editor libgdx runtime
http://overlap2d.com
Other
170 stars 96 forks source link

The sheep fall through the ground not stop on it. #62

Closed bangive closed 8 years ago

bangive commented 8 years ago

I have followed video tutorial from overlap2d homepage link. But i cannot make the sheep stop at the ground. It fall through the ground. Please help me resolve this problem. Here is the link of my project for you to check.

azakhary commented 8 years ago

I hate to be that guy :D but as I mentioned earlier. Bugs with editor go to editor repo, bugs with runtime go to this repo. And questions on "how do I do that" or "what am I doing wrong" go to forum. It's not that I am against this being here, it's just others will not see this topic to help you out, and they are more active in forums, so your best chance is there :)

bangive commented 8 years ago

Thank you i'm already post to forum but no one help.

azakhary commented 8 years ago

Well the part where sheep is controlled and moved is in Player.java have you looked through the code there and tried to understand what it does? Because the problem of why it does not stop is more then obvious. Just look at the act method, and see what it does :)

bangive commented 8 years ago

I don't understand some of the code. Here is the Player.java

package com.test.superninja;

import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.uwsoft.editor.renderer.scripts.IScript;
import com.uwsoft.editor.renderer.components.TransformComponent;
import com.uwsoft.editor.renderer.components.DimensionsComponent;
import com.uwsoft.editor.renderer.utils.ComponentRetriever;
import com.uwsoft.editor.renderer.physics.PhysicsBodyLoader;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.RayCastCallback;
import com.badlogic.gdx.physics.box2d.World;

public class Player implements IScript {

  private Entity player;
  private TransformComponent transformComponent;
  private DimensionsComponent dimensionsComponent;
  private Vector2 speed;
  private float gravity = -500f;
  private float jumpSpeed = 170f;
  private World world;

  public Player(World world) {
    this.world = world;
  }

  @Override
  public void init(Entity entity) {
    player = entity;
    transformComponent = ComponentRetriever.get(entity, TransformComponent.class);
    dimensionsComponent = ComponentRetriever.get(entity, DimensionsComponent.class);
    speed = new Vector2(50, 0);
  }
  @Override
  public void act(float delta) {
    //transformComponent.scaleY = 0.5;
    if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
      transformComponent.x -= speed.x * delta;
      transformComponent.scaleX = -1f;
    }
    if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
      transformComponent.x += speed.x * delta;
      transformComponent.scaleX = 1f;
    }
    if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) {
      speed.y = jumpSpeed;
    }
    speed.y += gravity * delta;
    transformComponent.y += speed.y * delta;
    /*
    if (transformComponent.y < 16f){
      speed.y = 0;
      transformComponent.y = 16f;
    }
    */
  }
  private void rayCast() {
    float rayGap = dimensionsComponent.height / 2;

    // Ray size is the exact size of the deltaY change we plan for this frame
    float raySize = -(speed.y) * Gdx.graphics.getDeltaTime();

    //if(raySize < 5f) raySize = 5f;

    // only check for collisions when moving down
    if (speed.y > 0) return;

    // Vectors of ray from middle middle
    Vector2 rayFrom = new Vector2((transformComponent.x + dimensionsComponent.width / 2) * PhysicsBodyLoader.getScale(), (transformComponent.y + rayGap) * PhysicsBodyLoader.getScale());
    Vector2 rayTo = new Vector2((transformComponent.x + dimensionsComponent.width / 2) * PhysicsBodyLoader.getScale(), (transformComponent.y - raySize) * PhysicsBodyLoader.getScale());
    // Cast the ray
    world.rayCast(new RayCastCallback() {
      @Override
      public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
        // Stop the player
        speed.y = 0;

        // reposition player slightly upper the collision point
        transformComponent.y  = point.y / PhysicsBodyLoader.getScale() + 0.1f;

        return 0;
      }
    }, rayFrom, rayTo);
  }
  public float getX() {
    return transformComponent.x;
  }
  public float getY() {
    return transformComponent.y;
  }
  public float getWidth() {
    return dimensionsComponent.width;
  }
  @Override
  public void dispose() {

  }
}
azakhary commented 8 years ago

just look at the act() method, it's pretty simple, there is nothing complicated to not being able to understand it.

azakhary commented 8 years ago

what do you think this might do:

if (transformComponent.y < 16f){
      speed.y = 0;
      transformComponent.y = 16f;
}
bangive commented 8 years ago

This code will stop it on the ground but i think it's not collision detection. And in your video you don't need this code but the sheep can still stop on the ground.

azakhary commented 8 years ago

I was just curious why is this part "commented out"

azakhary commented 8 years ago

Although it is true that it should have been mainly stopped by raycast instead.

azakhary commented 8 years ago

Are you sure physics is enabled?

bangive commented 8 years ago

Because the tutorial video did that i just followed. I agree that it should stop by raycast. Im sure physics is enabled.

azakhary commented 8 years ago

I'll try to test it in near hour.

bangive commented 8 years ago

Thank you very much.

bangive commented 8 years ago

I'm going to sleep. You don't need to test it right away. Have a good day.

azakhary commented 8 years ago

when you wake up, please also post the o2d project, not the codes, so I can open it and see if polygons are properly set up.

bangive commented 8 years ago

Sorry for delay, here is the link of o2d project

bangive commented 8 years ago

I've post a question on Stackoverflow and my problem is solved. I just forgot to call raycast() function. Thank you very much for your help.