Continuous motion using fling with libgdx for Android - android

I am pretty new to libgdx, and I'm trying to use gestures to move a ball across the screen when a flinging motion is made on the screen. I've made this gesture listener:
class MyGestureListener implements GestureListener {
public boolean fling(float velocityX, float velocityY, int button) {
if (Math.abs(velocityY) > 0) {
ballPositionY -= velocityY*Gdx.graphics.getDeltaTime();
}
return false;
}
}
I initialized a detector for the listener in create() and used the following for the ball image in render():
batch.draw(ballImage, ballPositionX, ballPositionY);
The ball moves on fling in proportion to the velocity of the fling. However, it jumps across the screen from its original position to the final position, but I want it to have continuous motion (i.e. see it move across the screen rather than just jump from one point to another). How would I do this? Is fling appropriate for this task, or would you use something else? I am guessing this might have something to do with the frame rate but am not sure.
Thanks in advance!

You're only updating the ball's position when there is an active fling. You need to apply changes (velocity) to the ball on every frame to get smooth continuous animation. In Libgdx, the render callback is called every frame and is where you should update the state of your objects.
In the "fling" callback you should store the new velocity, and then on each call to your render callback you should change the ball's position based on the velocity and the time that has passed since the last render (the Gdx.graphics.getDeltaTime()).

Related

Camera rotation not rotating correctly on mobile

I'm making a FPS and I want the player to rotate the camera, my code works for PC, but on mobile if I'm rotating the camera and I also touching the fire button (or anywhere else with my other finger) the camera rotates to right (here where my fire button is) and I don't know if I can do something about it, or I need to cancel the release for android and IOS and publish my game only for PC
Part of my code:
if (CanProcessInput())
{
// Check if this look input is coming from the mouse
bool isGamepad = Input.GetAxis(stickInputName) != 0f;
float i = isGamepad ? Input.GetAxis(stickInputName) : Input.GetAxisRaw(mouseInputName);
// handle inverting vertical input
if (InvertYAxis)
i *= -1f;
// apply sensitivity multiplier
i *= LookSensitivity;
if (isGamepad)
{
// since mouse input is already deltaTime-dependant, only scale input with frame time if it's coming from sticks
i *= Time.deltaTime;
}
else
{
// reduce mouse input amount to be equivalent to stick movement
i *= 0.01f;
#if UNITY_WEBGL
// Mouse tends to be even more sensitive in WebGL due to mouse acceleration, so reduce it even more
i *= WebglLookSensitivityMultiplier;
#endif
}
return i;
}
Segment the touch input so you ignore values that are generated in the area of the fire button. You can do this by checking the Touch. position value:
Description
The position of the touch in screen space pixel coordinates.
Position returns the current position of a touch contact as it's dragged. If you need the original position of the touch see Touch.rawPosition.
The documentation hints at what you might be interested in, too - the original touch position. Your question body is asking about horizontal motion, but your code is referencing y values. The position coordinates are given in (width, height), so I just wanted to be clear up front that I'll use x values to answer your question about horizontal motion.
So if you know that the bottom-left corner of the screen is (0,0) and you can use Screen.width to get the width in pixels, then you could calculate the right 20% of the screen as reserved for the fire button. Anything less than that would be acceptable inputs for rotation, but the 80%-of-width pixel would be the maximum allowable input:
private int maxAllowablePixel;
void Start()
{
maxAllowablePixel = 0.8f * Screen.width;
}
and then only process the touch as a rotation if the original touch was less than that value:
if(touch.rawPosition.x < maxAllowablePixel)
{
DoRotation(touch.position);
}
and again here you are allowing the user to put their finger down in the middle of the screen, drag it over the fire button, and still rotate that much, but any touches that originate in the fire button "exclusion zone" are ignored for the purposes of doing rotations.
Also when you do the rotation, do it where the finger is now (Touch.position) and not where it started (Touch.rawPosition).
The way I always solve an issue like this is to not use the touch positions directly, but to have a "TouchReceiver" Object in the Canvas with a script that implements IDragHandler. So the class might look like this:
[RequireComponent(typeof(Image))]
public class TouchReceiver : MonoBehaviour, IDragHandler
{
public void OnDrag(PointerEventData data)
{
Vector2 delta = data.delta;
// do something with delta, like rotating the camera.
}
}
Then set up your canvas like so:
Make sure to put the "TouchReceiver" behind the fire button, that way the OnDrag event will not occur when pressing the fire button, because the fire button blocks the "TouchReceiver" object itself. The "TouchReceiver" object needs to have an Image component, otherwise it won't be able to receive any touches. Of course the image should then be made totally transparent.
With this setup is pretty easy to move the fire button around without changing anything else. This might also come in handy when adapting to different screen resolutions.

