ich bin gerade dabei meine ersten Grafischen Erkenntnisse in Java(Android) zu sammeln.
Ich habe App erstellt in welcher sich ein Roter Bildschirm mit einer grünen und einer gelben "Scheibe"/"Kugel" befinden.
Diese sind in Bewegung und prallen von den "Wänden" ab jedoch wäre nun meine Frage wie ich denn nun es so machen könnte dass sie sich auch von einander abstoßen?
Hier der Code der Main:
Code:
package com.example.graphictest; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.view.View; public class BouncingBallView extends View { private int xMin = 0; // This view's bounds private int xMax; private int yMin = 0; private int yMax; float x = (float)Math.cos((double)System.currentTimeMillis())*10; float y = (float)Math.sin((double)System.currentTimeMillis())*10; private float ballRadius = 50; // Ball's radius private float ballX = ballRadius + 20; // Ball's center (x,y) private float ballY = ballRadius + 40; private float ball2X = ballRadius + 200; // Ball's center (x,y) private float ball2Y = ballRadius + 280; private float ballSpeedX = 5; // Ball's speed (x,y) private float ballSpeedY = 2; private float ballSpeed2X = 2; // Ball's speed (x,y) private float ballSpeed2Y = 5; private RectF ballBounds; // Needed for Canvas.drawOval private RectF ballBounds2; private Paint paint; // The paint (e.g. style, color) used for drawing private Paint paint2; // Constructor public BouncingBallView(Context context) { super(context); ballBounds = new RectF(); ballBounds2 = new RectF(); paint = new Paint(); paint2 = new Paint(); } // Called back to draw the view. Also called by invalidate(). @Override protected void onDraw(Canvas canvas) { // Draw the ball ballBounds.set(ballX-ballRadius, ballY-ballRadius, ballX+ballRadius, ballY+ballRadius); ballBounds2.set(ball2X-ballRadius, ball2Y-ballRadius, ball2X+ballRadius, ball2Y+ballRadius); paint2.setColor(Color.YELLOW); paint.setColor(Color.GREEN); canvas.drawOval(ballBounds, paint); canvas.drawOval(ballBounds2,paint2); // Update the position of the ball, including collision detection and reaction. update(); // Delay try { Thread.sleep(30); } catch (InterruptedException e) { } invalidate(); // Force a re-draw } // Detect collision and update the position of the ball. private void update() { // Get new (x,y) position ballX += ballSpeedX; ballY += ballSpeedY; ball2X += ballSpeed2X; ball2Y += ballSpeed2Y; // Detect collision and react if (ballX + ballRadius > xMax) { ballSpeedX = -ballSpeedX; ballX = xMax-ballRadius; } else if (ballX - ballRadius < xMin) { ballSpeedX = -ballSpeedX; ballX = xMin+ballRadius; } if (ballY + ballRadius > yMax) { ballSpeedY = -ballSpeedY; ballY = yMax - ballRadius; } else if (ballY - ballRadius < yMin) { ballSpeedY = -ballSpeedY; ballY = yMin + ballRadius; /** } else if (ballX + ballRadius == ball2X){ ballSpeedX = -ballSpeedX; ballX = ball2X + ballRadius; } else if (ballY + ballRadius == ball2Y){ ballSpeedY = -ballSpeedY; ballY = ball2Y + ballRadius; } */ //////-------////// if (ball2X + ballRadius > xMax) { ballSpeed2X = -ballSpeed2X; ball2X = xMax-ballRadius; } else if (ball2X - ballRadius < xMin) { ballSpeed2X = -ballSpeed2X; ball2X = xMin+ballRadius; } if (ball2Y + ballRadius > yMax) { ballSpeed2Y = -ballSpeed2Y; ball2Y = yMax - ballRadius; } else if (ball2Y - ballRadius < yMin) { ballSpeed2Y = -ballSpeed2Y; ball2Y = yMin + ballRadius; /** } else if (ball2Y + ballRadius == ballX){ * ballSpeed2X = -ballSpeed2X; * ball2X = ballX + ballRadius; * } else if (ball2Y + ballRadius == ballY){ * ballSpeed2Y = -ballSpeed2Y; * ball2Y = ballY + ballRadius; */ } } } // Called back when the view is first created or its size changes. @Override public void onSizeChanged(int w, int h, int oldW, int oldH) { // Set the movement bounds for the ball xMax = w-1; yMax = h-1; } }
EDIT: Die auskommentierten Teile sind Versuche meine Frage zu bewälltigen.
MfG Joshi