Angle facing other way libgdx - java

I figured out the convertion of x and y of my cursor to angle however the problem is my sprite is facing the oposite direction.
here is the code
#Override
public void create() {
bida = new Texture(Gdx.files.internal("data/alienblaster.png"));
tR = new TextureRegion(bida);
bucket = new Rectangle();
bucket.x = 800 / 2 - bida.getWidth() / 2; // center the bucket horizontally
bucket.y = 400/2;
/*bucket.x = Gdx.graphics.getWidth() - bida.getWidth();
bucket.y = Gdx.graphics.getHeight() - bida.getHeight();*/
bucket.width = bida.getWidth();
bucket.height = bida.getHeight();
bullets = new Array<Rectangle>();
spawnRaindrop();
camera = new OrthographicCamera();
camera.setToOrtho(true, 800, 480);
front = 270;
batch = new SpriteBatch();
Gdx.input.setInputProcessor(this);
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
{
//batch.draw(bida,bucket.x,bucket.y, 50,50);
batch.draw(tR, bucket.x, bucket.y, bucket.getWidth()/2 /*center x of image where it will rotate*/, bucket.getHeight()/2 /*center y of image where it will rotate*/, bucket.getWidth() , bida.getHeight(), 1, 1, front);
}
batch.end();
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
float dx = screenX - bucket.x; // cursor x and sprite x coordinates
float dy = screenY - bucket.y; // cursor y and sprite y coordinates
float secondSolution =MathUtils.radiansToDegrees*MathUtils.atan2(dx, dy);
double firstSolution = Math.toDegrees(Math.atan2(dx,dy));
front = secondSolution;
Gdx.app.log("","" + firstSolution+ " " + secondSolution);
return true;
}
and the jar file is here
what is the problem here?

In place of camera.setToOrtho(true, 800, 480); use
camera.setToOrtho(false, 800, 480);

Related

java android path animation

So I am nearly at the end.
I have been researching the whole day on how to do this.
Draw a path which grows(animation) from one point to another.
I have tried it with Matrix, but that just ended with turning my whole paths.
Here is a image of my project:
my project
My goal is to draw a animated path from one circle to the other.
Code:
public void init(#Nullable AttributeSet attr) {
circle = new Paint();
circle.setColor(Color.GREEN);
circle.setStyle(Paint.Style.FILL);
circle.setAntiAlias(true);
line = new Paint();
line.setColor(Color.GREEN);
line.setStyle(Paint.Style.STROKE);
line.setStrokeWidth(10);
line.setAntiAlias(true);
Collections.addAll(height, 100, 20, 50, 40, 70, 10, 50); // in percent
System.out.println(height.size() + " this is the size");
}
#Override
protected void onDraw(Canvas canvas) {
float y = getHeight() / 20 * 14;
float x = getWidth() / 8;
float radius = (canvas.getWidth() * canvas.getHeight()) / 40940;
for (int c = 1; c < 8; c++) {
System.out.println("at " + c);
canvas.drawCircle(x * c, y - ((getHeight() / 20) * (height.get(c - 1) / 10)), radius, circle);
points.add(new PointF(x * c, (y - ((getHeight() / 20) * (height.get(c - 1) / 10)))));
}
}
Please Help,
Thanks
you just need animate the path using ValueAnimator
create one path object
Path path = new Path();
and create animator
ValueAnimator animator = new ValueAnimator();
float startX = // starting circle x co-ordinate
float startY = // starting circle y co-ordinate
float endX = // end circle x co-ordinate
float endY = // end circle y co-ordinate
PropertyValuesHolder propertyX = PropertyValuesHolder.ofFloat("x",startX,endX);
PropertyValuesHolder propertyY = PropertyValuesHolder.ofFloat("y",startY,endY);
valueAnimator.setValues(propertyX,propertyY);
valueAnimator.setDuration(1000); // animation time
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
float x = (float) animation.getAnimatedValue("x");
float y = (float) animation.getAnimatedValue("y");
path.lineTo(x,y);
invalidate();
}
});
valueAnimator.start();
and in onDraw() draw the path
canvas.drawPath(path,paint);

LibGDX Actor in stage not receiving click events

