Java LIBGDX水平和垂直视差背景
我想用LibGdx做一个小游戏,现在我有一个太空船,它可以用触摸板在各个方向移动,相机也跟着它。 我正试图完成一个由恒星组成的视差背景,这些恒星的运动取决于宇宙飞船要去的地方 这里是代码,我给你所有的类只是为了确保不要搞砸了,因为我是这个编程代码的新手Java LIBGDX水平和垂直视差背景,java,android,libgdx,parallax,Java,Android,Libgdx,Parallax,我想用LibGdx做一个小游戏,现在我有一个太空船,它可以用触摸板在各个方向移动,相机也跟着它。 我正试图完成一个由恒星组成的视差背景,这些恒星的运动取决于宇宙飞船要去的地方 这里是代码,我给你所有的类只是为了确保不要搞砸了,因为我是这个编程代码的新手 public class TouchPadTest extends OrthographicCamera implements ApplicationListener { public static final int WIDTH=480; pu
public class TouchPadTest extends OrthographicCamera implements ApplicationListener {
public static final int WIDTH=480;
public static final int HEIGHT=800;
private OrthographicCamera camera;
private Stage stage;
private SpriteBatch batch;
private Touchpad touchpad;
private TouchpadStyle touchpadStyle;
private Skin touchpadSkin;
private Drawable touchBackground;
private Drawable touchKnob;
private Texture blockTexture;
private Sprite blockSprite;
private float blockSpeed;
public void create() {
batch = new SpriteBatch();
//Create camera
float aspectRatio = (float) Gdx.graphics.getWidth() / (float) Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.setToOrtho(false, TouchPadTest.WIDTH, TouchPadTest.HEIGHT);
//Create a touchpad skin
touchpadSkin = new Skin();
//Set background image
touchpadSkin.add("touchBackground", new Texture("data/touchBackground.png"));
//Set knob image
touchpadSkin.add("touchKnob", new Texture("data/touchKnob.png"));
//Create TouchPad Style
touchpadStyle = new TouchpadStyle();
//Create Drawable's from TouchPad skin
touchBackground = touchpadSkin.getDrawable("touchBackground");
touchKnob = touchpadSkin.getDrawable("touchKnob");
//Apply the Drawables to the TouchPad Style
touchpadStyle.background = touchBackground;
touchpadStyle.knob = touchKnob;
//Create new TouchPad with the created style
touchpad = new Touchpad(10, touchpadStyle);
//setBounds(x,y,width,height)
touchpad.setBounds(15, 15, 200, 200);
//Create a Stage and add TouchPad
stage = new Stage(new FitViewport(Gdx.graphics.getWidth(),Gdx.graphics.getHeight()));
stage.addActor(touchpad);
Gdx.input.setInputProcessor(stage);
//Create block sprite
blockTexture = new Texture(Gdx.files.internal("data/shuttle2.png"));
blockSprite = new Sprite(blockTexture);
//Set position to centre of the screen
blockSprite.setPosition(Gdx.graphics.getWidth()/2-blockSprite.getWidth()/2, Gdx.graphics.getHeight()/2-blockSprite.getHeight()/2);
blockSpeed=5;
}
public void movePlayer(){
Vector2 v = new Vector2(touchpad.getKnobPercentX(), touchpad.getKnobPercentY());
float angle = v.angle();
if (touchpad.isTouched()){
blockSprite.setRotation(angle);
}
blockSprite.setX(blockSprite.getX() + touchpad.getKnobPercentX()*blockSpeed);
blockSprite.setY(blockSprite.getY() + touchpad.getKnobPercentY()*blockSpeed);
//Draw
camera.position.set(blockSprite.getX() + blockSprite.getWidth() / 2, blockSprite.getY() + blockSprite.getHeight() / 2, 0);
camera.update();
batch.setProjectionMatrix(camera.combined);
}
public void renderBackground() {
//---------------PARALLAX BACKGROUND---------------------//
}
public void dispose() {
}
public void render() {
Gdx.gl.glClearColor(0/255f,5/255f,15/255f,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//Move blockSprite with TouchPad
movePlayer();
batch.begin();
renderBackground();
blockSprite.draw(batch);
batch.end();
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void resize(int width, int height) {
}
}
举一个更好的例子,这是我想要达到的结果:,如果你能帮助我,这将是惊人的。谢谢。这个3层视差背景的工作示例是根据改编而来的,应该可以让您了解如何实现视差效果。使用的三个图像都是1024x1024px
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector3;
public class Test extends ApplicationAdapter implements InputProcessor{
private SpriteBatch batch;
private ParallaxCamera camera;
private Texture bgClose;
private Texture bgMid;
private Texture bgFar;
final Vector3 curr = new Vector3();
final Vector3 last = new Vector3(-1, -1, -1);
final Vector3 delta = new Vector3();
@Override
public void create () {
bgClose = new Texture(Gdx.files.internal("starbg-close.png"));
bgMid = new Texture(Gdx.files.internal("starbg-mid.png"));
bgFar = new Texture(Gdx.files.internal("starbg-far.png"));
camera = new ParallaxCamera(1920,1080);
batch = new SpriteBatch();
Gdx.input.setInputProcessor(this);
}
@Override
public void render () {
//clear screen
Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// background layer, no parallax, centered around origin
batch.setProjectionMatrix(camera.calculateParallaxMatrix(0, 0));
batch.disableBlending();
batch.begin();
batch.draw(bgFar, -(int)(bgFar.getWidth() / 2), -(int)(bgFar.getHeight() / 2));
batch.end();
batch.enableBlending();
batch.setProjectionMatrix(camera.calculateParallaxMatrix(0.25f, 0.25f));
batch.begin();
for (int i = 0; i < 9; i++) {
batch.draw(bgMid, i * bgClose.getWidth() - 512, -512);
}
batch.end();
batch.setProjectionMatrix(camera.calculateParallaxMatrix(.5f, .5f));
batch.begin();
for (int i = 0; i < 9; i++) {
batch.draw(bgClose, i * bgClose.getWidth() - 512, -512);
}
batch.end();
}
//.. omitted empty methods ..//
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
last.set(-1, -1, -1);
return false;
}
@Override
public boolean touchDragged(int x, int y, int pointer) {
camera.unproject(curr.set(x, y, 0));
if (!(last.x == -1 && last.y == -1 && last.z == -1)) {
camera.unproject(delta.set(last.x, last.y, 0));
delta.sub(curr);
camera.position.add(delta.x, delta.y, 0);
}
last.set(x, y, 0);
return false;
}
private class ParallaxCamera extends OrthographicCamera {
Matrix4 parallaxView = new Matrix4();
Matrix4 parallaxCombined = new Matrix4();
Vector3 tmp = new Vector3();
Vector3 tmp2 = new Vector3();
public ParallaxCamera (float viewportWidth, float viewportHeight) {
super(viewportWidth, viewportHeight);
}
public Matrix4 calculateParallaxMatrix (float parallaxX, float parallaxY) {
update();
tmp.set(position);
tmp.x *= parallaxX;
tmp.y *= parallaxY;
parallaxView.setToLookAt(tmp, tmp2.set(tmp).add(direction), up);
parallaxCombined.set(projection);
Matrix4.mul(parallaxCombined.val, parallaxView.val);
return parallaxCombined;
}
}
}
导入com.badlogic.gdx.ApplicationAdapter;
导入com.badlogic.gdx.gdx;
导入com.badlogic.gdx.InputProcessor;
导入com.badlogic.gdx.graphics.GL20;
导入com.badlogic.gdx.graphics.Orthographic Camera;
导入com.badlogic.gdx.graphics.Texture;
导入com.badlogic.gdx.graphics.g2d.SpriteBatch;
导入com.badlogic.gdx.math.Matrix4;
导入com.badlogic.gdx.math.Vector3;
公共类测试扩展ApplicationAdapter实现InputProcessor{
专用SpriteBatch批次;
私人视差照相机;
私人关系密切;
私有结构;
私人住宅区;
最终矢量3 curr=新矢量3();
最终向量3 last=新向量3(-1,-1,-1);
最终矢量3增量=新矢量3();
@凌驾
公共void创建(){
bgClose=新纹理(Gdx.files.internal(“starbg-close.png”);
bgMid=新纹理(Gdx.files.internal(“starbg-mid.png”);
bgFar=新纹理(Gdx.files.internal(“starbg far.png”);
摄像机=新视差摄像机(19201080);
批次=新的SpriteBatch();
Gdx.input.setInputProcessor(此);
}
@凌驾
公共无效渲染(){
//清屏
Gdx.gl.glClearColor(0f,0f,0f,1f);
Gdx.gl.glClear(GL20.gl\u颜色\u缓冲\u位);
//背景层,无视差,以原点为中心
batch.setProjectionMatrix(camera.calculateParallaxMatrix(0,0));
batch.disableBlinding();
batch.begin();
batch.draw(bgFar,-(int)(bgFar.getWidth()/2),-(int)(bgFar.getHeight()/2));
batch.end();
batch.enablembling();
batch.setProjectionMatrix(camera.CalculateParlaxMatrix(0.25f,0.25f));
batch.begin();
对于(int i=0;i<9;i++){
batch.draw(bgMid,i*bgClose.getWidth()-512,-512);
}
batch.end();
batch.setProjectionMatrix(camera.calculateParallaxMatrix(.5f,.5f));
batch.begin();
对于(int i=0;i<9;i++){
batch.draw(bgClose,i*bgClose.getWidth()-512,-512);
}
batch.end();
}
//…省略了空方法//
@凌驾
公共布尔补色(整数屏幕X、整数屏幕Y、整数指针、整数按钮){
last.set(-1,-1,-1);
返回false;
}
@凌驾
公共布尔值(整数x,整数y,整数指针){
相机未投影(当前设置(x,y,0));
if(!(last.x=-1和last.y=-1和last.z=-1)){
取消投影(delta.set(last.x,last.y,0));
delta.sub(货币);
相机位置添加(增量x,增量y,0);
}
最后一组(x,y,0);
返回false;
}
专用视差照相机{
Matrix4视差视图=新Matrix4();
Matrix4视差组合=新Matrix4();
Vector3 tmp=新Vector3();
Vector3 tmp2=新Vector3();
公共视差摄像机(浮动视口宽度、浮动视口高度){
超级(视口宽度、视口高度);
}
公共矩阵x4 calculateParallaxMatrix(浮动视差xx,浮动视差x){
更新();
tmp.set(位置);
tmp.x*=视差xx;
tmp.y*=视差;
视差视图。设置视差(tmp,tmp2。设置视差(tmp)。添加(方向),向上);
视差合成集(投影);
矩阵4.mul(parallaxcomposed.val,parallaxView.val);
返回视差混合;
}
}
}