how to move character - trying to apply touchDown and touchUp - android

Using the example with the bucket I am trying instead of using the
if(Gdx.input.isTouched()) {
Vector3 touchPos = new Vector3();
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
to use touchUp and touchDown.
So, in order to use them ,I define :
Vector2 position = new Vector2();
Vector2 velocity = new Vector2();
and then :
public boolean touchDown(int x, int y, int pointer, int button) {
if (x < 800 / 2 && y > 480 / 2) {
//here?? for left movement
}
if (x > 800 / 2 && y > 480 / 2) {
//here?? for right movement
}
return true;
}
Generally , I understand that I have a position and a velocity.And I must update the position related to velocity ,but I can't figure how.

You must update the position of your object every frame using the deltatime and the velocity vector.
Something like this(in render):
position.set(position.x+velocity.x*delta, position.y+velocity.y*delta);
And:
public boolean touchDown(int x, int y, int pointer, int button) {
if (x < 800 / 2 && y > 480 / 2) {
//here?? for left movement
velocity.x = -10;
}
if (x > 800 / 2 && y > 480 / 2) {
//here?? for right movement
velocity.x = 10;
}
return true;
}
public boolean touchUp(int x, int y, int pointer, int button) {
velocity.x = 0;
}

Related

onSensorChanged check if the mobile is in an horizontal position (no landscape)

I need to check if the mobile goes to the horizontal position (no landscape, I mean like you place it on the table).
I know I have to use "SensorEventListener" interface and the "onSensorChanged" event, but I am not able to get an example to howto check the mobile position.
#Override
public void onSensorChanged(SensorEvent event) {
//
}
Try this:
#Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
float x = values[0];
float y = values[1];
float z = values[2];
float norm =(float) Math.sqrt(x * x + y * y + z * z);
// Normalize the accelerometer vector
x = (x / norm);
y = (y / norm);
z = (z / norm);
int inclination = (int) Math.round(Math.toDegrees(Math.acos(z)));
if (inclination < 25 || inclination > 155)
{
// device is horiontal
Toast.makeText(this,"device horiontal !",Toast.LENGTH_SHORT).show();
}
}

Animating a list of points to move away from a given touch location

The animation I am working on achieving is shown below in Figure 1.
1)I have a list containing points on a 2D plane(left) that I am working on animating.
2)A touch is made at location x, and an invisible circle is drawn (middle). I want all of the points contained in that circle to move away from the center (x).
3) A final result example (right)
I am planning on applying this in a way that I can supply any list of points, touch location, numFrames, and force applied on each point per frame. An array of length numFrames is returned, each array item being an already animated list.
Since I plan on implementing this on screen touch, it is possible for another touch to happen while the first touch animation is already in progress. How can I handle these collisions?
Figure 1:
here's my 2 cents, you need a list of points, these points should have a position, a velocity and a method to update the position. something like this
public class Point {
public float x, y;
public boolean isMoving;
private float dx,dy;
public Point(float x, float y){
this.x = x;
this.y = y;
}
public boolean move(float maxWidth, float maxHeight){
if(!isMoving) return false;
// update the position
x += dx;
y += dy;
dx /= FRICTION;
dy /= FRICTION;
// bounce...?
if(x < 0 || x > maxWidth){
dx *= -1;
}
if(y < 0 || y > maxHeight){
dy *= -1;
}
if(Math.abs(dx) < MOVEMENT_THRESHOLD && Math.abs(dy) < MOVEMENT_THRESHOLD){
isMoving = false;
}
return isMoving;
}
}
on every touch event you apply a force to every point within the radius and set their velocity
for(Point point : mPoints){
float distance = point.distance(x,y);
if(distance > mTouchRange) continue;
float force = (float) Math.pow(1 - (distance / mTouchRange), 2) * mForceFactor;
float angle = point.angle(x,y);
point.dx -= Math.cos(angle) * force;
point.dy -= Math.sin(angle) * force;
point.isMoving = true;
}
then you need an animation that call move on every frame and eventully stops when there are no moving points
you can find the full example here

OSMDroid Limit the map in North/South

I am working with the OSMDroid API and I'm wondering if there's a way to limit the map in north and south. I mean, is this possible to prevent the map from endlessly repeating itself on the Y-Axis.
I found this issue on the Github's "osmdroid" project but the code of the patch is too old and cannot be applied to the new version of osmdroid (4.2)
EDIT
I tried the setScrollableAreaLimit method but there is a bug (maybe) in the upper-left point. The map jump on the other side when I come close to its end.
Thank you by advance
I modified two lines in the class MapView.
I replaced x += worldSize; by x=0; and y += worldSize; by y=0;
public void scrollTo(int x, int y) {
final int worldSize = TileSystem.MapSize(this.getZoomLevel(false));
while (x < 0) {
//x += worldSize;
x=0;
}
while (x >= worldSize) {
x -= worldSize;
}
while (y < 0) {
// y += worldSize;
y=0;
}
while (y >= worldSize) {
y -= worldSize;
}
[...]
}
I was extend MapView and override to scrollTo()
public void scrollTo(int x, int y) {
final int worldSize = TileSystem.MapSize(this.getZoomLevel(false));
if(y < 0) { // when over north pole
y = 0; // scroll to north pole
}else if(y + getHeight() >= worldSize) { // when over south pole
y = worldSize-getHeight() - 1; // scroll to south pole
}
super.scrollTo(x,y);
}
I was able to overcome this by expanding on #kenji's answer. With the subclass approach, I found that rapidly zooming out would sometimes reset the map to the south pole. To prevent this, the y-value needs to be downscaled first (just as the super class implementation does in v5.6.5).
#Override
public void scrollTo(int x, int y) {
final int worldSize = TileSystem.MapSize(this.getZoomLevel(false));
final int mapViewHeight = getHeight();
// Downscale the y-value in case the user just zoomed out.
while (y >= worldSize) {
y -= worldSize;
}
if (y < 0) {
y = 0;
} else if ((y + mapViewHeight) > worldSize) {
y = worldSize - mapViewHeight;
}
super.scrollTo(x, y);
}
use setScrollableAreaLimit.
BoundingBoxE6 bbox_bariloche = new BoundingBoxE6(-41.033770, -71.591065, -41.207056,-71.111444);
map.setScrollableAreaLimit(bbox_bariloche);

