Note that there are some explanatory texts on larger screens.

plurals
  1. POCollision and deflection between two balls
    primarykey
    data
    text
    <p>This is a code for the movement of two balls in a rectangle.They deflect whenever they hit the wall but don't deflect when they collide with each other .Can someone help? MovementView .java is as follows.</p> <pre><code>package com.example.movements; import android.view.SurfaceView; import android.app.LocalActivityManager; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.view.SurfaceHolder; public class MovementView extends SurfaceView implements SurfaceHolder.Callback{ private int xPos,xPos1; private int yPos,yPos1; private int xVel,xVel1; private int yVel,yVel1; private int width; private int height; private int circleRadius,circleRadius1; private Paint circlePaint,circlePaint1; UpdateThread updateThread; public MovementView(Context context) { super(context); getHolder().addCallback(this); circleRadius = 10; circlePaint = new Paint(); circlePaint.setColor(Color.BLUE); xVel = 10; yVel = 10; circleRadius1 = 10; circlePaint1 = new Paint(); circlePaint1.setColor(Color.MAGENTA); xVel1 = 11; yVel1 = 11; } @Override protected void onDraw(Canvas canvas) { canvas.drawColor(Color.WHITE); canvas.drawCircle(xPos, yPos, circleRadius, circlePaint); canvas.drawCircle(xPos1, yPos1, circleRadius1, circlePaint1); } public void updatePhysics() { xPos += xVel; yPos += yVel; if (yPos - circleRadius &lt; 0 || yPos + circleRadius &gt; height) { if (yPos - circleRadius &lt; 0) { yPos = circleRadius; }else{ yPos = height - circleRadius; } yVel *= -1; } if (xPos - circleRadius &lt; 0 || xPos + circleRadius &gt; width) { if (xPos - circleRadius &lt; 0) { xPos = circleRadius; } else { xPos = width - circleRadius; } xVel *= -1; } xPos1 += xVel1; yPos1 += yVel1; if (yPos1 - circleRadius1 &lt; 0 || yPos1 + circleRadius1 &gt; height) { if (yPos1 - circleRadius1 &lt; 0) { yPos1 = circleRadius1; }else{ yPos1 = height - circleRadius1; } yVel1 *= -1; } if (xPos1 - circleRadius1 &lt; 0 || xPos1 + circleRadius1 &gt; width) { if (xPos1 - circleRadius1 &lt; 0) { xPos1 = circleRadius1; } else { xPos1 = width - circleRadius1; } xVel1 *= -1; } } public void surfaceCreated(SurfaceHolder holder) { Rect surfaceFrame = holder.getSurfaceFrame(); width = surfaceFrame.width(); height = surfaceFrame.height(); xPos = width / 2; yPos = circleRadius; xPos1 = width / 2; yPos1 = circleRadius1; updateThread = new UpdateThread(this); updateThread.setRunning(true); updateThread.start(); } public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } public void surfaceDestroyed(SurfaceHolder holder) { boolean retry = true; updateThread.setRunning(false); while (retry) { try { updateThread.join(); retry = false; } catch (InterruptedException e) { } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload