Calculating Velocity - android

I am working on a very simple Physics game for Android where an object bounces up and down with some gravity (0 friction, ball should always reach same height). I want it to reach the same height on all devices (density independence). My game is coded with the following for the physics, which works perfectly fine on a 540 * 960 device.
Note: My game has a max FPS of 30
physics code
//Instance variables
private final float acceleration = .1f;
private static float timePoint;
private float velocityY = 0;
//game loop code
timePoint += 1;//updates every tick
velocityY = velocityY - acceleration * timePoint;
position.y = (int) (position.y - velocityY);
if(position.y + bitMapHeight <= 0){
velocityY = 30;
timePoint = 0;
}
When the ball hits the ground (y = 0) I simply apply a velocity of 30, which makes the ball reach a desired height on the 540 * 960 device. Of course multiple devices lead to major headaches, and this formula will not suite all of them. I think the best approach would be to somehow calculate the velocity based on screen height, but I'm not really sure what the best approach is for something like this. Below are some screenshots showing whats happening.
If anyone can point me in the right direction I would be very grateful! Please let me know if any more details are required.

I had to solve a similar problem in a 2D game I was working on a while ago.
I developed the initial physics and rendering functionality on my own Android device until it behaved nicely on that particular device. I then obtained 'baseline' values for the relevant variables (eg. pixels:meter as mentioned by bjb568), to achieve the correct velocities on my baseline device.
Then, to make the game behave in the same way on different devices/screens, I simply used the baseline values scaled by the ratio of the actual screen resolution and the baseline screen resolution.
My game used a SurfaceView, so the 'actual screen resolution' was basically determined by the width and height of the surface.
I hope that helps.

Why don't you try this on the onCreate:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
With that you can get the values of the screen and, instead of using ´velocityY = 30;´ you can set it to ´velocityY= height/10´ for example

Why you set velocityY = 30; after point reaches the bottom? I think that you should use perfectly elastic collision. So, you just need to set velocityY = -velocityY;

I apologize to those who posted on this for my delayed response. I found a solution that worked for me. Below is the approach I took.
Find the target height that the object should reach after it hits the ground (bottom of screen).
//We want the object to bounce halfway on screen
targetBounceHeight = metrics.heightPixels / 2;
Use physics calculation to find required velocity to reach point Y.
double velocityI = Math.sqrt(2 * targetBounceHeight * acceleration);
I simply find the initial velocity required, then plug it into the original code snippet dynamically instead of the value 30 (see top post). This solution has worked on all tested devices perfectly. Hope this helps someone!

Related

Android GestureOverlayView parameters

I started experimenting with Custom Gestures and the GestureOverlayView and noticed a few variables where I am not sure what they are for and what range of values can and should be assigned, the docs seem to be somewhat vague on those:
//Minimum curve angle a stroke must contain before it is recognized as a gesture.
android:gestureStrokeAngleThreshold
I assumed this is in degrees and when I add "25" here, a sharp edge must be contained in the gesture, but actually it still is detected ifI draw a circle or a perfect square.
//Minimum length of a stroke before it is recognized as a gesture.
android:gestureStrokeLengthThreshold
Is this in dp ? Because it seems like on smaller screens it is harder to trigger the gesture...
//Squareness threshold of a stroke before it is recognized as a gesture.
android:gestureStrokeSquarenessThreshold
what is this?
EDIT:
Ok I just realized that every prediction has a score value, which should be used to find out if the gesture performed actually meets the requirements, so I added a check if the prediction's score is greater than 1.
Still I am curious what those variables in GestureOverlayView are doing, so enlighten me :)
gestureStrokeLengthThreshold definitely is not density independent but apparently uses pixels. If you want to set a density independent threshold you can calculate the gestureStrokeLengthThreshold at runtime, like that:
DisplayMetrics metrics = getResources().getDisplayMetrics();
float normalizedScreenSize = (metrics.heightPixels + metrics.widthPixels) / 2.0f;
return normalizedScreenSize * GESTURE_LENGTH_THRESHOLD;
GESTURE_LENGTH_THRESHOLD would be a value representing how long the gesture should be. A value of 1.0 would roughly be the size of the screen (averaged from screen width and height)
Still I am interested in what those other variables in GestureOverlayView do, so if you know more - enlighten me :)