Edit:
I think it's something to do with the way I setBounds()...
A similar project I have (that actually works), involves a moving box that changes color when clicked... A box - not a circle. I think this is a clue.
OG Post:
Subclassed Actor.
Put Actor in a Stage.
Added a ClickListener inside Actor.constructor.
Set Stage as inputProcessor.
But when I click on the Actor, nothing seems to happen.
There is no response - when I try to click the actor.
It's suppose to change color, increase speed and track input x/ys.
It's weird 'cause I built a very similar program that works. So why doesn't this?
The actor is represented by a circle drawn by the ShapeRenderer. I assume it's "click" boundary must be just a rectangle?
HelloGDXGame.java
#Override
public void create() {
SCREEN_WIDTH = Gdx.graphics.getWidth() / 2;
SCREEN_HEIGHT = Gdx.graphics.getHeight() / 2;
rowHeight = 80;
touchPos = new Vector3();
batch = new SpriteBatch();
font = new BitmapFont();
font.setColor(Color.PINK);
font.setScale(4.0f);
ball = new Ball(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 180, 90, this);
stage = new Stage(new StretchViewport(SCREEN_WIDTH, SCREEN_HEIGHT));
stage.addActor(ball);
Gdx.input.setInputProcessor(stage);
}
#Override
public void render()
{
Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// Translate inputs x/y
touchPos.x = Gdx.input.getX();
touchPos.y = (Gdx.input.getY() - Gdx.graphics.getHeight()) * -1;
stage.act();
stage.draw();
// UI, to check it all works (it doesn't)
batch.begin();
font.draw(batch, "Ball Class: Taps: " + ball.getTaps(), 64, SCREEN_HEIGHT - (rowHeight * 8));
font.draw(batch, "Main Class: Touch X: " + touchPos.x, 64, SCREEN_HEIGHT - (rowHeight * 7));
font.draw(batch, "Main Class: Touch Y: " + touchPos.y, 64, SCREEN_HEIGHT - (rowHeight * 6));
font.draw(batch, "Ball Class: Touch X: " + ball.getScreenX(), 64, SCREEN_HEIGHT - (rowHeight * 5));
font.draw(batch, "Ball Class: Touch Y: " + ball.getScreenY(), 64, SCREEN_HEIGHT - (rowHeight * 4));
batch.end();
}
Ball.java
public Ball(float xPos, float yPos, float xSpeed, float ySpeed, HelloGdxGame game) {
super();
this.game = game;
renderer = new ShapeRenderer();
taps = 0;
radius = 196;
super.setBounds(xPos - radius, yPos - radius, radius * 2, radius * 2);
// just to check it's working
screenXY = new Vector3(0, 0, 0);
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
this.setTouchable(Touchable.enabled);
this.addListener(new ClickListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
doStuff(x, y);
return false;
}
});
changeColour(); // sets random color
}
private void doStuff(float screenX, float screenY) {
screenXY.x = screenX;
screenXY.y = screenY;
changeColour();
taps++;
// Increase speed
if (xSpeed > 0) xSpeed++;
else xSpeed--;
if (ySpeed > 0) ySpeed++;
else ySpeed--;
}
#Override
public void act(float delta) {
super.act(delta);
// move
super.setX(super.getX() + (xSpeed * delta));
super.setY(super.getY() + (ySpeed * delta));
// Bounce horizontally
if (super.getX() < 0 || super.getX() + (radius / 2) > Gdx.graphics.getWidth()) {
super.setX(super.getX() - (xSpeed * delta));
xSpeed *= -1;
}
// Bounce vertically
if (super.getY() < 0 || super.getY() + (radius / 2) > Gdx.graphics.getHeight()) {
super.setY(super.getY() - (ySpeed * delta));
ySpeed *= -1;
}
}
#Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
renderer.begin(ShapeRenderer.ShapeType.Filled);
renderer.setColor(r, g, b, 1);
renderer.circle(super.getX(), super.getY(), radius);
renderer.end();
}
1) Take a changed Ball from here
2)
Change
SCREEN_WIDTH = Gdx.graphics.getWidth() / 2;
SCREEN_HEIGHT = Gdx.graphics.getHeight() / 2;
To
SCREEN_WIDTH = Gdx.graphics.getWidth();
SCREEN_HEIGHT = Gdx.graphics.getHeight();
This is illogical.