How can you correctly continue the fling of a ScrollView when adding children?

Context: I have a ScrollView that may have children added to its content view during a fling. Since the fling is configured with the height of the ScrollView contents before those children are added, if children get added then the fling stops itself (and actually scrolls backwards a bit because of overscroll) even though it hasn't reached the bottom of the contents.
I can possibly restart the fling using #fling(int velocity), but I can't figure out a way to get the current fling velocity or stop the current fling. Any ideas?
To answer your questions from the prompt:
There are no APIs to get the current velocity of ScrollView.
You can stop the current fling by careful handling of touch events as described in a previous SO post.
Looking at the ScrollView source code, it uses an OverScroller to handle the fling events. There are two ways of approaching this:
Use reflection to access the mScroller private field within ScrollView. Once you have accessed it you can get the current velocity through its public API: public float getCurrVelocity(); Using reflection is never ideal since you have to worry about it breaking across android versions, but a simple isolated usage can be relatively efficient.
Override the fling() method in ScrollView and instantiate a OverScroller to keep track of the scrolling. If all goes well, your Overscoller will always be in the same state as the one within ScrollView.
Something like this:
#Override
public void fling(int velocityY)
{
// Pass through fling to parent
super.fling(velocityY);
// Keep track of velocity using our own Overscoller instance
mOurOverscroller = mScroller.fling(mScrollX, mScrollY, 0, velocityY, 0, 0, 0,
Math.max(0, bottom - height), 0, height/2);
}
I solved a similar problem with #Override fling method. If you override fling method on your ScrollViewNoFling class and not call super.fling on this method, you gonna have not-fling-handled scrollView.
#Override
public void fling (int velocity)
{
/*Scroll view is no longer going to handle scroll velocity.
* super.fling(velocity);
*/
}

Dragging a sprite in Android

I'm simply trying to drag a sprite around using my finger. I'm doing this by detecting the distance the finger that is touching the screen has moved by and then moving the sprite by the same amount.
This is an openGL ES 2.0 app, so my rendering and logic updating takes place on my GL Rendering thread, and obviously touch events are captured on the UI thread.
So, my setup is something like this:
UI Thread
case MotionEvent.ACTION_MOVE: {
movedByX = event.getX()-oldXPosition;
movedByY = event.getY()-oldYPosition;
oldXPosition = event.getX();
oldYPosition = event.getY();
break;
}
GL Rendering thread
Rendering
#Override
public void render() {
drawSprite(testSprite); //Draws using the sprites' internal x and y coordinates
}
logic update
#Override
public void updateLogic() {
testSprite.x+=movedByX; //Update the sprite's X position
testSprite.y+=movedByY; //Update the sprite's Y position
}
The issue
If one drags the sprite around the screen for a while, and then stops. The resting point of the finger relative to the sprite is not the same as it was when the finger initially went down. Take for a example a circlar sprite like so. The blue circle is the sprite and the red dot represents the finger/pointer.
So as you can see, it doesn't quite work as expected and I'm not sure why.
I had a similar question in previously in which I 'worked around' the problem by initially grabbing the X and Y in ACTION_MOVE (onTouchEvent/UI Thread) as I am above, but then in my updateLogic method, I make a copy of it and work out the 'moveByY' amount there before applying it to my sprite's position.
Doing this effectively solved the problem of the finger 'wandering' - but - it makes the movement of the sprite very choppy, therefore I can't use this solution.
I think this choppiness may be because the rendering thread sometimes runs twice without the UI thread running, therefore, even though the finger has moved, the logic is still using the version it has because onTouch hasn't been able to capture to actual most up to date finger position. But I'm not 100% sure.
If I simply update my sprite's positionin my UI thread, (again, in ACTION_MOVE) - again, I get very choppy movement but the pointer position does remain correct).
So, I need to keep the smooth movement that I get from the method outlined at the top of the question, but I need to know why the moveBy amount is causing the sprite to wander from the finger.
Other Notes
I need to move the sprite using a the difference between the finger's old and current positions, and not simply draw the sprite at the finger's current position because this will eventually become part of a 'scrollable' menu system.
All of my variables are declared as 'private float volatile' and my onTouchEvent and updateLogic methods are synchronised.
i dont know if its a typo
#Override
public void updateLogic() {
testSprite.x+=movedByX; //Update the sprite's X position
testSprite.x+=movedByY; //Update the sprite's Y position
}
but you set testsprite.x both times

