Java tutorial
/* Copyright (c) Philip Bliss, 2013 This file is provided under the Apache License, Version 2.0. See the file LICENSE for more. */ package com.me.mygdxgame; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.physics.box2d.BodyDef; import com.badlogic.gdx.physics.box2d.PolygonShape; public class TargetActor extends MoveableActor { private float TARGET_DENSITY = 10f; public TargetActor(GdxTest gdxTest) { super(gdxTest, "texture/beer0.png", TARGET_SIZE.cpy()); Vector2 initialPos = new Vector2(.025f, .025f); BodyDef targetBodyDef = new BodyDef(); targetBodyDef.type = BodyDef.BodyType.KinematicBody; targetBodyDef.position.set(initialPos); body = world.createBody(targetBodyDef); PolygonShape rectangleShape = new PolygonShape(); Vector2 pixelHalfSize = size.cpy().div(2f); rectangleShape.setAsBox(pixelHalfSize.x, pixelHalfSize.y); body.createFixture(rectangleShape, TARGET_DENSITY); rectangleShape.dispose(); } public void setTouched(Vector2 newTouchPos) { body.setTransform(pixelToMeter(newTouchPos).sub(size.cpy().div(2f)), body.getAngle()); } public Rectangle getRectForTouch(Vector2 tentativeTouchPos) { Vector2 tentativePositionScreen = meterToPixel(pixelToMeter(tentativeTouchPos).sub(body.getLocalCenter())); return new Rectangle(tentativePositionScreen.x, tentativePositionScreen.y, sprite.getWidth(), sprite.getHeight()); } }