I'm trying to see if a touch position collides with a Body and I'm not sure about the best way to implement it.
As of now, I have an Obstacle.java that draws a polygon-shaped body as a rectangle. It works fine to draw in the world, I just need to check for collisions.
Furthermore, I want to add some Sprite/Texture/Image to this body, so maybe it's better to check for collision between the sprite rather than the body, I'm not sure. So any pointers in the right direction would be helpful.
Obstacle.java
public class Obstacle{
public int x,y;
public Vector3 position;
public World world;
private Body body;
private BodyDef bodyDef;
private PolygonShape shape;
private float density = 1f;
private final float width = 360f;
private final float height = 20f;
Random r;
public Obstacle(World world) {
r = new Random();
this.world = world;
//rectangle
bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set( new Vector2( x + width / 2, y + height / 2));
float randomValue = (float)(0 + (540 - 0) * r.nextDouble());
shape = new PolygonShape();
shape.setAsBox( width / 2, height / 2 );
body = world.createBody(bodyDef);
body.createFixture(shape, density);
body.setTransform(randomValue,1160,0);
float angularVelocity;
if(r.nextInt(2)==0){
angularVelocity=-3;
}else{
angularVelocity=3;
}
body.setAngularVelocity(angularVelocity);
body.setLinearVelocity(0,-250);
}
public float getX() {
return position.x;
}
public float getY() {
return position.y;
}
public float getWidth() {
return 360 / GdxGame.PPM;
}
public float getHeight() {
return 20 / GdxGame.PPM;
}
public void render(SpriteBatch batch,float delta) {
}
public Boolean collidesWithPlayer(float touchX, float touchY){
return false;
}
}
The last method collidesWithPlayer was an effort from me to implement the detection, and in my Screen.java I have:
for(int i = 0; i < obstacles.size(); i++) {
if(obstacles.get(i).collidesWithPlayer(pointerX, pointerY)) {
hasDied=true;
}else{
isPaused=true;
}
}
Related
I want to change the height of my texture to a random height (with a specified range), and I almost figured out how, but the problem I have is that the texture (tower) is now above the ground. I want to stretch the texture while it remains on the same position, but changes height length.
I have a Tower class and a Scrollable class. In the Tower class I generate a random height in the reset method, but the problem is that I don't know what exactly I have to add or write to put the texture on the correct position (so that it isn't above the ground).
Here is the Tower class:
public class Tower extends Scrollable {
private Random r;
// When Tower's constructor is invoked, invoke the super (Scrollable)
// constructor
public Tower(float x, float y, int width, int height, float scrollSpeed) {
super(x, y, width, height, scrollSpeed);
// Initialize a Random object for Random number generation
r = new Random();
}
#Override
public void reset(float newX) {
// Call the reset method in the superclass (Scrollable)
super.reset(newX); // newX
// Change the height to a random number
Random r = new Random();
int low = 0;
int high = 15;
int result = r.nextInt(high-low) + low;
height = result;
}
}
And here's the Scrollable class:
public class Scrollable {
protected Vector2 position;
protected Vector2 velocity;
protected int width;
protected int height;
protected boolean isScrolledLeft;
public Scrollable(float x, float y, int width, int height, float scrollSpeed) {
position = new Vector2(x, y);
velocity = new Vector2(scrollSpeed, 0);
this.width = width;
this.height = height;
isScrolledLeft = false;
}
public void update(float delta) {
position.add(velocity.cpy().scl(delta));
// If the Scrollable object is no longer visible:
if (position.x + width < 0) {
isScrolledLeft = true;
}
}
// Reset: Should Override in subclass for more specific behavior.
public void reset(float newX) {
position.x = newX;
isScrolledLeft = false;
}
public boolean isScrolledLeft() {
return isScrolledLeft;
}
public float getTailX() {
return position.x + width;
}
public float getX() {
return position.x;
}
public float getY() {
return position.y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
Maybe it's important to know that I have a GameRenderer class which has a drawTowers() method, which is then used in a render() method.
That's my drawTowers() method:
private void drawTowers() {
batcher.draw(AssetLoader.texture1, tower1.getX(), tower1.getY() + tower1.getHeight(),
tower1.getWidth(), midPointY - (tower1.getHeight()));
batcher.draw(AssetLoader.texture2, tower2.getX(), tower2.getY() + tower2.getHeight(),
tower2.getWidth(), midPointY - (tower2.getHeight()));
batcher.draw(AssetLoader.texture3, tower3.getX(), tower3.getY() + tower3.getHeight(),
tower3.getWidth(), midPointY - (tower3.getHeight()));
batcher.draw(AssetLoader.texture4, tower4.getX(), tower4.getY() + tower4.getHeight(),
tower4.getWidth(), midPointY - (tower4.getHeight()));
}
You are drawing the tower too high, you need to be adding half the height rather than the whole height.
Here in drawTowers():
batcher.draw(AssetLoader.texture1, tower1.getX(), tower1.getY() + tower1.getHeight() / 2, tower1.getWidth(), midPointY - (tower1.getHeight()));
Do the same for the other towers.
This may not be perfectly correct but it shouldn't be far off.
I have been working with box2d and am having a problem where my body is not being set to the correct y coordinate when I try to position it at the bottom right of the screen.
I am trying to set the y coordinate to 0 so that the body aligns directly with the bottom of the screen. However, the half of the body is hanging off the screen. Even when I print out the body's location, it says the y is at 0.
I have my pixel per meter set at screenHeight / 80 which comes out to be around 25 PPM for the current devise I am using. I am not sure if my scaling is causing something weird to happen.
Code:
public class GameScreen extends ScreenAdapter {
Controller game;
public World world;
public Body ball;
public Body block;
public Box2DDebugRenderer box2DDebugRenderer;
private Matrix4 cameraBox2D;
public MyGestureListener myGestureListener;
//x-axis length for top/bottom bar
private float goalWidth;
//pixels per meter
public float PPM;
//y-axis height for back bar
private float goalHeight;
private float goalPostThickness;
//Screen height and width
private float screenWidth;
private float screenHeight;
//How far down/up posts are from edge of screen
private float goalPostTopOffset;
private float goalPostBottomOffset;
//Center x,y of cannon
private float cannonOriginX; //Variables used for starting poisition of balls
private float cannonOriginY;
//Center x,y of ball
float ballX;
float ballY;
//Velocity of ball
public float velocity = 50;
public int colorCode = 1;
//Tracks users drag y coord
public float panLocation = 0;
//Velocity of block once the user releases
public float flingVelocity;
//Changes to true when the block is released
public boolean isReleased = false;
public GameScreen (Controller game){
this.game = game;
}
#Override
public void show(){
myGestureListener = new MyGestureListener(game, this);
cameraBox2D = new Matrix4(game.cam.combined);
screenWidth = game.getScreenWidth();
screenHeight = game.getScreenHeight();
//PPM = screenHeight / 80f;
PPM = screenHeight / 80f;
goalPostTopOffset = screenHeight/7;
goalPostBottomOffset = goalPostTopOffset * 3;
goalHeight = screenHeight - (goalPostTopOffset + goalPostBottomOffset);
goalWidth = screenWidth / 6;
goalPostThickness = screenWidth / 75;
cannonOriginX = goalWidth / 2; //Variables used for starting position of balls
cannonOriginY = (goalPostThickness*5) / 2;
ballX = 0 + cannonOriginX;
ballY = (goalPostBottomOffset - (goalPostBottomOffset / 4)) + cannonOriginY;
world = new World(new Vector2(0, 0f), true);
box2DDebugRenderer = new Box2DDebugRenderer();
//Creates animated ball
ball = createBall();
//Sets radius, density, etc.
setBallProperties(ball);
//Creates block that stops balls
block = createBlock();
//Sets properties
setBlockProperties(block);
}
#Override
public void render(float delta){
//Logic
world.step(1/60f, 6, 2);
//Draw
game.cam.update();
Gdx.gl.glClearColor(32/255f, 32/255f, 32/255f, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
box2DDebugRenderer.render(world, game.cam.combined.scl(PPM));
game.shape.setProjectionMatrix(game.cam.combined);
Gdx.input.setInputProcessor(new GestureDetector(myGestureListener));
System.out.println(block.getPosition().y);
System.out.println(block.getPosition().x);
drawNonAnimated();
drawAnimated();
//show()
cameraBox2D = game.cam.combined.cpy(); //Copy the main camera
cameraBox2D.scl(PPM); //Scale the camera projection to the ratio you're using for Box2D
//render()
box2DDebugRenderer.render(world, cameraBox2D);
}
#Override
public void hide(){
}
//draws stationary objects such as goal posts
public void drawNonAnimated(){
//Cannon platform
game.shape.setColor(1, 1, 1, 1);
game.shape.begin(ShapeRenderer.ShapeType.Filled);
game.shape.rect(0, (goalPostBottomOffset - (goalPostBottomOffset / 4))/PPM, goalWidth/PPM, (goalPostThickness*2)/PPM);
game.shape.end();
//Cannon
game.shape.setColor(1, 1, 1, 1);
game.shape.begin(ShapeRenderer.ShapeType.Filled);
game.shape.rect(0, (goalPostBottomOffset - (goalPostBottomOffset / 4))/PPM, cannonOriginX/PPM,
cannonOriginY/PPM, goalWidth/PPM, (goalPostThickness*5)/PPM, 1, 1, 45);
game.shape.end();
}
public void drawAnimated(){
//FOR BALL
velocity--;
ball.setLinearVelocity(new Vector2(35,velocity));
Vector2 pos = ball.getPosition();
if (pos.x >= screenWidth/PPM){
ball.setTransform(new Vector2(ballX/PPM, ballY/PPM), 0);
//Changing color of ball each loop
if (colorCode == 3){
colorCode = 1;
}
else{
colorCode++;
}
//max 75 min 50
velocity = randInt(50, 75);
}
attachCircleShape(pos);
blockerMovement();
}
public void blockerMovement(){
//FOR BLOCKER RECTANGLE ANIMATION
Vector2 pos = block.getPosition();
float boundaryLocation = (goalPostBottomOffset - (goalPostBottomOffset / 4))/PPM;
//attachBlockerShape(pos);
panLocation = myGestureListener.getPanLocation();
if (isReleased == true){
//Get the velocity as the block is released
//to determine the speed it will continue to go after release
block.setLinearVelocity(0, flingVelocity);
flingVelocity -= 3;
}
else if (myGestureListener.getCheckRelease()|| panLocation/PPM >= boundaryLocation && !isReleased) {
flingVelocity = myGestureListener.speed;
isReleased = true;
System.out.println("SPEED: " + flingVelocity);
}
else if (myGestureListener.isPaning) {
block.setTransform(new Vector2((screenWidth - goalPostThickness) / PPM, panLocation / PPM), 0);
}
}
//Creates animated ball
public Body createBall(){
Body bBody;
BodyDef def = new BodyDef();
def.type = BodyDef.BodyType.DynamicBody;
def.position.set(ballX/PPM,ballY/PPM);
def.fixedRotation = true;
bBody = world.createBody(def);
return bBody;
}
//Sets radius, density, etc.
public void setBallProperties(Body body){
float gbt = screenWidth / 75; //Goal post thickness
float ballRadius = (gbt * 1.5f) / PPM;
// Create a circle shape and set its radius to 6
CircleShape circle = new CircleShape();
circle.setRadius(ballRadius);
// Create a fixture definition to apply our shape to
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = circle;
fixtureDef.density = 3.5f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 0.6f; // Make it bounce a little bit
// Create our fixture and attach it to the body
Fixture fixture = ball.createFixture(fixtureDef);
circle.dispose();
}
public Body createBlock(){
Body bBody;
BodyDef def = new BodyDef();
def.type = BodyDef.BodyType.DynamicBody;
def.position.set((screenWidth - goalPostThickness)/PPM,0);
def.fixedRotation = true;
bBody = world.createBody(def);
return bBody;
}
//Sets radius, density, etc.
public void setBlockProperties(Body body){
float gbt = screenWidth / 75; //Goal post thickness
// Create a circle shape and set its radius to 6
PolygonShape square = new PolygonShape();
square.setAsBox(goalPostThickness/PPM, (goalWidth/2)/PPM);
// Create a fixture definition to apply our shape to
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = square;
fixtureDef.density = 3.5f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 0.6f; // Make it bounce a little bit
// Create our fixture and attach it to the body
Fixture fixture = block.createFixture(fixtureDef);
square.dispose();
}
public static int randInt(int min, int max) {
Random rand = new Random();
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
public void attachBlockerShape(Vector2 pos){
game.shape.setColor(1, 1, 1, 1);
game.shape.begin(ShapeRenderer.ShapeType.Filled);
game.shape.rect(pos.x, pos.y, goalPostThickness/PPM, (goalWidth/2)/PPM);
game.shape.end();
}
//Attaching a regular libgdx shape to the position of box2d shape
public void attachCircleShape(Vector2 pos){
//Color of ball changes each loop
if (colorCode == 1){
//Blue
game.circle.setColor(66/255f, 134/255f, 244/255f, 0);
}
else if (colorCode == 2){
//Pink
game.circle.setColor(244/255f, 66/255f, 116/255f, 0);
}
else if (colorCode == 3){
game.circle.setColor(244/255f, 116/255f, 66/255f, 0);
}
game.circle.begin(ShapeRenderer.ShapeType.Filled);
game.circle.circle(pos.x*PPM, pos.y*PPM, goalPostThickness * 1.5f);
game.circle.end();
game.cam.update();
}
#Override public void dispose(){
box2DDebugRenderer.dispose();
world.dispose();
}
}
If you create a Body with Box2d 0,0 is in the center of the Body while libgdx set 0,0 on the bottom left corner.
So if you create a Body with Box2d and you want that 0,0 is in the bottom left corner you must move the Body by half of width on x and half of height on y:
public Body createBody(float x, float y, float width, float height){
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
//Move the body so if x and y are 0 the bottom left corner of the body is on position 0,0
bodyDef.position.x = x + width / 2;
bodyDef.position.y = y + height / 2;
PolygonShape polygonShape = new PolygonShape();
polygonShape.setAsBox(width / 2, height / 2);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = polygonShape;
Body body = world.createBody(bodyDef);
body.createFixture(fixtureDef);
polygonShape.dispose();
return body;
}
How to deal with different sizes of frames? If I setBounds(0, 0, 100, 100) in constructor then some frames are smaller but if I update it then frames are moved to left and up. How to make that every frame will be in the same place?
mayby you can see it
http://imgur.com/a/AN8Gc
public class Player extends Sprite{
//floats
private float animationTimer;
//box2d variables
public World world;
public Body body;
//enums
public enum State{STANDING, MOVING, SHOOTING, RELOAD, MALEE_ATTACK}
public State currentState;
public State previousState;
//booleans
boolean shoot;
boolean reload;
boolean maleeAttack;
private TextureRegion region;
public PlayScreen playScreen;
public Player(PlayScreen playScreen){
this.playScreen = playScreen;
this.world = playScreen.getWorld();
definePlayer();
animationTimer = 0;
region = Assets.instance.playerAssets.idleAniamtion.getKeyFrame(animationTimer);
setRegion(region);
setBounds(0, 0, getRegionWidth() / Constants.PPM, getRegionHeight() / Constants.PPM);
setPosition(0, 0);
currentState = State.STANDING;
previousState = State.STANDING;
shoot = false;
reload = false;
maleeAttack = false;
}
public void definePlayer(){
BodyDef bodyDef = new BodyDef();
bodyDef.position.set(100 / Constants.PPM, 100 / Constants.PPM);
bodyDef.type = BodyDef.BodyType.DynamicBody;
body = world.createBody(bodyDef);
FixtureDef fixtureDef = new FixtureDef();
CircleShape shape = new CircleShape();
shape.setRadius(50 / Constants.PPM);
fixtureDef.shape = shape;
body.createFixture(fixtureDef).setUserData(this);
body.setLinearDamping(Constants.LINEAR_DAMPING);
}
public void update(float delta){
region = Assets.instance.playerAssets.idleAniamtion.getKeyFrame(animationTimer);
setRegion(getFrame(delta));
moving();
//rotate();
}
public void moving(){
setPosition(body.getPosition().x - getWidth() / 2, body.getPosition(). y - getHeight() / 2);
//setBounds(0, 0, getRegionWidth() / Constants.PPM, getRegionHeight() / Constants.PPM); update bounds
if (input.isKeyPressed(Input.Keys.W) && body.getLinearVelocity().y < 5){
body.applyLinearImpulse(new Vector2(0, 1), body.getWorldCenter(), true);
}
if (input.isKeyPressed(Input.Keys.S) && body.getLinearVelocity().y > -5){
body.applyLinearImpulse(new Vector2(0, -1), body.getWorldCenter(), true);
}
if (input.isKeyPressed(Input.Keys.D) && body.getLinearVelocity().x < 5){
body.applyLinearImpulse(new Vector2(1, 0), body.getWorldCenter(), true);
}
if (input.isKeyPressed(Input.Keys.A) && body.getLinearVelocity().x > -5){
body.applyLinearImpulse(new Vector2(-1, 0), body.getWorldCenter(), true);
}
if (Gdx.input.isKeyPressed(Input.Keys.R)){
reload = true;
}
if (Gdx.input.isKeyPressed(Input.Keys.F)){
maleeAttack = true;
}
if (Gdx.input.isButtonPressed(Input.Buttons.LEFT)){
shoot = true;
}
}
public TextureRegion getFrame(float delta){
TextureRegion region;
currentState = getState();
switch (currentState){
case MOVING:
region = Assets.instance.playerAssets.moveAnimation.getKeyFrame(animationTimer);
break;
case SHOOTING:
maleeAttack = false;
region = Assets.instance.playerAssets.shootAniamtion.getKeyFrame(animationTimer);
if (Assets.instance.playerAssets.shootAniamtion.isAnimationFinished(animationTimer)){
shoot = false;
}
break;
case RELOAD:
region = Assets.instance.playerAssets.reloadAnimation.getKeyFrame(animationTimer);
if (Assets.instance.playerAssets.reloadAnimation.isAnimationFinished(animationTimer)){
reload = false;
}
break;
case MALEE_ATTACK:
region = Assets.instance.playerAssets.maleeAttackAnimation.getKeyFrame(animationTimer);
if (Assets.instance.playerAssets.maleeAttackAnimation.isAnimationFinished(animationTimer)) {
maleeAttack = false;
}
break;
default:
region = Assets.instance.playerAssets.idleAniamtion.getKeyFrame(animationTimer);
break;
}
animationTimer = currentState == previousState ? animationTimer + delta : 0;
previousState = currentState;
return region;
}
public State getState(){
if ((body.getLinearVelocity().x > 1 || body.getLinearVelocity().x < -1 || body.getLinearVelocity().y > 1 || body.getLinearVelocity().y < - 1) && !reload && !shoot && !maleeAttack){
return State.MOVING;
}
if (shoot && !reload){
return State.SHOOTING;
}
if (reload && !maleeAttack){
return State.RELOAD;
}
if (maleeAttack){
return State.MALEE_ATTACK;
}
else {
return State.STANDING;
}
}
public Vector2 getMousePosition(){
Vector2 mousePosition;
mousePosition = playScreen.getViewport().unproject(new Vector2(Gdx.input.getX(), Gdx.input.getY()));
return mousePosition;
}
public float getMouseAngle(){
float angle = (float) Math.atan2(getMousePosition().y - body.getPosition().y, getMousePosition().x - body.getPosition().x);
return angle;
}
public void rotate(){
setOrigin(getWidth() / 2, getHeight() / 2 );
setRotation((float) (getMouseAngle() * (180/Math.PI)));
body.setTransform(body.getPosition(), getMouseAngle());
}
}
To avoid the resizing of the texture just create a variable size used to set the bounds. The bound needs to set every frame.
public class Player extends Sprite {
private Body body;
private Vector2 size;
public Player(){
this.size = new Vector2( getRegionWidth() / Constants.PPM, getRegionHeight() / Constants.PPM );
}
public void update( float delta ){
Vector2 position = body.getPosition();
setRegion( getFrame( delta ) );
setRotation( MathUtils.radDeg * body.getAngle() );
setBounds( position.x, position.y, size.x, size.y );
setOriginCenter();
}
public void rotate(){
this.body.setTransform( body.getPosition(), getMouseAngle() );
}
}
You need to use the additional data information a TextureAtlas provides as an AtlasRegion.
One complete animation should have the same (width/height) size for each keyframe. Pick a size where the biggest keyfarme fits into. Also be aware of positioning correctly. Have the same pivot point for each keyframe.
To avoid wasting space when packing your TextureAtlas enable "trim" function. Should be supported by any TexturePacker Tool out there.
The texture data file (for libGDX) then has entries like this:
walk_animation
rotate: false
xy: 794, 235
size: 86, 109
orig: 160, 170
offset: 37, 22
index: 5
Use the AtlasRegion to draw things at the correct size and position:
float width = 400; // pick your size here
float height = 300;
float offsetPctX = atlasRegion.offsetX / atlasRegion.originalWidth;
float offsetPctY = atlasRegion.offsetY / atlasRegion.originalHeight;
float scaleWidth = (float) atlasRegion.packedWidth / atlasRegion.originalWidth;
float scaleHeight = (float) atlasRegion.packedHeight / atlasRegion.originalHeight;
float drawWidth = width * scaleWidth;
float drawHeight = height * scaleHeight;
float regionOffsetX = offsetPctX * width;
float regionOffsetY = offsetPctY * height;
float drawScaleX = 1; // adjust to your needs
float drawScaleY = 1;
float drawOriginX = 0; // adjust to tour needs
float drawOriginY = 0;
float drawRotation = false;
float x = 100 + offsetX + regionOffsetX; // adjust to your needs
float y = 100 + offsetY + regionOffsetY;
spriteBatch.draw(atlasRegion, x, y, drawOriginX, drawOriginY, drawWidth, drawHeight, drawScaleX, drawScaleY, drawRotation);
I'm using libgdx in java android studio. i have just started. i'm working on android phone. i not using any cameras. all i want is a sprite bounce of all four sides of the screen without tapping. i tried many codes i thought would work but nope. i hope u guys can help me. I'm expecting an answer soon as possible. Thanks
this is what i have:
SpriteBatch batch;
Texture background;
Sprite backgroundsprite;
Sprite ballsprite;
Texture line;
Texture ballimg;
BitmapFont credits;
BitmapFont input;
BitmapFont play;
float dt;
String string = "";
float ballx;
float bally;
float speedx;
float speedy;
Rectangle screenrect;
Rectangle ballrect;
float screenLeft ;
float screenBottom ;
float screenTop ;
float screenRight ;
#Override
public void create() {
batch = new SpriteBatch();
speedx = 5f * dt;
speedy = 5f * dt;
createsprite();
createbackground();
createtext();
ballx = ballsprite.getX();
bally = ballsprite.getY();
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
dt = Gdx.graphics.getDeltaTime();
ballsprite.setPosition(ballx + speedx,ballsprite.getY());
ballsprite.translateX(speedx);
float left = ballrect.getX();
float bottom = ballrect.getY();
float top = bottom + ballrect.getHeight();
float right = left + ballrect.getWidth();
if(left < screenLeft) {
string = "left";
speedx = 5f*dt;
}
if(right > screenRight)
{
string = "right";
speedx = -5f*dt;
}
if(bottom < screenBottom)
{
string = "bottom";
}
if(top > screenTop)
{
string = "top";
}
batch.begin();
backgroundsprite.draw(batch);
ballsprite.draw(batch);
rendertext();
batch.end();
}
public void createbackground() {
background = new Texture("images/BackgroundGodwin.jpg");
backgroundsprite = new Sprite(background);
backgroundsprite.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
screenrect = new Rectangle(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
screenLeft = screenrect.getX();
screenBottom = screenrect.getY();
screenTop = screenBottom + screenrect.getHeight();
screenRight = screenLeft + screenrect.getWidth();
}
public void createsprite() {
ballimg = new Texture("images/SpriteGodwin.png");
ballsprite = new Sprite(ballimg);
ballsprite.setScale(0.65f);
ballsprite.setPosition(Gdx.graphics.getWidth()/3,Gdx.graphics.getHeight()/2);
ballrect = new Rectangle(ballsprite.getBoundingRectangle());
}
#Override
public void dispose() {
batch.dispose();
ballimg.dispose();
background.dispose();
credits.dispose();
play.dispose();
input.dispose();
line.dispose();
}
public void createtext(){
play = new BitmapFont(Gdx.files.internal("fonts/realfont.fnt"));
play.setColor(com.badlogic.gdx.graphics.Color.GOLD);
credits = new BitmapFont(Gdx.files.internal("fonts/realfont.fnt"));
credits.setColor(com.badlogic.gdx.graphics.Color.GOLD);
input = new BitmapFont(Gdx.files.internal("fonts/realfont.fnt"));
input.setColor(com.badlogic.gdx.graphics.Color.OLIVE);
}
public void rendertext() {
credits.draw(batch, "Maded", Gdx.graphics.getWidth() / 7 - 50, Gdx.graphics.getHeight() / 9);
play.draw(batch, "Touch the Screen to play!!", Gdx.graphics.getWidth() / 2 - 175, Gdx.graphics.getHeight() - 80);
input.draw(batch, string, Gdx.graphics.getWidth() / 2 - 160, Gdx.graphics.getHeight() - 120);
}
}
I made a very simple version of what you want:
public class BouncyGame extends ApplicationAdapter {
SpriteBatch batch;
Texture ball;
float speedX = 3f;
float speedY = 3f;
int x;
int y;
#Override
public void create () {
batch = new SpriteBatch();
ball = new Texture("ball.png");
x = Gdx.graphics.getWidth()/2;
y = Gdx.graphics.getHeight()/2;
}
#Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//When the ball's x position is on either side of the screen.
//The width of the sprite is taken into account.
if (x > Gdx.graphics.getWidth() - ball.getWidth()/2 || x < 0 + ball.getWidth()/2) {
//Here we flip the speed, so it bonces the other way.
speedX = -speedX;
}
//Same as above, but with on the y-axis.
if (y > Gdx.graphics.getHeight() - ball.getHeight()/2 || y < 0 + ball.getHeight()/2) {
speedY = -speedY;
}
//Move the ball according to the speed.
x += speedX;
y += speedY;
batch.begin();
//Draw the ball so the center is at x and y. Normally it would be drawn from the lower left corner.
batch.draw(ball, x - ball.getWidth()/2, y - ball.getHeight()/2);
batch.end();
}
}
It will result in the following:
http://gfycat.com/TatteredCarefreeHapuku
There are numerous ways to improve this code, you could for example use vectors, and I wouldn't recommend using it in your final product, but it might help you figure out how to do something like this for your own project.
I have class which represents every object in my simple game (player, enemy, beam etc - they all have many commons like speed, position, dmg). So i made class named Thing. Here is how it looks like:
public abstract class Thing {
private Image image;
private float x;
private float y;
private float speed;
private final int WIDTH;
private final int HEIGHT;
public Thing(String filename, float x, float y, float speed) {
try {
Image image = ImageIO.read(new File(filename));
} catch (Exception e) {}
this.x = x;
this.y = y;
this.speed = speed;
WIDTH = image.getWidth(null);
HEIGHT = image.getHeight(null);
}
//Zwraca ksztalt do sprawdzania czy contains...
public Rectangle2D getShade() {
return new Rectangle2D.Float(x, y, WIDTH, HEIGHT);
}
public Image getImage() {
return image;
}
public Point2D getPoint() {
return new Point2D.Float(x, y);
}
public float getX() {
return x;
}
public float getY() {
return y;
}
}
I have extended the class Player:
public class Player extends Thing {
public Player(String filename, float x, float y, float speed) {
super(filename, x, y, speed);
}
public void moveToPoint(Point2D targetPoint) {
int targetX = (int)targetPoint.getX();
int targetY = (int)targetPoint.getY();
if ( ((int)x+20 < targetX+3) && ((int)x+20 > targetX-3) ) {
return;
}
float distanceX = targetX - x;
float distanceY = targetY - y;
//Dodanie 20px wymiarow statku
distanceX -= 20;
distanceY -= 20;
//Ustalenie wartosci shiftow
float shiftX = speed;
float shiftY = speed;
if (abs(distanceX) > abs(distanceY)) {
shiftY = abs(distanceY) / abs(distanceX) * speed;
}
if (abs(distanceY) > abs(distanceX)) {
shiftX = abs(distanceX) / abs(distanceY) * speed;
}
//Zmiana kierunku shifta w zaleznosci od polozenia
if (distanceX < 0) {
shiftX = -shiftX;
}
if (distanceY < 0) {
shiftY = -shiftY;
}
//Jezeli statek mialby wyjsc poza granice to przerywamy
if ( (((int)x+shiftX < 0) || ((int)x+shiftX > 260)) || ((y+shiftY < 0) || (y+shiftY > 360)) ) {
return;
}
//Zmiana pozycji gracza
x += shiftX;
y += shiftY;
}
}
And here is the problem because my IDE underlines x, y and speed fields red and tells they cannot be accessed from Player class. I tried to change them into private and default but there appears an error after that. What am I doing wrong? When i create new object from class which extends Thing I want to copy all fields and init them as it is said in constructor. So how to repair it?
You need to use getX(), getY() etc., because x,y, speed are private variables for class Thing.
The fact that Player extends Thing doesn't mean Player can access private fields. Thing provided public get... set... to access its private variables.
Change the variables x, y, and speed to protected, or use the accessors getX(), getY(), getSpeed() (getSpeed() needs to be added in this case) to solve the access issues.
The error that appeared after you changed them to default was the fact that you're calling abs(...) instead of Math.abs(...). Change all instances of abs(...) to Math.abs(...) to get rid of the new errors.