Speed and distance not same on every device

I have created an Android game using a canvas, but when testing, I have found that the speed and distance of the movements such as flying up or falling down are set right on a phone with a resolution of 1920x1080 (401 ppi). But when I tested on a smaller phone with a resolution of 480 x 854 (196 ppi), I found that the movement of my sprites are a lot quicker which is affecting the gameplay. E.g. The main character sprite jumps a lot higher than I want it to.
Is there any way of keeping the speed and distance the same across all device sizes and types?
Here is some code on how I have implemented the movement:
A sprite class.
//class variables
private int GRAVITY_LIMIT = -30;
public int gravity = 0;
//gravity
if(gravity>GRAVITY_LIMIT){
gravity= gravity-2;
}
//fall
y= y-gravity;
Drawing the sprite
canvas.drawBitmap(bmp1, x, y, null);
When onTouch is triggered (Jumping)
bird.gravity=30;
You should base your movement around world coordinates. For example, set your world to be 10meters x 10meters, so that when you jump, you jump 1m. You then need to map that world to screen pixels.
float worldHeight = 10f;
float worldToPixels = screenHeight/worldHeight;
y = bird.y * worldToPixels;
So, on a 500px height screen, you would jump 50px and on a 1000px height screen you would jump 100px.
Gravity and other forces need to be based on the world as well for it to work on all devices.
Lastly, if you're trying to make a game for multiple devices, it would be better to use a library like libGDX. There are lots of helpful classes like ViewPorts to make this easier.
I have found another solution.
This one works well.
y= y-(gravity * game.getResources().getDisplayMetrics().density);

Android - calculating pixel rotation without matrix? And checking if pixel is in view

I'm hoping someone can help me out. I'm making an image manipulation app, and I found I needed a better way to load in large images.
My plan, is to iterate through "hypothetical" pixels of an image (a "for loop" that covers width/height of the base image, so each iteration represents a pixel), scale/translate/rotate that pixels position relative to the view, then use this information to determine which pixels are being displayed in the view itself, then use a combination of BitmapRegionDecoder and BitmapFactory.Options to load in only the section of image that the output actually needs rather than a full (even if scaled) image.
So far I seem to have covered scale of the image and translation properly, but I can't seem to figure out how to calculate rotation. Since it's not a real Bitmap pixel I can't use Matrix.rotate =( Here is the image translations in the onDraw of the view, imgPosX and imgPosY hold the center point of the image:
m.setTranslate(-userImage.getWidth() / 2.0f, -userImage.getHeight() / 2.0f);
m.postScale(curScale, curScale);
m.postRotate(angle);
m.postTranslate(imgPosX, imgPosY);
mCanvas.drawBitmap(userImage.get(), m, paint);
and here is the math so far of how I'm trying to determine if an images pixel is on the screen:
for(int j = 0;j < imageHeight;j++) {
for(int i = 0;i < imageWidth;i++) {
//image starts completely center in view, assume image is original size for simplicity
//this is the original starting position for each pixel
int x = Math.round(((float) viewSizeWidth / 2.0f) - ((float) newImageWidth / 2.0f) + i);
int y = Math.round(((float) viewSizeHeight / 2.0f) - ((float) newImageHeight / 2.0f) + j);
//first we scale the pixel here, easy operation
x = Math.round(x * imageScale);
y = Math.round(y * imageScale);
//now we translate, we do this by determining how many pixels
//our images x/y coordinates have differed from it's original
//starting point, imgPosX and imgPosY in the view start in center
//of view
x = x + Math.round((imgPosX - ((float) viewSizeWidth / 2.0f)));
y = y + Math.round((imgPosY - ((float) viewSizeHeight / 2.0f)));
//TODO need rotation here
}
}
so, assuming my math up until rotation is correct (probably not but it appears to be working so far), how would I then calculate the rotation from that pixels position? I've tried other similar questions like:
Link 1
Link 2
Link 3
without using rotation the pixels I expect to actually be on the screen are represented (I made text file that outputs the results in 1's and 0's so I can have a visual representation of whats on the screen), but with the formula found in those questions the information isn't what is expected. (Scenario: I've rotated an image so only the top left corner is visible in the view. Using the info from Here to rotate the pixel, I should expect to see a triangular set of 1's in the upper left corner of the output file, but that's not the case)
So, how would I calculate a a pixels position after rotation without using the Android matrix? But still get the same results.
And if I've just messed it up entirely my apologies =( Any help would be appreciated, this project has gone on for so long and I want to finally be done lol
If you need any more information I will provide as much as I possibly can =) Thank you for your time
I realize this question is particularly difficult so I will be posting a bounty as soon as SO allows.
You do not need to create your own Matrix, use the existing one.
http://developer.android.com/reference/android/graphics/Matrix.html
You can map bitmap coordinates to screen coordinates by using
float[] coords = {x, y};
m.mapPoints(coords);
float sx = coords[0];
float sy = coords[1];
If you want to map screen to bitmap coordinates, you can create the inverse matrix
Matrix inverse = new Matrix(m);
inverse.inverse();
inverse.mapPoints(...)
I think your overall approach is going to be slow, as doing the pixel manipulation on the CU from Java has a lot of overhead. When drawing bitmaps normally, the pixel manipulation is done on the GPU.

