i am trying to implement Deceleration on the bouncy ball. i am able to move the ball up and down but i wanted to add Declaration so when i drop the ball from height it jumps then strike the ground bounce once again and slowly slowly its speed decreases and stop at the end.
Here is My code
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.ImageView;
public class AnimatedView extends ImageView{
private Context mContext;
int x = -1;
int y = -1;
private int xVelocity = 0;
private int yVelocity = 25;
private Handler h;
private final int FRAME_RATE = 20;
public AnimatedView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
h = new Handler();
}
private Runnable r = new Runnable() {
#Override
public void run() {
invalidate();
}
};
protected void onDraw(Canvas c) {
BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball);
if (x<0 && y <0) {
x = this.getWidth()/2;
y = this.getHeight()/2;
} else {
x += xVelocity;
y += yVelocity;
if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
xVelocity = xVelocity*-1;
}
if ((y > this.getHeight() - ball.getBitmap().getHeight()) || (y < 0)) {
yVelocity = yVelocity*-1;
}
}
c.drawBitmap(ball.getBitmap(), x, y, null);
h.postDelayed(r, FRAME_RATE);
}
Hope Anyone Can Help me.
Thanks in Advance
Acceleration is to velocity as velocity is to position. You just need to do what you're doing with your position to your velocity.
The simplest solution is to add
yVelocity += yAccel;
to the top of onDraw where yAccel is a int that you want added to the yVelocity every tick. Based on your velocity values
private int yAccel = -2;
might be appropriate.
edit: You should also add checks to ensure the ball does not go into the ground. I added these in the blocks that reverse the velocity, the following code works for me. If it don't work for you, there might be something wrong with another part of your project.
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.ImageView;
public class AnimatedView extends ImageView {
private Context mContext;
int x = -1;
int y = -1;
private int xVelocity = 0;
private int yVelocity = 25;
private int yAccel = 2;
private Handler h;
private final int FRAME_RATE = 20;
public AnimatedView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
h = new Handler();
}
private Runnable r = new Runnable() {
#Override
public void run() {
invalidate();
}
};
protected void onDraw(Canvas c) {
yVelocity += yAccel;
BitmapDrawable ball = (BitmapDrawable) mContext.getResources()
.getDrawable(R.drawable.cakes);
if (x < 0 && y < 0) {
x = this.getWidth() / 2;
y = this.getHeight() / 2;
} else {
x += xVelocity;
y += yVelocity;
if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
xVelocity = xVelocity * -1;
x = Math.min(this.getWidth() - ball.getBitmap().getWidth(), x);
x = Math.max(0, x);
}
if ((y > this.getHeight() - ball.getBitmap().getHeight())
|| (y < 0)) {
yVelocity = (int)(yVelocity * -0.8f);
y = Math.min(this.getHeight() - ball.getBitmap().getHeight(), y);
y = Math.max(0, y);
}
}
c.drawBitmap(ball.getBitmap(), x, y, null);
h.postDelayed(r, FRAME_RATE);
}
}
Related
i keep getting error draw to canvas
Error
The error keep happening when I tried to draw to the canvas the player after the user move the joystick
it always writes me "cant lock canvas ,canvas already locked"
i'm kind of beginner in game development and sorry if the question is a little unclear if you need more details just say and I will send you more info
Main class - the main activity
package app.shahardagan.Raven;
import android.graphics.Point;
import android.os.Bundle;
import android.app.Activity;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class Main extends Activity {
RelativeLayout layout_joystick;
ImageView image_joystick, image_border;
GameEngine gameEngine;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get a Display object to access screen details
Display display = getWindowManager().getDefaultDisplay();
// Load the resolution into a Point object
Point size = new Point();
size.set(display.getWidth(),display.getHeight());
layout_joystick = (RelativeLayout)findViewById(R.id.layout_joystick);
gameEngine = new GameEngine(this,size.x,size.y);
setContentView(gameEngine);
}
#Override
protected void onResume(){
super.onResume();
gameEngine.resume();
}
#Override
protected void onPause(){
super.onPause();
gameEngine.pause();
}
}
GameEngine class - responsible to draw the player and update the game
package app.shahardagan.Raven;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class GameEngine extends SurfaceView implements Runnable {
JoyStickClass js;
// This is our thread
private Thread gameThread = null;
// This is new. We need a SurfaceHolder
// When we use Paint and Canvas in a thread
// We will see it in action in the draw method soon.
private SurfaceHolder ourHolder;
// A boolean which we will set and unset
// when the game is running- or not.
private volatile boolean playing;
// Game is paused at the start
private boolean paused = true;
// A Canvas and a Paint object
private Canvas canvas;
private Paint paint;
// How wide and high is the screen?
private int screenX;
private int screenY;
// This variable tracks the game frame rate
private long fps;
// This is used to help calculate the fps
private long timeThisFrame;
private Context context;
private Player player;
private Drawable playerImage;
public GameEngine(Context context, int x, int y) {
// This calls the default constructor to setup the rest of the object
super(context);
this.context = context;
// Initialize ourHolder and paint objects
ourHolder = getHolder();
paint = new Paint();
// Initialize screenX and screenY because x and y are local
screenX = x;
screenY = y;
prepareLevel();
}
private void prepareLevel(){
player = new Player(context,screenX,screenY);
}
public void PlayerControls(Context context,RelativeLayout layout_joystick, ImageView image_joystick, ImageView image_border){
js = new JoyStickClass(context, layout_joystick, R.drawable.image_button);
js.setStickSize(150, 150);
js.setLayoutSize(500, 500);
js.setLayoutAlpha(150);
js.setStickAlpha(100);
js.setOffset(90);
js.setMinimumDistance(50);
layout_joystick.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
js.drawStick(arg1);
if(arg1.getAction() == MotionEvent.ACTION_DOWN || arg1.getAction() == MotionEvent.ACTION_MOVE) {
int direction = js.get8Direction();
if(direction == JoyStickClass.STICK_UP) {
} else if(direction == JoyStickClass.STICK_UPRIGHT) {
player.setMovementState(Player.UP);
} else if(direction == JoyStickClass.STICK_RIGHT) {
} else if(direction == JoyStickClass.STICK_DOWNRIGHT) {
} else if(direction == JoyStickClass.STICK_DOWN) {
} else if(direction == JoyStickClass.STICK_DOWNLEFT) {
} else if(direction == JoyStickClass.STICK_LEFT) {
} else if(direction == JoyStickClass.STICK_UPLEFT) {
} else if(direction == JoyStickClass.STICK_NONE) {
}
} else if(arg1.getAction() == MotionEvent.ACTION_UP) {
}
return true;
}
});
}
// Runs when the OS calls onPause on BreakoutActivity method
public void pause() {
playing = false;
try {
gameThread.join();
} catch (InterruptedException e) {
Log.e("Error:", "joining thread");
}
}
// Runs when the OS calls onResume on BreakoutActivity method
public void resume() {
playing = true;
gameThread = new Thread(this);
gameThread.start();
}
#Override
public void run() {
while (playing) {
// Capture the current time in milliseconds in startFrameTime
long startFrameTime = System.currentTimeMillis();
// Update the frame
// Update the frame
if(!paused){
update();
}
// Draw the frame
draw();
// Calculate the fps this frame
// We can then use the result to
// time animations and more.
timeThisFrame = System.currentTimeMillis() - startFrameTime;
if (timeThisFrame >= 1) {
fps = 1000 / timeThisFrame;
}
}
}
private void draw() {
// Make sure our drawing surface is valid or game will crash
if (ourHolder.getSurface().isValid()) {
// Lock the canvas ready to draw
canvas = ourHolder.lockCanvas();
// Draw the background color
canvas.drawColor(Color.argb(255, 26, 128, 182));
// Draw everything to the screen
// Choose the brush color for drawing
paint.setColor(Color.argb(255, 255, 255, 255));
canvas.drawBitmap(player.getBitmap(),player.getX(),screenY - player.getHeight(),paint);
}
}
private void update() {
player.update(fps);
}
}
Player class - the player class methods
package app.shahardagan.Raven;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.RectF;
public class Player {
// RectF is an object that holds four coordinates - just what we need
private RectF rect;
private Bitmap bitmap;
// How long will our paddle will be
private float length;
private float height;
// X is the far left of the rectangle which forms our paddle
private float x;
private float y;
// Which ways can the player move
final int STOPPED = 0;
public static final int UP = 1;
public static final int UPRIGHT = 2;
public static final int RIGHT = 3;
public static final int DOWNRIGHT = 4;
public static final int DOWN = 5;
public static final int DOWNLEFT = 6;
public static final int LEFT = 7;
public static final int UPLEFT = 8;
// Is the paddle moving and in which direction
private int playerMoving = STOPPED;
private int playerSpeed;
public Player(Context context,int screenX, int screenY){
rect = new RectF();
length =screenX/10;
height = screenX/10;
x = screenX;
y= 0;
bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.eagle_fly);
bitmap = Bitmap.createScaledBitmap(bitmap,(int)length,(int)height,false);
playerSpeed = 350;
}
public RectF getRect(){
return rect;
}
public Bitmap getBitmap(){
return bitmap;
}
public float getX(){ return x; }
public float getHeight(){return height;}
private float getLength(){return length;}
public void setMovementState(int state){
playerMoving = state;
}
void update(long fps){
if(playerMoving == LEFT){
x = x - playerSpeed / fps;
}
if(playerMoving == RIGHT){
x = x + playerSpeed / fps;
}
rect.top = y;
rect.bottom = y+ height;
rect.left = x;
rect.right = x + length;
}
}
Joy stick class
package app.shahardagan.Raven;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
public class JoyStickClass {
public static final int STICK_NONE = 0;
public static final int STICK_UP = 1;
public static final int STICK_UPRIGHT = 2;
public static final int STICK_RIGHT = 3;
public static final int STICK_DOWNRIGHT = 4;
public static final int STICK_DOWN = 5;
public static final int STICK_DOWNLEFT = 6;
public static final int STICK_LEFT = 7;
public static final int STICK_UPLEFT = 8;
private int STICK_ALPHA = 200;
private int LAYOUT_ALPHA = 200;
private int OFFSET = 0;
private Context mContext;
private ViewGroup mLayout;
private LayoutParams params;
private int stick_width, stick_height;
private int position_x = 0, position_y = 0, min_distance = 0;
private float distance = 0, angle = 0;
private DrawCanvas draw;
private Paint paint;
private Bitmap stick;
private boolean touch_state = false;
public JoyStickClass (Context context, ViewGroup layout, int stick_res_id) {
mContext = context;
stick = BitmapFactory.decodeResource(mContext.getResources(),
stick_res_id);
stick_width = stick.getWidth();
stick_height = stick.getHeight();
draw = new DrawCanvas(mContext);
paint = new Paint();
mLayout = layout;
params = mLayout.getLayoutParams();
}
public void drawStick(MotionEvent arg1) {
position_x = (int) (arg1.getX() - (params.width / 2));
position_y = (int) (arg1.getY() - (params.height / 2));
distance = (float) Math.sqrt(Math.pow(position_x, 2) + Math.pow(position_y, 2));
angle = (float) cal_angle(position_x, position_y);
if(arg1.getAction() == MotionEvent.ACTION_DOWN) {
if(distance <= (params.width / 2) - OFFSET) {
draw.position(arg1.getX(), arg1.getY());
draw();
touch_state = true;
}
} else if(arg1.getAction() == MotionEvent.ACTION_MOVE && touch_state) {
if(distance <= (params.width / 2) - OFFSET) {
draw.position(arg1.getX(), arg1.getY());
draw();
} else if(distance > (params.width / 2) - OFFSET){
float x = (float) (Math.cos(Math.toRadians(cal_angle(position_x, position_y))) * ((params.width / 2) - OFFSET));
float y = (float) (Math.sin(Math.toRadians(cal_angle(position_x, position_y))) * ((params.height / 2) - OFFSET));
x += (params.width / 2);
y += (params.height / 2);
draw.position(x, y);
draw();
} else {
mLayout.removeView(draw);
}
} else if(arg1.getAction() == MotionEvent.ACTION_UP) {
mLayout.removeView(draw);
touch_state = false;
}
}
public int[] getPosition() {
if(distance > min_distance && touch_state) {
return new int[] { position_x, position_y };
}
return new int[] { 0, 0 };
}
public int getX() {
if(distance > min_distance && touch_state) {
return position_x;
}
return 0;
}
public int getY() {
if(distance > min_distance && touch_state) {
return position_y;
}
return 0;
}
public float getAngle() {
if(distance > min_distance && touch_state) {
return angle;
}
return 0;
}
public float getDistance() {
if(distance > min_distance && touch_state) {
return distance;
}
return 0;
}
public void setMinimumDistance(int minDistance) {
min_distance = minDistance;
}
public int getMinimumDistance() {
return min_distance;
}
public int get8Direction() {
if(distance > min_distance && touch_state) {
if(angle >= 247.5 && angle < 292.5 ) {
return STICK_UP;
} else if(angle >= 292.5 && angle < 337.5 ) {
return STICK_UPRIGHT;
} else if(angle >= 337.5 || angle < 22.5 ) {
return STICK_RIGHT;
} else if(angle >= 22.5 && angle < 67.5 ) {
return STICK_DOWNRIGHT;
} else if(angle >= 67.5 && angle < 112.5 ) {
return STICK_DOWN;
} else if(angle >= 112.5 && angle < 157.5 ) {
return STICK_DOWNLEFT;
} else if(angle >= 157.5 && angle < 202.5 ) {
return STICK_LEFT;
} else if(angle >= 202.5 && angle < 247.5 ) {
return STICK_UPLEFT;
}
} else if(distance <= min_distance && touch_state) {
return STICK_NONE;
}
return 0;
}
public int get4Direction() {
if(distance > min_distance && touch_state) {
if(angle >= 225 && angle < 315 ) {
return STICK_UP;
} else if(angle >= 315 || angle < 45 ) {
return STICK_RIGHT;
} else if(angle >= 45 && angle < 135 ) {
return STICK_DOWN;
} else if(angle >= 135 && angle < 225 ) {
return STICK_LEFT;
}
} else if(distance <= min_distance && touch_state) {
return STICK_NONE;
}
return 0;
}
public void setOffset(int offset) {
OFFSET = offset;
}
public int getOffset() {
return OFFSET;
}
public void setStickAlpha(int alpha) {
STICK_ALPHA = alpha;
paint.setAlpha(alpha);
}
public int getStickAlpha() {
return STICK_ALPHA;
}
public void setLayoutAlpha(int alpha) {
LAYOUT_ALPHA = alpha;
mLayout.getBackground().setAlpha(alpha);
}
public int getLayoutAlpha() {
return LAYOUT_ALPHA;
}
public void setStickSize(int width, int height) {
stick = Bitmap.createScaledBitmap(stick, width, height, false);
stick_width = stick.getWidth();
stick_height = stick.getHeight();
}
public void setStickWidth(int width) {
stick = Bitmap.createScaledBitmap(stick, width, stick_height, false);
stick_width = stick.getWidth();
}
public void setStickHeight(int height) {
stick = Bitmap.createScaledBitmap(stick, stick_width, height, false);
stick_height = stick.getHeight();
}
public int getStickWidth() {
return stick_width;
}
public int getStickHeight() {
return stick_height;
}
public void setLayoutSize(int width, int height) {
params.width = width;
params.height = height;
}
public int getLayoutWidth() {
return params.width;
}
public int getLayoutHeight() {
return params.height;
}
private double cal_angle(float x, float y) {
if(x >= 0 && y >= 0)
return Math.toDegrees(Math.atan(y / x));
else if(x < 0 && y >= 0)
return Math.toDegrees(Math.atan(y / x)) + 180;
else if(x < 0 && y < 0)
return Math.toDegrees(Math.atan(y / x)) + 180;
else if(x >= 0 && y < 0)
return Math.toDegrees(Math.atan(y / x)) + 360;
return 0;
}
private void draw() {
try {
mLayout.removeView(draw);
} catch (Exception e) { }
mLayout.addView(draw);
}
private class DrawCanvas extends View{
float x, y;
private DrawCanvas(Context mContext) {
super(mContext);
}
public void onDraw(Canvas canvas) {
canvas.drawBitmap(stick, x, y, paint);
}
private void position(float pos_x, float pos_y) {
x = pos_x - (stick_width / 2);
y = pos_y - (stick_height / 2);
}
}
}
In your draw() method, you're locking the Canvas, but not unlocking the Canvas after you finish drawing. So, when you re-enter your draw method, the Canvas is still locked from the previous call.
You likely need to add unlockCanvasAndPost(Canvas canvas) after you finish drawing. See the full docs here.
I understand that this is technically a repeat question, but all of the similar questions include code I do not understand, so I decided to ask the question using code I understand.
I am attempting to make a flappy bird style game to try android programing and I cannot get Rect.intersects to change the player's score when the player collides with an object (cloud)
Thanks in advance!!
View class:
package com.gregsapps.fallingbird;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
public class GameView extends View{
private Bird bird;
private boolean runOnce = false;
private Context context;
private Paint red;
ArrayList<Cloud> clouds = new ArrayList<Cloud>();
private int cloudDelay;
private Collision collision;
//private Paint textPaint;
public GameView(Context context) {
super(context);
this.context = context;
this.setDrawingCacheEnabled(true);
red = new Paint();
red.setColor(Color.RED);
red.setTextSize(100f);
collision = new Collision();
//textPaint.setColor(Color.BLACK);
//textPaint.setTextAlign(Align.RIGHT);
// TODO add setup code
}
protected void onDraw(Canvas canvas){
update(canvas);
System.out.println(bird.score);
//TODO add drawing code
this.buildDrawingCache();
//bird.canvasImage = this.getDrawingCache(true);
canvas.drawColor(Color.rgb(10, 255, 255));
canvas.drawRect(bird.birdRect, red);
canvas.drawBitmap(bird.image, bird.x, bird.y, null);
for(int i = 0; i < clouds.size(); i++){
System.out.println("Drawing cloud");
Cloud cloud = (Cloud) clouds.get(i);
cloud.move(5);
System.out.println(cloud.leftX + "/t" + cloud.rightX);
canvas.drawRect(cloud.rightCloud, red);
canvas.drawRect(cloud.leftCloud, red);
canvas.drawBitmap(cloud.image, cloud.leftX, cloud.leftY, null);
canvas.drawBitmap(cloud.image, cloud.rightX, cloud.rightY, null);
if(cloud.leftY <= -144)clouds.remove(cloud);
if(bird.y > cloud.leftY + bird.height) bird.score++;
if(Rect.intersects(bird.birdRect, cloud.leftCloud)){
bird.score = 0;
}
else if(Rect.intersects(bird.birdRect, cloud.rightCloud)){
bird.score = 0;
}
}
canvas.drawLine(canvas.getWidth()/2 - 1, 0, canvas.getWidth()/2 - 1, canvas.getHeight(), red);
cloudDelay --;
if(cloudDelay <= 0){
System.out.println("new cloud");
Cloud cloud = new Cloud(com.gregsapps.fallingbird.R.drawable.cloud, context);
System.out.println("added");
clouds.add(cloud);
cloudDelay = 175;
}
canvas.drawText(Integer.toString(bird.score/12), 50, 100, red);
invalidate();
}
private void update(Canvas canvas){
//TODO add code to update stuff
if(runOnce == false){
bird = new Bird(canvas, com.gregsapps.fallingbird.R.drawable.bird, context);
runOnce = true;
StaticVarHandler.canvasHeight = canvas.getHeight();
StaticVarHandler.canvasWidth = canvas.getWidth();
}
bird.move();
}
}
Cloud class:
package com.gregsapps.fallingbird;
import java.util.Random;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
public class Cloud {
public int leftX;
public int leftY;
public int rightX;
public int rightY;
private Random random;
public Bitmap image;
private Context context;
public Rect leftCloud;
public Rect rightCloud;
public int height;
public int width;
Cloud(int image, Context context){
this.context = context;
this.image = BitmapFactory.decodeResource(this.context.getResources(), R.drawable.cloud);
random = new Random();
leftX = random.nextInt(StaticVarHandler.canvasWidth-(StaticVarHandler.birdWidth*2));
rightX = leftX + StaticVarHandler.birdWidth*2;
rightY = leftY = StaticVarHandler.canvasHeight+this.image.getHeight();
leftX -= this.image.getWidth();
leftCloud = new Rect(leftX, leftY, this.image.getWidth(), this.image.getHeight());
rightCloud = new Rect(rightX, rightY, this.image.getWidth(), this.image.getHeight());
}
public void move(int scrollSpeed){
leftCloud.offset(0, -scrollSpeed);
rightCloud.offset(0, -scrollSpeed);
leftY-=scrollSpeed;
rightY-=scrollSpeed;
}
}
Bird class:
package com.gregsapps.fallingbird;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
public class Bird {
public int x;
public int y;
private int speed;
public Bitmap image;
Context context;
private int gravity;
public int width;
public int height;
private int canvasWidth;
private int canvasHeight;
public Bitmap canvasImage;
public boolean touch;
public int touchX;
public int touchY;
private int gravDelay = 0;
private int jump = 0;
public int score;
public Rect birdRect;
Bird(Canvas canvas, int image, Context context){
this.context = context;
this.image = BitmapFactory.decodeResource(this.context.getResources(), image);
x = canvas.getWidth()/2 - this.image.getWidth()/2;
y = 10;
speed = this.image.getWidth()/10;
//setup gravity, speed, width and height attributes
speed = canvas.getWidth()/25;
gravity = speed/10;
StaticVarHandler.birdWidth = width = this.image.getWidth();
height = this.image.getHeight();
canvasWidth = canvas.getWidth();
canvasHeight = canvas.getHeight();
System.out.println(canvasWidth);
System.out.println(canvas.getWidth());
birdRect = new Rect(x, y, this.image.getWidth(), this.image.getHeight());
}
public void move(){
gravDelay --;
jump --;
if(StaticVarHandler.touch) jump = 3;
if(jump >= 0){
if(StaticVarHandler.touchX < canvasWidth/2){
x -= speed/3;
}
if(StaticVarHandler.touchX > canvasWidth/2){
x += speed/3;
}
StaticVarHandler.touch = false;
if(jump == 0) gravDelay = 7;
}
else if(gravDelay <= 0){
System.out.println("GRAVITY");
if(x+width/2 < canvasWidth/2){
x += gravity;
//code to move bird via gravity
}
if(x+width/2 > canvasWidth/2){
x -= gravity;
//same as above but other side
}
gravDelay = 1;
}
if(x <= 0){
score = 0;
x = 0;
}
else if(x+width >= canvasWidth){
score = 0;
x = canvasWidth - width;
}
birdRect.offsetTo(x-1, y-1);
}
private void collisionCheck(){
if(longEquation()){
}
}
}
I think I found your problem.
birdRect = new Rect(x, y, this.image.getWidth(), this.image.getHeight());
and
leftCloud = new Rect(leftX, leftY, this.image.getWidth(), this.image.getHeight());
rightCloud = new Rect(rightX, rightY, this.image.getWidth(), this.image.getHeight());
Should not use getWidth() or getHeight() but should use x + width and y + height.
public Rect (int left, int top, int right, int bottom)
Added in API level 1 Create a new rectangle with the specified
coordinates. Note: no range checking is performed, so the caller must
ensure that left <= right and top <= bottom.
Parameters
left The X coordinate of the left side of the rectangle
top The Y coordinate of the top of the rectangle
right The X
coordinate of the right side of the rectangle
bottom The Y coordinate
of the bottom of the rectangle
This is from the documentation.
I want to move a sprite between two points.
first point is x=0 and y=0 and the second point is a point where the user touched the screen.
To move, I want to use an equation to go through the two points which is y=ax+b.
I have attempted this in the method move() but the sprite does not move.
Please help.
Class Display:
package com.example.name;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class Display extends SurfaceView {
private Bitmap bmp;
private SurfaceHolder holder;
private GameLoopThread gameLoopThread;
private Sprite sprite;
private long lastClick;
private float x = 0.0F;
private float y = 0.0F;
public Display(Context context) {
super(context);
bmp = BitmapFactory.decodeResource(getResources(), R.drawable.gonia);
sprite = new Sprite(this, bmp, x, y, x, y);
gameLoopThread = new GameLoopThread(this);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
gameLoopThread.setRunning(false);
while (retry) {
try {
gameLoopThread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
gameLoopThread.setRunning(true);
gameLoopThread.start();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
}
});
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
sprite.onDraw(canvas);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
if (System.currentTimeMillis() - lastClick > 500) {
lastClick = System.currentTimeMillis();
x = (float) sprite.ostatniaWartoscX();
y = (float) sprite.ostatniaWartoscY();
float gotox = (float) event.getX();
float gotoy = (float) event.getY();
synchronized (getHolder()) {
sprite = new Sprite(this, bmp, x, y, gotox, gotoy);
}
}
return true;
}
}
Class Sprite:
package com.example.name;
import android.graphics.Bitmap;
import android.graphics.Canvas;
public class Sprite {
private float a;
private float b;
private float x;
private float y;
private float gotox;
private float gotoy;
private int executeMove = 0;
private Display display;
private Bitmap bmp;
public Sprite(Display display, Bitmap bmp, float x, float y, float gotox,
float gotoy) {
this.display = display;
this.bmp = bmp;
this.x = x;
this.y = y;
this.gotox = gotox;
this.gotoy = gotoy;
}
void update() {
if (x < gotox) {x++;executeMove = 1;}
if (x > gotox) {x--;executeMove = 1;}
if (executeMove == 1) {move();}
executeMove = 0;
}
void move() {
float x1 = x;
float y1 = y;
float x2 = gotox;
float y2 = gotoy;
a = (y2-y1)/(x2-x1);
b = y1 - x1*a;
y = x1 * a + b;
}
public float ostatniaWartoscX() {
return x;
}
public float ostatniaWartoscY() {
return y;
}
public void onDraw(Canvas canvas) {
update();
canvas.drawBitmap(bmp, x, y, null);
}
}
Thank You!
You need Bresenham's line algorithm to move your player. You can even cut it short that you only need to calculate the next movement (not the whole line). It's a simple/easy and low-calorie-algorithm.
You must adjust it to your needs.
public static ArrayList<Point> getLine(Point start, Point target) {
ArrayList<Point> ret = new ArrayList<Point>();
int x0 = start.x;
int y0 = start.y;
int x1 = target.x;
int y1 = target.y;
int sx = 0;
int sy = 0;
int dx = Math.abs(x1-x0);
sx = x0<x1 ? 1 : -1;
int dy = -1*Math.abs(y1-y0);
sy = y0<y1 ? 1 : -1;
int err = dx+dy, e2; /* error value e_xy */
for(;;){ /* loop */
ret.add( new Point(x0,y0) );
if (x0==x1 && y0==y1) break;
e2 = 2*err;
if (e2 >= dy) { err += dy; x0 += sx; } /* e_xy+e_x > 0 */
if (e2 <= dx) { err += dx; y0 += sy; } /* e_xy+e_y < 0 */
}
return ret;
}
Here is an example of code I use to move an object between two points.
Math.atan2 calculates the theta angle (-pi to +pi) which is the trajectory the object needs to travel. Delta is the time between this this update and the last update and velocity is the desired speed of the object. These all need to be multiplied together and then added to the current position to get the new position.
#Override
protected void update(float delta) {
double theta = Math.atan2(targetPos.y - pos.y, targetPos.x - pos.x);
double valX = (delta * velocity) * Math.cos(theta);
double valY = (delta * velocity) * Math.sin(theta);
pos.x += valX;
pos.y += valY;
}
I have written the code for movement of two balls within a rectangle(canvas). The balls deflect in opposite direction when they hit the top, bottom, left or right of the rectangle. But,I've tried in vain to make the balls collide with each other and deflect in opposite direction. I have searched many sites and articles but in vain. Can someone please help.
This is MainActivity.java
package com.example.movements;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MovementView(this));
}
}
This is MovementView.java
package com.example.movements;
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 < 0 || yPos + circleRadius > height) {
if (yPos - circleRadius < 0) {
yPos = circleRadius;
}else{
yPos = height - circleRadius;
}
yVel *= -1;
}
if (xPos - circleRadius < 0 || xPos + circleRadius > width) {
if (xPos - circleRadius < 0) {
xPos = circleRadius;
} else {
xPos = width - circleRadius;
}
xVel *= -1;
}
xPos1 += xVel1;
yPos1 += yVel1;
if (yPos1 - circleRadius1 < 0 || yPos1 + circleRadius1 > height) {
if (yPos1 - circleRadius1 < 0) {
yPos1 = circleRadius1;
}else{
yPos1 = height - circleRadius1;
}
yVel1 *= -1;
}
if (xPos1 - circleRadius1 < 0 || xPos1 + circleRadius1 > width) {
if (xPos1 - circleRadius1 < 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) {
}
}
}
}
This is UpdateThread.java
package com.example.movements;
import android.view.SurfaceHolder;
public class UpdateThread extends Thread {
private long time;
private final int fps = 20;
private boolean toRun = false;
private MovementView movementView;
private SurfaceHolder surfaceHolder;
public UpdateThread(MovementView rMovementView) {
movementView = rMovementView;
surfaceHolder = movementView.getHolder();
}
public void setRunning(boolean run) {
toRun = run;
}
#Override
public void run() {
Canvas c;
while (toRun) {
long cTime = System.currentTimeMillis();
if ((cTime - time) <= (1000 / fps)) {
c = null;
try {
c = surfaceHolder.lockCanvas(null);
movementView.updatePhysics();
movementView.onDraw(c);
} finally {
if (c != null) {
surfaceHolder.unlockCanvasAndPost(c);
}
}
}
time = cTime;
}
}
}
To check wether they are hitting each other, you can just check if the distance between their centers is smaller than (radius*2). To make a nice deflection, you'll have to do some math that's beyond me. Here is another answer on that.
I've googled it a bit and apparantly the term used in physics for this problem is called elastic collision. I've found a nice tutorial on the subject here (especially the dynamic circle-circle collision bit). Also, here is an applet which demonstrates this. The source code for this can be found [here]
Can anyone help, my app does not seem to do the onDraw() in the GraView class, even if .invalidate is called upon from either inside or outside the class. I managed to figure out with system.err that it does invalidate the view, however it never gets in the onDraw( ), even though the rest of the app keeps running. I found a lot of solutionssuggesting putting setWillNotDraw(false) in the constructor, but that did not solve anything.
GravIO.java File:
package dabawi.gravitas;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class GravIO extends Activity implements Runnable, OnTouchListener {
public final static int clock = 1000;
private GravEngine engine;
private GraView TV;
private SensorManager mSensorManager;
private Sensor mSensor;
private Thread t1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
t1 = new Thread(this);
t1.start();
engine = new GravEngine();
TV = new GraView(this, engine);
setContentView(TV);
TV.setOnTouchListener(this);
TV.setVisibility(View.VISIBLE);
//mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
System.err.println("Starting Engine") ;
run();
}
#Override
public void run() {
while (true) {
try {
System.err.println("Tik") ;
engine.tick();
TV.invalidate();
Thread.sleep(GravIO.clock);
} catch (InterruptedException ex) {
System.err.println("faal");
}
}
}
#Override
public boolean onTouch(View view, MotionEvent event) {
engine.switchGravity();
return true;
}
}
GraView.java File:
package dabawi.gravitas;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
#SuppressLint({ "DrawAllocation", "ViewConstructor" })
public class GraView extends View {
private GravEngine engine;
private int screenWidth, screenHeight, ySpace = 5, xSpace, scale;
private Paint paint;
private int xLoc, yLoc;
private boolean xWall = false, yWall = false;
public GraView(Context context, GravEngine engine) {
super(context);
setWillNotDraw(false);
this.engine = engine;
calcScale(context);
}
#Override // We think the problem is with this method, that it is never called upon.
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
System.err.println("Calculating position");
calcPos();
canvas = new Canvas();
// drawing background
paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
canvas.drawPaint(paint);
drawGame(canvas);
}
private void drawGame(Canvas canvas) {
drawRoom(canvas);
drawPlayer(canvas);
}
#SuppressWarnings("deprecation")
private void calcScale(Context context) {
WindowManager wm = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display d = wm.getDefaultDisplay();
screenWidth = d.getWidth();
screenHeight = d.getHeight();
scale = screenHeight / ySpace;
xSpace = screenWidth / scale;
}
private void calcPos() {
xLoc = (engine.getxPlayer() / GravEngine.roomScaling);
yLoc = (engine.getyPlayer() / GravEngine.roomScaling);
if (xLoc < (xSpace + 1) / 2) {
xLoc = (xSpace + 1) / 2;
xWall = true;
} else if (xLoc > (GravRoom.xSize - ((xSpace + 1) / 2))) {
xLoc = GravRoom.xSize - ((xSpace + 1) / 2);
xWall = true;
} else {
xWall = false;
}
if (yLoc < (ySpace + 1) / 2) {
yLoc = (ySpace + 1) / 2;
yWall = true;
} else if (yLoc > (GravRoom.xSize - ((ySpace + 1) / 2))) {
xLoc = GravRoom.ySize - ((ySpace + 1) / 2);
yWall = true;
} else {
yWall = false;
}
}
private void drawPlayer(Canvas canvas) {
float xPos = engine.getxPlayer() / GravEngine.roomScaling, yPos = engine
.getyPlayer() / GravEngine.roomScaling;
if (xWall) {
}
if (yWall) {
}
paint.setColor(Color.BLUE);
int left = (int) (xPos * scale), top = (int) (yPos * scale);
int right = left + (GravEngine.pxSize / GravEngine.roomScaling) * scale;
int bot = top + (GravEngine.pySize / GravEngine.roomScaling) * scale;
canvas.drawRect(left, top, right, bot, paint);
}
private void drawRoom(Canvas canvas) {
for (int i = 0, x = xLoc - ((xSpace + 1) / 2); i < xSpace + 1; x++, i++) {
for (int j = 0, y = yLoc - ((ySpace + 1) / 2); i < ySpace + 1; y++, j++) {
drawRoomPart(x,y,i,j,canvas);
}
}
}
private void drawRoomPart(int x, int y, int i, int j, Canvas canvas) {
if (x >= 0 && y >= 0 && x < GravRoom.xSize && y < GravRoom.ySize) {
short type = engine.getRoom(engine.getCurrentRoom()).getGridPos(x,y);
if (type != 0) {
drawBlock(canvas, i, x, j, y, type);
}
}
}
private void drawBlock(Canvas canvas, int i, int x, int j, int y, short type) {
int left = i * scale, top = y * scale;
int right = left + scale;
int bot = top + scale;
paint.setColor(colorBlock(type));
canvas.drawRect(left, top, right, bot, paint);
System.err.println("Left" + left + " top: " + top + " right: "
+ right + " bot: " + bot);
}
private int colorBlock(short type) {
if (type == 1) {
return Color.DKGRAY;
} else if (type == 2) {
return Color.CYAN;
} else if (type == 3) {
return Color.GREEN;
} else if (type == 4) {
return Color.RED;
} else {
return Color.MAGENTA;
}
}
// private void imageBlock(short type) {
//
// }
}
Ouch !
Your call to your run method is executed in the UIThread (as the onCreate method), and it's doing an infinite loop in it.
Just add a println after the call and you will see that the UI thread is never released, your application should not even start !
You don't see any changes because you are creating a new canvas and drawing on that. To fix this, remove the line
canvas = new Canvas();
Also, you should initiate your Paint outside of the onDraw method (this is an optimization recommended by the Android documentation).