android


Make ball bounce and eventually come to rest


I've written some code to move a ball around the screen using an orientation sensor. I wanted to get the ball to bounce when it hits the bottom of the screen, sort of like under gravity. Could somebody help out with implementing the physics in my existing code? Flipping the velocity doesn't seem to work. Here's my ball class:
package perseus.gfx.test;
import everything
public class Ball extends View {
RectF lol;
Paint paint, lpaint;
Bitmap bitmap;
Canvas canvas;
private float ballx = 150;
private float bally = 140;
private double speedx = 0;
private double speedy = 0; //ignore
private double accx, accy=0;
private float rad = 20;
private float mult = 0.5f;
private double xv, yv, xS, yS;
int width, height;
int xmax, ymax;
int xmin, ymin;
public Ball(Context context) {
super(context);
lol = new RectF();
paint = new Paint();
paint.setColor(Color.CYAN);
lpaint = new Paint();
lpaint.setColor(Color.GRAY);
}
public void moveBall() {
xv = accx * mult;
yv = accy * mult;
xS = xv * mult;
yS = yv * mult;
ballx -= xS;
bally -= yS;
// Collision detection
if (ballx + rad > xmax) {
ballx = xmax-rad;
}
else if (ballx - rad < 0) {
ballx = rad;
}
if (bally + rad > 2*ymax/3) //Shouldn't take up the whole screen
{
bally = 2*ymax/3 - rad;
}
else if (bally - rad < 0) {
bally = rad;
}
try {
Thread.sleep(20);
} catch(InterruptedException e) {}
invalidate();
}
#Override
public void onMeasure(int widthM, int heightM)
{
width = View.MeasureSpec.getSize(widthM);
height = View.MeasureSpec.getSize(heightM);
xmax = width-1;
ymax = height-1;
xmin = 0;
ymin = 0;
setMeasuredDimension(width, height);
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
}
#Override
public void onDraw(Canvas canvas)
{
canvas.drawBitmap(bitmap, 0, 0, paint);
lol.set(ballx - rad, bally-rad, ballx + rad, bally+rad);
canvas.drawLine(0, 2*height/3, width, 2*height/3, lpaint);
canvas.drawOval(lol, paint);
canvas.drawText(xv + " " + yv, 0, height/2, lpaint);
canvas.save();
moveBall();
canvas.restore();
}
}
So the key is to just add a bit of friction, just remove a tiny bit of acceleration (negative!) at each step in moveBall(). E.g.
float friction = -0.001;
xv = accx * mult + friction;
yv = accy * mult + friction;
Then adjust the variable friction accordingly to suit your needs. For the collision you need to invert the velocity, e.g. bounce on bottom:
bally = -bally;

Related Links

APDU Case 2 Response too short
Extra tab in the scrollable tablayout
Android: LatLng object to String format
downloading image using Glide(Android)
Getting Location out of a LocationResult in onReceive()
Flurry Analytics Integration Errors
Facebook Unity SDK on Android crashes when fb.init is called
Is possible decelerate ViewPager transitions?
Data retrieval parse.com
How do I make it so I can have the screen scrolled down more on Activity1 after pressing the back button on Activity2?
Spinner setOnItemSelectedListener is not invoked
reworking maploading from sdcard to assets in android
Android Studio gradle can't import groovy.swing.SwingBuilder
Unable to run virtual box and emulator in Genymotion
SharedPreference not working- can't see why
Is it possible to disable logging for Android.Vibrator?

Categories

HOME
xml
kentico
localization
dns
android-studio-2.2
pyqt
static
toolbox
yahoo-finance
sh
sitecore
jsf-2.2
can
xbee
alsa
3d-reconstruction
android-json
google-cloud-datalab
boto3
title
modal-dialog
computer-science
rcloud
windows-ce
ycsb
openbugs
http-post
powershell-v5.0
fileserver
selinux
scriptella
siri
gpio
fable-f#
android-sharedpreferences
android-maps
inversion-of-control
spotipy
google-cast
angularjs-material
simplecv
xajax
apic
sim-card
typesafe-config
ogc
stringtokenizer
watchface
parser-generator
android-ibeacon
print-css
dynamic-linq
openrasta
serde
robomongo
posixct
static-cast
lsyncd
vibrate
ampps
bootstrap-tags-input
simplepie
hadoop-2.7.2
webjars
ng-grid
pre-build-event
android-wake-lock
html-to-pdf
np-spring
concept-insights
r-package
wiredep
video-player
text-search
lasso
graphics2d
pyobjc
jquery-forms-plugin
dbaccess
webmatrix-3
named-parameters
live555
draw2d-js
menubar
koala
accesscontrolexception
spring-3
beaker-testing
jquery-1.9
mantle
chart-director
qtconcurrent
objectquery
oracle-enterprise-linux
jsr286
screen-size
strsep
quotation-marks
socketserver
aspmenu
cfwindow
application-planning
soappy

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App