Android canvas game, game speed

I have a simple function that increases the speed of my object in the screen:
float Velocity = 10;
float MaxVelocity = 100;
float VelocityGiven = 0;
RectF position = new RectF(ScreenHeight/2, ScreenWidth,0,0);
public void update()
{
if(VelocityGiven < MaxVelocity)
{
Position.left -= Velocity;
VelocityGiven += Velocity;
}
}
public void draw(Canvas canvas)
{
canvas.drawBitmap(Bitmap,Position,null);
}
But depending on the phone screen size, or pixel density, this function moves the object too fast or too slow.
if I try that on a galaxy S4, witch has a really big screen resolution, the object goes slowly.
But if I try it on a low end device (small screens), the object goes very fast,
What can I do to avoid that?
I already have my FPS controlled, on every phone, this runs at 30 FPS. so it's not about the update ratio.
It depends on what you wish to do exactly. One simple approach would be to make everything proportional to screen size. For instance:
Position.left -= Velocity * getWidth() / REFERENCE_WIDTH;
where REFENRENCE_WIDTH corresponds to a screen resolution where your app behaves as you like.
You need to work with density independent units. That way it moves the same relative amount no matter what the pixel size is. To do this, decide how "big" your area is going to be. For example, to fix your width at a logical 1000 units:
float scaleWidth = (screenWidth / 1000);
float velocity = 10 * scaleWidth;
float maxVelocity = 100 * scaleWidth;
This would mean that no matter how wide the display actually is, at max velocity it would take 10 movements to go all the way across.
You are probably best to move your objects based on a grid system independent of the screen size. You could you meters, feet, or what ever works as your unit. Then get the screen size and screen resolution of the device and figure out what the relationship is. If your object moves one foot then how many pixels is that on the device. Based on resolution.
Here is a very general overview on screen sizes for android:
http://developer.android.com/guide/practices/screens_support.html
There are probably a lot more, and better, places to look for getting the device resolution and size.
Hope that at least gets you started :)

android live wallpaper rescaling