Pixel collision detection in libgdx

I am creating a maze game in libGDX. The background texture is very similar to this one: http://www.wpclipart.com/recreation/games/maze/maze_square_medium.png
My moving character is on the maze, and I would like to do collision detection between the character and black walls by checking the colour of my maze texture, without creating rectangles and figuring out all the coordinates of the maze walls, if that is possible. I have researched a lot, but since I am quite new to libGDX and even java, I haven't really found anything that made a lot of sense to me for my case. If anyone has an answer for me on how I could do this or can refer me to a page that explains this well, it would be very greatly appreciated!
EDIT:
This is my code right now:
private static final int COLS = 4;
private static final int ROWS = 4;
Sprite sprChar, sprMaze;
SpriteBatch batch;
Texture Sprite;
TextureRegion[] frames;
TextureRegion CurrentFrame;
float fTime = 0f, fSpeed = 4;
Animation animation;
Texture txtMaze;
Pixmap pixmap;
int nX, nY, nPix, nR, nG, nB, nA;
Color color;
#Override
public void create() {
batch = new SpriteBatch();
Sprite = new Texture(Gdx.files.internal("snowwhite.png"));
sprChar = new Sprite(Sprite);
sprChar.setPosition(300, 200);
TextureRegion[][] tmp = TextureRegion.split(Sprite, Sprite.getWidth() / COLS, Sprite.getHeight(
frames = new TextureRegion[COLS * ROWS];
int index = 0;
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
frames[index++] = tmp[i][j];
}
}
animation = new Animation(1f, frames);
sprChar.setPosition(10, 10);
pixmap = new Pixmap(Gdx.files.internal("Maze2.png"));
color = new Color();
}
#Override
public void render() {
if (fTime < 4) {
fTime += Gdx.graphics.getDeltaTime();
} else {
fTime = 0;
}
nX = Math.round(sprChar.getX());
nY = Math.round(sprChar.getY());
nPix = pixmap.getPixel(nX, nY);
Color.rgba8888ToColor(color, nPix);
nR = (int) (color.r * 255f);
nG = (int) (color.g * 255f);
nB = (int) (color.b * 255f);
nA = (int) (color.a * 255f);
if (nR == 0 && nG == 0 && nB == 0) {
System.out.println("is hit");
}
txtMaze = new Texture(pixmap);
sprMaze = new Sprite(txtMaze);
CurrentFrame = animation.getKeyFrame(0);
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
sprChar.setX(sprChar.getX() - fSpeed);
CurrentFrame = animation.getKeyFrame(4 + fTime);
}
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
sprChar.setX(sprChar.getX() + fSpeed);
CurrentFrame = animation.getKeyFrame(8 + fTime);
}
if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
sprChar.setY(sprChar.getY() + fSpeed);
CurrentFrame = animation.getKeyFrame(12 + fTime);
}
if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
sprChar.setY(sprChar.getY() - fSpeed);
CurrentFrame = animation.getKeyFrame(0 + fTime);
}
batch.begin();
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.draw(sprMaze, sprMaze.getX(), sprMaze.getY(), sprMaze.getWidth() / 2,
sprMaze.getHeight() / 2, sprMaze.getWidth(),
sprMaze.getHeight(), sprMaze.getScaleX(),
sprMaze.getScaleY(), sprMaze.getRotation());
batch.draw(CurrentFrame, sprChar.getX(), sprChar.getY(), 20, 20);
batch.end();
}
public static void updatePosition(Sprite spr, float fX, float fY) {
spr.setPosition(fX, fY);
}
I just want it to output "is hit" if it passes a black wall, but I don't believe I am getting correct responses. Also, if I try to resize the maze sprite, it seems to me that it still checks for pixels on the maze at the original size.
P.S. Please excuse my bad code, I am VERY new. So any help would be very greatly appreciated, as I am on a time crunch!! Thank you

How do I flip the Y-axis so the (0,0) ends up in the Bottom-left corner

