Finished the collision detection.

This commit is contained in:
2014-10-24 08:24:10 -04:30
parent c8f8512fcf
commit 83c0b894f7
5 changed files with 68 additions and 24 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 84 KiB

After

Width:  |  Height:  |  Size: 22 KiB

View File

@@ -109,7 +109,8 @@ public class PongEntityInitializer extends EntityInitializerBase {
if(!entitiesCreated) if(!entitiesCreated)
throw new IllegalStateException("Entities have not been created before setting assets."); throw new IllegalStateException("Entities have not been created before setting assets.");
Vector2 randomVector = new Vector2().set(Vector2.X).setAngle(MathUtils.random(0, 360)); Vector2 randomVector = new Vector2().set(Vector2.X).setAngle(MathUtils.random(-60, 60));
int randomSign = MathUtils.random(-1, 1) >= 0 ? 1 : -1;
TextureAtlas atlas = loader.getAsset("data/gfx/textures/pong_atlas.atlas", TextureAtlas.class); TextureAtlas atlas = loader.getAsset("data/gfx/textures/pong_atlas.atlas", TextureAtlas.class);
Texture bckg = loader.getAsset("data/gfx/textures/bckg.png", Texture.class); Texture bckg = loader.getAsset("data/gfx/textures/bckg.png", Texture.class);
@@ -121,7 +122,7 @@ public class PongEntityInitializer extends EntityInitializerBase {
Mappers.spriteMapper.get(ball).sprite = atlas.createSprite("ball"); Mappers.spriteMapper.get(ball).sprite = atlas.createSprite("ball");
Mappers.positionMapper.get(ball).setXY(-(Mappers.spriteMapper.get(ball).sprite.getWidth() / 2), -(Mappers.spriteMapper.get(ball).sprite.getHeight() / 2)); Mappers.positionMapper.get(ball).setXY(-(Mappers.spriteMapper.get(ball).sprite.getWidth() / 2), -(Mappers.spriteMapper.get(ball).sprite.getHeight() / 2));
Mappers.velocityMapper.get(ball).setXY(randomVector.x * 475.0f, randomVector.y * 475.0f); Mappers.velocityMapper.get(ball).setXY(randomVector.x * 475.0f * randomSign, randomVector.y * 475.0f * randomSign);
Mappers.bboxMapper.get(ball).bbox.set(Mappers.spriteMapper.get(ball).sprite.getBoundingRectangle()); Mappers.bboxMapper.get(ball).bbox.set(Mappers.spriteMapper.get(ball).sprite.getBoundingRectangle());
Mappers.soundMapper.get(ball).path = "data/sfx/BounceYoFrankie.ogg"; Mappers.soundMapper.get(ball).path = "data/sfx/BounceYoFrankie.ogg";

View File

@@ -22,6 +22,7 @@ import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.systems.IteratingSystem; import com.badlogic.ashley.systems.IteratingSystem;
import com.badlogic.ashley.utils.ImmutableArray; import com.badlogic.ashley.utils.ImmutableArray;
import com.badlogic.gdx.math.MathUtils; import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.math.Vector2;
import com.gamejolt.mikykr5.poukemon.ProjectConstants; import com.gamejolt.mikykr5.poukemon.ProjectConstants;
import com.gamejolt.mikykr5.poukemon.ecs.components.BoundingBoxComponent; import com.gamejolt.mikykr5.poukemon.ecs.components.BoundingBoxComponent;
@@ -42,7 +43,6 @@ public class CollisionDetectionSystem extends IteratingSystem {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public CollisionDetectionSystem(Engine engine){ public CollisionDetectionSystem(Engine engine){
//super(Family.getFor(PositionComponent.class, BoundingBoxComponent.class, VelocityComponent.class));
super(Family.getFor(ComponentType.getBitsFor(PositionComponent.class, BoundingBoxComponent.class, VelocityComponent.class), ComponentType.getBitsFor(), ComponentType.getBitsFor(PlayerComponent.class))); super(Family.getFor(ComponentType.getBitsFor(PositionComponent.class, BoundingBoxComponent.class, VelocityComponent.class), ComponentType.getBitsFor(), ComponentType.getBitsFor(PlayerComponent.class)));
collidables = engine.getEntitiesFor(Family.getFor(BoundingBoxComponent.class)); collidables = engine.getEntitiesFor(Family.getFor(BoundingBoxComponent.class));
@@ -63,8 +63,6 @@ public class CollisionDetectionSystem extends IteratingSystem {
// Check if this entity is within the screen. // Check if this entity is within the screen.
// If the entity collides with any of the borders then bounce or score as needed. // If the entity collides with any of the borders then bounce or score as needed.
if(position.x < screenLeftBorder){ if(position.x < screenLeftBorder){
position.x = screenLeftBorder;
velocity.vx = velocity.vx < 0.0f ? -velocity.vx : velocity.vx;
resetEntity(entity); resetEntity(entity);
message = new InterSystemMessage(ScoringSystem.class.getCanonicalName()); message = new InterSystemMessage(ScoringSystem.class.getCanonicalName());
@@ -77,8 +75,6 @@ public class CollisionDetectionSystem extends IteratingSystem {
} }
if(position.x + bounds.bbox.getWidth() >= screenRightBorder){ if(position.x + bounds.bbox.getWidth() >= screenRightBorder){
position.x = screenRightBorder - bounds.bbox.getWidth();
velocity.vx = velocity.vx > 0.0f ? -velocity.vx : velocity.vx;
resetEntity(entity); resetEntity(entity);
message = new InterSystemMessage(ScoringSystem.class.getCanonicalName()); message = new InterSystemMessage(ScoringSystem.class.getCanonicalName());
@@ -113,9 +109,39 @@ public class CollisionDetectionSystem extends IteratingSystem {
} }
for(int i = 0; i < collidables.size(); i++){ for(int i = 0; i < collidables.size(); i++){
BoundingBoxComponent collidable;
PositionComponent colPosition;
if(collidables.get(i).getIndex() == entity.getIndex()){ if(collidables.get(i).getIndex() == entity.getIndex()){
continue; continue;
}else{ }else{
collidable = Mappers.bboxMapper.get(collidables.get(i));
colPosition = Mappers.positionMapper.get(collidables.get(i));
if(colPosition == null)
continue;
bounds.bbox.setPosition(position.x, position.y);
collidable.bbox.setPosition(colPosition.x, colPosition.y);
if(collidesLeft(bounds.bbox, collidable.bbox)){
velocity.vx = velocity.vx < 0.0f ? -velocity.vx : velocity.vx;
accelerate(velocity);
if(sound != null){
message = new InterSystemMessage(SoundSystem.class.getCanonicalName());
message.data.put("PLAY", sound.path);
InterSystemMessagingQueue.pushMessage(message);
}
}else if(collidesRight(bounds.bbox, collidable.bbox)){
velocity.vx = velocity.vx > 0.0f ? -velocity.vx : velocity.vx;
accelerate(velocity);
if(sound != null){
message = new InterSystemMessage(SoundSystem.class.getCanonicalName());
message.data.put("PLAY", sound.path);
InterSystemMessagingQueue.pushMessage(message);
}
}
} }
} }
@@ -129,13 +155,35 @@ public class CollisionDetectionSystem extends IteratingSystem {
velocity.vy *= 1.03f; velocity.vy *= 1.03f;
} }
private void resetEntity(Entity entity){ private boolean collidesLeft(Rectangle a, Rectangle b){
PositionComponent position = Mappers.positionMapper.get(entity); float leftBottomCornerY, leftTopCornerY, leftCenterY;
SpriteComponent sprite = Mappers.spriteMapper.get(entity);
VelocityComponent velocity = Mappers.velocityMapper.get(entity);
randomVector.set(Vector2.X).setAngle(MathUtils.random(0, 360)); leftBottomCornerY = a.y;
velocity.setXY(randomVector.x * -475, randomVector.y * 475); leftTopCornerY = a.y + a.height;
leftCenterY = a.y + (a.height / 2);
return b.contains(a.x, leftBottomCornerY) || b.contains(a.x, leftTopCornerY) || b.contains(a.x, leftCenterY);
}
private boolean collidesRight(Rectangle a, Rectangle b){
float x, rightBottomCornerY, rightTopCornerY, rightCenterY;
x = a.x + a.width;
rightBottomCornerY = a.y;
rightTopCornerY = a.y + a.height;
rightCenterY = a.y + (a.height / 2);
return b.contains(x, rightBottomCornerY) || b.contains(x, rightTopCornerY) || b.contains(x, rightCenterY);
}
private void resetEntity(Entity entity){
PositionComponent position = Mappers.positionMapper.get(entity);
SpriteComponent sprite = Mappers.spriteMapper.get(entity);
VelocityComponent velocity = Mappers.velocityMapper.get(entity);
int randomSign = MathUtils.random(-1, 1) >= 0 ? 1 : -1;
randomVector.set(Vector2.X).setAngle(MathUtils.random(-60, 60));
velocity.setXY(randomVector.x * -475 * randomSign, randomVector.y * 475 * randomSign);
if(position != null){ if(position != null){
if(sprite != null){ if(sprite != null){

View File

@@ -39,11 +39,11 @@ public class ComputerPlayerPositioningSystem extends IteratingSystem {
@Override @Override
public void processEntity(Entity entity, float deltaTime) { public void processEntity(Entity entity, float deltaTime) {
InterSystemMessage message; InterSystemMessage message;
VelocityComponent velocity = Mappers.velocityMapper.get(entity); VelocityComponent velocity = Mappers.velocityMapper.get(entity);
PositionComponent position = Mappers.positionMapper.get(entity); PositionComponent position = Mappers.positionMapper.get(entity);
PlayerComponent player = Mappers.playerMapper.get(entity); PlayerComponent player = Mappers.playerMapper.get(entity);
BoundingBoxComponent bounds = Mappers.bboxMapper.get(entity); BoundingBoxComponent bounds = Mappers.bboxMapper.get(entity);
if(player.id == PlayerComponent.COMPUTER_PLAYER){ if(player.id == PlayerComponent.COMPUTER_PLAYER){
while((message = InterSystemMessagingQueue.popMessage(ComputerPlayerPositioningSystem.class.getCanonicalName())) != null){ while((message = InterSystemMessagingQueue.popMessage(ComputerPlayerPositioningSystem.class.getCanonicalName())) != null){

View File

@@ -189,12 +189,7 @@ public class InGameState extends BaseState implements AssetsLoadedListener{
private float convertWorldYToFrameBufferY(float y){ private float convertWorldYToFrameBufferY(float y){
Vector3 vec3 = new Vector3(0, y - Math.abs(fbBounds.y - (-(h/2.0f))), 0); Vector3 vec3 = new Vector3(0, y - Math.abs(fbBounds.y - (-(h/2.0f))), 0);
fbCamera.unproject(vec3, -(w / 2.0f), -((h / oldRatio) / 2.0f), w, h / oldRatio);
Gdx.app.log("IN_GAME", "Y before: " + Float.toString(vec3.y));
fbCamera.unproject(vec3, 0, 0, w, h / oldRatio);
Gdx.app.log("IN_GAME", "Y after: " + Float.toString(vec3.y));
return vec3.y; return vec3.y;
} }