I'm currently learning to code 2d games in Libgdx. Right now I'm making a kind of worms clone. I am using Box2d and have just my main sprite and a flat ground object so far.
What im stuck on is making the crosshair for the main character which I would like to rotate around him at a distance of say 90px when the player presses UP or DOWN key.
Here is the code I have so far but I havent even been able to attempt the rotation code at all:
package com.supermegabrobro.apoca.Sprites;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.supermegabrobro.apoca.Screens.PlayScreen;
public class ArmyDude extends Sprite {
PlayScreen playScreen;
BodyDef bodyDef;
Body body;
FixtureDef fixtureDef;
boolean crossHairShowing = false;
Texture crossHairTexture;
float crossHairDistance = 30f;
float crossHairAngle = 45f;
Vector2 crossHairPosition;
public ArmyDude(PlayScreen playScreen){
super(new Texture("armydude.png"));
this.playScreen = playScreen;
bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(0,0); // TODO: Make this random
body = playScreen.world.createBody(bodyDef);
CircleShape circleShape = new CircleShape();
circleShape.setRadius(getWidth() / 2);
fixtureDef = new FixtureDef();
fixtureDef.shape = circleShape;
body.createFixture(fixtureDef);
crossHairTexture = new Texture("crosshair.png");
crossHairPosition = new Vector2(0,0);
}
public void update(float dt){
setPosition(body.getPosition().x - getWidth() / 2, body.getPosition().y - getHeight() / 2);
if(Gdx.input.isKeyPressed(Input.Keys.UP)){
crossHairShowing = true;
}
}
@Override
public void draw(Batch batch) {
super.draw(batch);
if (crossHairShowing){
}
}
}
Any help is massively appreciated thanks. (nb. I have read up on, and implemented in previous projects, normal rotation of sprites. But I beleive this is very different since I am trying to not rotate the actual sprite but instead draw a texture (of a crosshair) that rotates around this sprite.
I'm really stuck though my poor brain can't figure it out alone :(