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

SAVE RESTORE VIEW: fields not displayed
How to mute Notification Sound in Android
getResourceId method of TypedArray
How can i maintain the position of listview when user comes back to the activity by pressing back button?
Can I develop Android TV app without ADT-1?
Can't use debugger with NotificationListenerService
Android - Add views to item in a ListView
Image getting captured only in first fragment
ProgressDialog causes app to crash in android
Building android jar library with smaller targets
How to show second alertDialog window depending on the decision in the first alertDialog?
Android Simple Data Sharing
error on adding rout to map view in titanium
How to get AndroidManifest property with gradle
java.lang.NoClassDefFoundError: com.google.firebase.FirebaseOptions exception in API 19
Adding languages to android emulator

Categories

HOME
batch-file
algorithm
django
jpa
angular-formly
fftw
oracle-apex-5
devise
shiro
vscode-extensions
newrelic
entity-framework-core
cakephp-3.4
interpolation
python-multithreading
banner
modal-dialog
special-characters
tput
enterprise-architect
plink
3nf
mongodb-csharp
event-sourcing
imessage
guzzle
ios10.2
qliksense
epsilon
game-theory
gpio
pylons
failover
magento-1.8
anki
complex-numbers
quickfixj
batching
getlasterror
watchman
protein-database
odroid
metawidget
movilizer
drupal-theming
oim
jboss-4.2.x
opennms
chai-as-promised
podscms
amq
context-switch
reshape
lsyncd
flac
pocketsphinx-android
nss
bizagi
character-replacement
binomial-theorem
dnssec
mraid
punycode
webfinger
formview
universal-image-loader
gii
parallel-testing
reflector
ice-cube
merb
fb.ui
intersect
redis-py
panda3d
sentestingkit
wcf-configuration
faye
database-create
window-decoration
wiimote
screen-size
workflow-services
strsep
codebase
timertask
interprocess
pagemethods
perl5.12

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App