My Y-axis is flip so it's in the top left corners, but this problem only occurs with the
bounds = new Rectangle()
I am using the OrthographicCamera like this
camera= new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
My question is, how do I flip the Y-axis so the (0,0) ends up in the Bottom-left corner
public class MyGdxGame extends ApplicationAdapter {
SpriteBatch batch;
Player player;
Player2 player2;
InputProcessor inputprocessor;
Texture img;
Vector2 pos;
Rectangle bounds;
GameTime gameTime;
ShapeRenderer sr;
Rectangle p1,p2,p3,p4;
Rectangle tSwitch;
Rectangle Bplayer;
Vector2 p;
Vector2 button1,button2,button3,button4;
Vector2 tButton;
BitmapFont font;
OrthographicCamera camera;
int xi = 0;
int yi = 0;
int Movetime = 0;
int playerSwitch = 0;
float totalTime = 3*60;
Rectangle touchPos;
#Override
public void create () {
camera= new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
player = new Player(new Vector2(Gdx.graphics.getHeight() / 2 , Gdx.graphics.getWidth() / 2),"badlogic.jpg");
player2 = new Player2(new Vector2(Gdx.graphics.getHeight() / 2 , Gdx.graphics.getWidth() / 2),"Fagot.jpg");
pos = new Vector2(player.getPosition().x,player.getPosition().y);
gameTime = new GameTime();
button1 = new Vector2(0,0);
button2 = new Vector2(200,0);
button3 = new Vector2(1620,0);
button4 = new Vector2(1820,0);
tButton = new Vector2(800,0);
p1 = new Rectangle(button1.x + 100,button1.y+1225,100,100);
p2 = new Rectangle(button2.x + 100,button2.y+1225,100,100);
p3 = new Rectangle(button3.x + 100,button3.y+1225,100,100);
p4 = new Rectangle(button4.x + 100,button4.y+1225,100,100);
Bplayer = new Rectangle(player2.getPosition().x,player2.getPosition().y,100,100);
tSwitch = new Rectangle(tButton.x+100, tButton.y+1225 ,100,100);
sr = new ShapeRenderer();
font = new BitmapFont();
player.getPosition().x = 100;
player.getPosition().y = 100;
player2.setPosition(new Vector2(500, 500));
bounds = new Rectangle(player.getPosition().x , player.getPosition().y , 255, 255);
touchPos = new Rectangle(Gdx.input.getX(), Gdx.input.getY(),200,200);
}
#Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update(false);
bounds.setPosition(player.getPosition().x,player.getPosition().y);
//bounds = new Rectangle(player.getPosition().x, player.getPosition().y + 800, 255, 255);
float deltaTime = Gdx.graphics.getRawDeltaTime();
totalTime -= deltaTime;
String time = String.valueOf(totalTime);
String meow = String.valueOf(playerSwitch);
String Mtime = String.valueOf(Movetime);
batch.begin();
font.draw(batch, meow, 500, 500);
batch.end();
if(Gdx.input.isTouched(1)){
xi = Gdx.input.getX();
yi = Gdx.input.getY();
}
if(Gdx.input.isTouched()) {
Vector3 pointerPos = new Vector3();
pointerPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
touchPos.x = pointerPos.x /*+ 64 / 2*/;
touchPos.y = pointerPos.y /* + 64 / 2*/;
}
if(touchPos.overlaps(tSwitch)&& Gdx.input.isTouched()){
playerSwitch++;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//}else if(touchPos.overlaps(tSwitch)&& Gdx.input.isTouched() ){
// playerSwitch=1;
}
if(touchPos.overlaps(bounds)&& Gdx.input.isTouched()){
batch.begin();
font.draw(batch,"K",900,1000);
batch.end();
}
if(playerSwitch == 1){
if(touchPos.overlaps(p1)&& Gdx.input.isTouched()){
player.getPosition().x += 5f;
Movetime++;
}
if(touchPos.overlaps(p2)&& Gdx.input.isTouched()){
player.getPosition().y += 5f;
Movetime++;
}
if(touchPos.overlaps(p3)&& Gdx.input.isTouched()){
player.getPosition().x -= 5f;
Movetime++;
}
if(touchPos.overlaps(p4)&& Gdx.input.isTouched()){
player.getPosition().y -= 5f;
Movetime++;
}
}
if(playerSwitch == 2){
if(touchPos.overlaps(p1)&& Gdx.input.isTouched()){
player2.getPosition().x += 5f;
}
if(touchPos.overlaps(p2)&& Gdx.input.isTouched()){
player2.getPosition().y += 5f;
}
if(touchPos.overlaps(p3)&& Gdx.input.isTouched()){
player2.getPosition().x -= 5f;
}
if(touchPos.overlaps(p4)&& Gdx.input.isTouched()){
player2.getPosition().y -= 5f;
}
}
//Draw method
font.setScale(5, 5);
batch.begin();
batch.draw(player.getTexture(), player.getPosition().x, player.getPosition().y);
batch.draw(player2.getTexture(),player2.getPosition().x, player2.getPosition().y);
font.draw(batch, time, 900, 1100);
font.draw(batch, meow, 500, 500);
font.draw(batch, Mtime, 200,200 );
batch.end();
sr.begin(ShapeType.Line);
sr.setColor(255,255,255,255);
sr.rect(player.getPosition().x , player.getPosition().y, 255, 255);
sr.rect(player2.getPosition().x, player2.getPosition().y, 200, 200);
sr.end();
}
}