I am learning how to make live wallpapers, but I have a dilemma I'm sure all who start off have as well.
There is so many resolution screen sizes, how can I just make one set of artwork to be rescaled in code for all versions? I know it's been done as I seen the images in the apk's on a lot of them and they get rescaled.
If it was just one image that did not need any positioning that would be easy, but my problem is I have to get the background image rescaled to fit all devices, I also have animations that fit in a certain x and y position on that background image to fit in place so it looks like the whole background is being animated but only parts of it is (my way of staying away from 300 images of frame by frame live wallpapers).
So the background image needs to be rescaled and the animations need to be rescaled as well to the exact percentage as the background image and they need to sit in a specific x and y position.
Any help would be appreciated so I can get this going.
I tired a few things, figured I would make a scaler for everything example: int scaler; then in onSurfaceChanged scaler = width /1024; //if the bigger image is 1024. that will give me a ratio to work with everywhere. then scale accordingly using scaleBitmap by multiplying the scaler by the image height and width, and also use the same scaler for positioning example image x lets say is at 50, scale it using the same thing x = scaler * 50; that should take care of scaling and positioning, just how to translate all this into java is the next lesson, since I'm new to java, I used to program for flash and php but this is a lot different, take some getting used to. Next thing is how to pan the width, when you move your screen from side to side how to make the image show is the next puzzle I have figure out. Right now it just shows the same width no matter what even though the width is double what the surface shows. If you got an answer or somewhere I can find out the info on this one that would be greatly appreciated.
Well, um, all I can say is "Welcome to the real world." You get your screen dimensions passed to you via onSurfaceChanged, and yes, it is your job to figure out how to scale everything based on this data. That's why they pay us the big bucks. :-)
You will want to make sure your resources are large enough to fit the biggest display you intend to support, so you will always be shrinking things (which distorts much less than expanding things).
Suggest starting with "best practices for screen independence" here: http://developer.android.com/guide/practices/screens_support.html
Additional comments in re your request for more help...
You cannot (necessarily) scale your artwork just using the width, because you need to support multiple aspect ratios. If the screen proportions do not match your artwork, you must decide if you want to distort your artwork, leave blank spaces, etc.
I'm not sure how to interpret your trouble passing around the screen dimensions. Most of us put all of our active code within a single engine class, so our methods can share data via private variables. For example, in the Cube wallpaper in the SDK, onSurfaceChanged() sets mCenterX for later use in drawCube(). I suggest beginning with a similar, simple approach.
Handling scrolling takes some "intelligence" and a careful assessment of the data you receive via onOffsetsChanged(). xStep indicates how many screens your launcher supports. Normally xStep will be 0.25, indicating 5 screens (i.e. xOffset = 0, 0.25, 0.5, 0.75, or 1) but it can be any value from 0 to 1; 0.5 would indicate 3 screens. xPixels gives you an indication of how much the launcher "wants" you to shift your imagery based on the screen you're on; normally you should respect this. On my phone, the launcher "desires" a virtual wallpaper with twice the pixels of the physical screen, so each scroll is supposed to shift things only one quarter of one screen's pixels. All this, and more, is documented in http://developer.android.com/reference/android/app/WallpaperManager.html
This is not "easy" coding--apps are easier than wallpaper. :-)
Good luck...George
P.S. I'll throw in one more thing: somewhere along the line you might want to retrieve the "desired minimum width" of the wallpaper desired by the launcher, so you can explicitly understand the virtualization implicit in xPixels. For example, in my engine constructor, I have
mContext = getApplicationContext();
mWM = WallpaperManager.getInstance(mContext);
mDW = mWM.getDesiredMinimumWidth();
My device has 320 pixel width; I get mDW = 640; as I scroll from screen to screen, xPixels changes by 80 each time...because four scrolls (across five screens) is supposed to double the amount of revealed artwork (this effect is called "parallax scrolling"). The rightmost section has xPixels equals 0; the center (of five) sections has xPixels = -160, etc.
I've used this code snippet to scale one image to fit on different screen sizes.
Bitmap image1, pic1;
image1 = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
float xScale = (float) canvas.getWidth() / image1.getWidth();
float yScale = (float) canvas.getHeight() / image1.getHeight();
float scale = Math.max(xScale, yScale); //selects the larger size to grow the images by
//scale = (float) (scale*1.1); //this allows for ensuring the image covers the whole screen.
scaledWidth = scale * image1.getWidth();
scaledHeight = scale * image1.getHeight();
pic1 = Bitmap.createScaledBitmap(image1, (int)scaledWidth, (int)scaledHeight, true);
Make sure that the edges don't contain vital information as it will be scaled out of the picture on some screen ratios.

Categories

Resources