LibGDX - InputProcessor - if user draggs finger from button which represents moving on X, to button which represents moving on Y

So, i am creating a joystic for a android game, so in my Touching class(implementing InputProcessor) it looks like a :
public class Touching implements InputProcessor{
int y = 0;
int x = 0;
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
x = Gdx.input.getX();
y = Gdx.input.getY();
if (x > 122 && x < 202 && y > 620 && y < 700 )
{
Core.player1.anmov(2); //button 1
}
if ( x > 122 && x < 202 && y > 520 && y < 600)
{
Core.player1.anmov(1); //button 2
}
if (x > 20 && x < 100 && y > 620 && y < 700)
{
Core.player1.anmov(3); //button 3
}
if (x > 222 && x < 302 && y > 620 && y < 700)
{
Core.player1.anmov(4); // button 4
}
return true;
}
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
x = Gdx.input.getX();
y = Gdx.input.getY();
if (x > 122 && x < 202 && y > 620 && y < 700)
{
Core.player1.anmov(7); // button 1
}
if ( x > 122 && x < 202 && y > 520 && y < 600)
{
Core.player1.anmov(8); // button 2
}
if (x > 20 && x < 100 && y > 620 && y < 700)
{
Core.player1.anmov(5); // button 3
}
if (x > 222 && x < 302 && y > 620 && y < 700)
{
Core.player1.anmov(6); // button 4
}
return false;
}
public boolean touchDragged(int screenX, int screenY, int pointer) {
x = Gdx.input.getX();
y = Gdx.input.getY();
return true;
}
so now, if i touch the button which represents moving on X, dragg to button which represents moving on Y, it is still moving on X, until i get my finger out of screen(the touchUP is calling), and then, it it standing, it does not move on Y..
Can anybody help me please? I think this is a very primitive, but i cant find a solution.
I would probably go for something like this in the player class update method itself
//Declare velocity on top so you just have to change this to fine tune speed.
float velocity = 10;
if (Gdx.input.isTouched()) //isTouched should register as long as a screen is touched, you can use pointers for multiple fingers.
{
if (touch within button) //just use Gdx.input.getX / Gdx.input.getY
{
position.X += velocity * Gdx.graphics.getDeltaTime();
{
//do for each button
}
Never used dragging for onscreen buttons myself but theoretically this should work since isTouched registers as long screen is touched. Gdx.input.getX/Y should update. You probably had something in there that only ran once and continued moving your player until a release was registered.

Android Animation Question

I am trying to write the logic for an animation sequence and can't seem to get the thing right. What I want to happen is: if the user clicks on the screen, the method takes in the touchEvent coordinates, and then changes the movement variables of a sprite so that the sprite travels to where the user touched the screen. I have my "launch" event setup like this.
public void launch(float eventX, float eventY) {
//get the touch event coords and then move the banana to them.
fire = true;
//the x and y variables for the sprite
getX();
getY();
//the target x and y variables
targetX = eventX;
targetY = eventY;
//the total distance the two variable have to "travel"
distanceX = x - targetX;
distanceY = y - targetY;
//variables to update the movement
moveX = distanceX;
moveY = distanceY;
}
Then, I thought I was supposed to put the movement variables in the update method like this:
public void update(long gameTime) {
if(gameTime > frameTicker + framePeriod) {
frameTicker = gameTime;
currentFrame++;
if(currentFrame >= frameNbr){
currentFrame = 0;
}
}
this.sourceRect.left = currentFrame * spriteWidth;
this.sourceRect.right = this.sourceRect.left + spriteWidth;
if(fire == true){
x = (int) moveX;
y = (int) moveY;
}
If the user clicks as it is, the animation shows up like it's supposed to, but then instantaneously goes to the top left corner of the screen or what I have come to understand is (0,0) on a coordinate system. I can't figure out how to slow it down so that it moves at a reasonable space and goes where it is supposed to.
You could put the whole animation in your launch() function if you want.
For instance, at the end of the function something like:
float incrementX = distanceX / 100;
float incrementY = distanceY / 100;
float spriteX = getX();
float spriteY = getY();
bool xDone = false;
bool yDone = false;
while(!(xDone && yDone)) {
if (distanceX <= spriteX) {
spriteX += incrementX; // update the sprite's x coordinate as well
}
if (distanceY <= spriteY) {
spriteY += incrementY; // update the sprite's y coordinate as well
}
try{ Thread.sleep(10) } catch(Exception e) {}
}
That code relies on the sprite starting at a lower x and y than the event; if that's not the case it needs to be modified.

Categories

Resources