Closed bangive closed 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 :)
Thank you i'm already post to forum but no one help.
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 :)
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() {
}
}
just look at the act() method, it's pretty simple, there is nothing complicated to not being able to understand it.
what do you think this might do:
if (transformComponent.y < 16f){
speed.y = 0;
transformComponent.y = 16f;
}
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.
I was just curious why is this part "commented out"
Although it is true that it should have been mainly stopped by raycast instead.
Are you sure physics is enabled?
Because the tutorial video did that i just followed. I agree that it should stop by raycast. Im sure physics is enabled.
I'll try to test it in near hour.
Thank you very much.
I'm going to sleep. You don't need to test it right away. Have a good day.
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.
Sorry for delay, here is the link of o2d project
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.
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.