ScaleGestureDetector triggers onScaleEvent with single finger touch

I have code that onTouchEvent passes the touch event to a member ScaleGestureDetector and if the scale GD triggers a onScaleEvent on my scale listener, I scale up the display item. If the onScale event is not triggered I treat this event as a potential drag and move the displayed item around. I have observed that on certain devices the ScaleGestureDetector triggers an onScale event for me, even if there is just a single finger touching the screen. Is this expected? What is the recommended mechanism to deal with scales and drags at the same time?
If I'm correct you're triggering mScaleGestureDetector.onTouchEvent(mEvent) with single pointer (mEvent.getPointersCount() == 1). It is supposed to be triggered with pointers greater than 1. Make sure you're triggering with pointersCount > 1 Eg.
if(pointersCount >1) {
TOUCH_GESTURE_MODE = GESTURE_MODE_SCALE;
mScaleGestureDetector.onTouchEvent(mEvent);
return true;
}

Problems with touch event

Speaking of events we know that the touch screen method onTouchEvent (MotionEvent events) allows you to capture touch events on the screen and event.getX () and
event.getY () give me the coordinates of the touch as float values ​​(ie values ​​with a comma).
Specifically, I realized that taking logcat using the fixed point the finger in a mobile phone screen and not moving it, the event is not only perceived but MotionEvent.ACTION_DOWN MotionEvent.ACTION_MOVE and returned coordinates are manifold. This i can understand because the surface of the finger touches more points and more by reading the device coordinates believe that the finger moves while holding it.
My problem is that I would read the color of one pixel of a color image when I hold your finger still. In particular I need the central area occupied by the finger.
First, assuming you have a Bitmap object, I have to round to integer coordinates of the touch as the getPixel (intX, int y) coordinates of the entire Bitmap wants to return the pixel color.
Then how do I get the central point of touch? Be that there is a function that can help me? Keep in mind that while I stopped and read the value of a pixel on the image then I start to move slowly and even though I always record every move the center pixel. I need this because every move, if you change the color of the image in pixels, I want to vibrate or not the device.
If I could stop thinking about themselves in a dynamic array to store the coordinates of different pixels and search for the center of gravity, but moving I do not know what to do. I think however, that the research center of gravity, if not done very efficiently, can be slow during the move and then give the wrong results. If the CG is not quite enough for me to another point belonging to the environment.
I hope you can help me maybe with a few lines of code sketched.
Thanks in advance.
To get the touch position of finger, just cast the getX() and getY() to int and perform your desired tasks.
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN ) {
getPixelAt((int)event.getX(), (int)event.getY());
}
}
you can put you on main activity class when you can want to touch event occurs. let see you can put following code or not ? and return is most important as well.
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mDistanceX = 0;
}
return super.onTouchEvent(event);
}

Categories

Resources