libGDX ViewPort Android devices

How can i use the fitviewport or viewport? i need to fit the screen to many android devices as posible. I dont have idea how to use that, thank you for your help, if you can paste some code for me i aprecciate that...
public void show(){
batch = main.getBatch();
texture = new Texture(Gdx.files.internal("spacebg.png"));
Texture texturaBola = new Texture(Gdx.files.internal("bola.png"));
ball = new Ball(Gdx.graphics.getWidth() / 2 - texturaBola.getWidth() / 2, Gdx.graphics.getHeight() / 2 - texturaBola.getHeight() / 2);
Texture texturaPala= new Texture(Gdx.files.internal("pala.png"));
Lpaddle = new LeftPaddle(80, Gdx.graphics.getHeight()/2 -texturaPala.getHeight() /2);
Rpaddle = new RightPaddle(Gdx.graphics.getWidth() -100, Gdx.graphics.getHeight()/2 - texturaPala.getHeight() /2, ball);
font = new BitmapFont();
font.setColor(Color.WHITE);
font.setScale(1f);
puntuacion = 0;
}
public void render(float delta){
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
updatePuntuacion();
Lpaddle.update();
Rpaddle.update();
ball.update(Lpaddle, Rpaddle);
batch.begin();
batch.draw(texture, 0, 0,texture.getWidth(), texture.getHeight());
ball.draw(batch);
Lpaddle.draw(batch);
Rpaddle.draw(batch);
font.draw(batch, "Points: " + Integer.toString(puntuacion), Gdx.graphics.getWidth() / 4 ,Gdx.graphics.getHeight() - 5);
font.draw(batch, "High score: " + Integer.toString(puntuacionMaxima),Gdx.graphics.getWidth() - Gdx.graphics.getWidth() / 4 ,Gdx.graphics.getHeight() - 5);
batch.end();
}
private void updatePuntuacion(){
if(ball.getBordes().overlaps(Lpaddle.getBordes())) {
puntuacion = puntuacion + 1;
if(puntuacion > puntuacionMaxima)
puntuacionMaxima = puntuacion;
}
if(ball.getBordes().x <= 0)
sonidoex.play();
if(ball.getBordes().x <= 0)
puntuacion =0;
if(ball.getBordes().x <=0)
Gdx.input.vibrate(1000);
if(ball.getBordes().x <=0)
Screens.juego.setScreen(Screens.MAINSCREEN);
ball.comprobarPosicionBola();
}
public void hide(){
font.dispose();
texture.dispose();
}
#Override
public void dispose(){
preferencias.putInteger("puntuacionMaxima", puntuacionMaxima);
preferencias.flush();
}
public void resize(int width, int height){
float widthImage = texture.getWidth();
float heightImage = texture.getHeight();
float r = heightImage / widthImage;
if(heightImage > height) {
heightImage = height;
widthImage = heightImage / r;
}
if(widthImage > width) {
widthImage = width;
heightImage = widthImage * r;
}
escala = width / widthImage;
}
Some code
https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/ViewportTest1.java
https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/ViewportTest2.java
https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/ViewportTest3.java
And the documentation
https://github.com/libgdx/libgdx/wiki/Viewports
Run the tests and you will understand the way it works.

Categories

Resources