Created
November 13, 2015 12:26
-
-
Save picopicolab/5153835680729a758045 to your computer and use it in GitHub Desktop.
衝突判定サンプル実装(Intersector クラス利用)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.picopicolab.games.demo; | |
import com.badlogic.gdx.ApplicationAdapter; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.graphics.Color; | |
import com.badlogic.gdx.graphics.FPSLogger; | |
import com.badlogic.gdx.graphics.GL20; | |
import com.badlogic.gdx.graphics.glutils.ShapeRenderer; | |
import com.badlogic.gdx.math.Circle; | |
import com.badlogic.gdx.math.Intersector; | |
import com.badlogic.gdx.math.Rectangle; | |
/** | |
* 衝突判定サンプル実装(Intersector クラス利用) | |
*/ | |
public class DemoGame extends ApplicationAdapter { | |
private ShapeRenderer renderer; | |
private Rectangle rectangle; | |
private Circle circle; | |
private Direction circleDirection; | |
private Direction rectangle2Direction; | |
private float speed; | |
private FPSLogger logger; | |
@Override | |
public void create() { | |
renderer = new ShapeRenderer(); | |
renderer.setAutoShapeType(true); | |
float centerX = Gdx.graphics.getWidth() / 2; | |
float centerY = Gdx.graphics.getHeight() / 2; | |
rectangle = new Rectangle(centerX - 50.0f, centerY - 10.0f, 100.0f, 20.0f); // 画面中央に幅100, 高さ20の四角形 | |
circle = new Circle(Gdx.graphics.getWidth() - 30.0f, 7.5f, 7.5f); | |
circleDirection = Direction.Up; | |
speed = 50.0f; // 図形移動速度 | |
logger = new FPSLogger(); | |
} | |
@Override | |
public void render() { | |
Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | |
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); | |
// 図形移動 | |
float deltaTime = Gdx.graphics.getDeltaTime(); | |
if (Direction.Down == circleDirection) { | |
// 進行方向が下の場合 | |
circle.y -= speed * deltaTime; | |
if (circle.y <= circle.radius) { | |
// 画面下部まで来たら反転 | |
circle.y = circle.radius; | |
circleDirection = Direction.Up; | |
} | |
} else { | |
// 進行方向が上の場合 | |
circle.y += speed * deltaTime; | |
if (circle.y >= Gdx.graphics.getHeight() - circle.radius) { | |
// 画面下部まで来たら反転 | |
circle.y = Gdx.graphics.getHeight() - circle.radius; | |
circleDirection = Direction.Down; | |
} | |
} | |
// 画像描画 | |
renderer.begin(); | |
if (Intersector.overlaps(circle, rectangle)) { | |
// 円と四角形が衝突している(重なっている)場合 | |
renderer.setColor(Color.RED); // 色を緑に指定 | |
} else { | |
// 円と四角形が衝突していない(重なっていない)場合 | |
renderer.setColor(Color.GREEN); // 色を緑に指定 | |
} | |
renderer.set(ShapeRenderer.ShapeType.Filled); // 線にて描画 | |
renderer.rect(rectangle.x, rectangle.y, rectangle.width, rectangle.height); | |
renderer.setColor(Color.YELLOW); // 色を黄色に指定 | |
renderer.set(ShapeRenderer.ShapeType.Filled); // 塗りつぶしにて描画 | |
renderer.circle(circle.x, circle.y, circle.radius); | |
renderer.end(); | |
// FPS 出力 | |
logger.log(); | |
} | |
public enum Direction { | |
Up, Down; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment