2014-03-03 5 views
3

두 점 사이에서 스프라이트를 이동하려고합니다.점 사이에서 스프라이트 이동

첫 번째 점은 x=0y=0이고 두 번째 점은 사용자가 화면을 터치 한 지점입니다.

이동하려면 방정식을 사용하여 두 점 (y=ax+b)을 사용하고 싶습니다.

이 메서드는 move()에서 시도했지만 스프라이트는 이동하지 않습니다.

도와주세요.

클래스 디스플레이 :

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; 
} 

} 

클래스 스프라이트 :

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); 
} 

} 

당신을 감사합니다!

답변

2

다음은 두 점 사이에서 오브젝트를 이동하는 데 사용하는 코드 예제입니다.

Math.atan2는 물체가 이동해야하는 궤적 인 θ 각 (-pi에서 + pi)을 계산합니다. Delta는이 업데이트와 마지막 업데이트 사이의 시간이며 velocity는 객체의 원하는 속도입니다. 이들은 모두 곱해질 필요가 있고 새로운 위치를 얻으려면 현재 위치에 추가되어야합니다.

@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; 
} 
3

플레이어를 이동하려면 Bresenham's line algorithm이 필요합니다. 당신은 단지 다음 운동 (전체 라인이 아닌)을 계산할 필요가있을만큼 짧게자를 수 있습니다. 간단하고 쉬운 칼로리 알고리즘입니다.

필요에 맞게 조정해야합니다.

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; 
}