2013-06-03 1 views
1

사각형에 엘보우 바운스를 만들려고합니다. 나는 타원을 x와 y 방향으로 움직이기 위해 타이머를 사용하고있다. 내 생각은 elipse의 좌표가 사각형의 좌표와 일치하는지 확인하기 위해 if 문을 작성하는 것이 었습니다. 내가 문제가있는 것을 알고개체 바운스하기

public partial class Form1 : Form 
    {  
    Class1 square = new Class1(); 
    public int before; 
    public int after; 
    public int c; 

    public Form1() 
    {  
     InitializeComponent(); 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     after = 590 - (b * b)/100; 
     before = 100 + (a * a)/100; 
     c = a + b; 
     Graphics g = e.Graphics; 
     SolidBrush Brush = new SolidBrush(Color.White); 
     g.FillEllipse(Brush, a, before, 10, 10);    
     square.Draw(g); 


     if (k >= square.y && a >= square.x && a <= square.x + 40) 
     { 
      a=c; 
      before= after; 
      timer1.Start(); 
      timer2.Stop(); 
     } 
     else if (k >= square.y + 10) 
     { 
      timer2.Stop(); 
      MessageBox.Show("You lost");     
     }   
    }   

    protected override void OnMouseMove(MouseEventArgs e) 
    { 
     base.OnMouseMove(e); 
     square.x = e.X;   
     Cursor.Hide(); 
    } 

    public int a = 0; 
    public int b = 0; 

    public void timer2_Tick(object sender, EventArgs e) 
    { 
     a += 1; 
     Invalidate(); 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     b += 1; 
     Invalidate(); 
    } 
} 

: 여기

는 내가 지금까지 쓴 코드입니다. 그리고 몇 가지 질문이 있습니다.

  1. 더 쉬운 방법으로 엘립이 "바운스 (bounce)"할 수 있습니까?
  2. 문제는 엘보가 따르는 곡선의 수학에만 문제가 있습니까?

나는이 질문이 다소 불확실하거나 추상적 일 수 있지만 어떤 도움이 필요한지 알고 있습니다. 그리고 내가 어떤면에서 내가 더 분명 해지 길 원한다면 알려주세요! 감사합니다

답변

0

하는 간단한 방법은 가장자리의 타원 바운스가 경계에 대해 가장자리 점을 확인하는 것입니다 확인한 다음 바로 올바른 방향을 반전합니다. 그래서, 당신은이 꽤 간단한 시뮬레이션 의사 코드

loop used to animate the ellipse 
before moving, check the position: 

if right-most position == right wall 
    invert x velocity 
if left-most position == left wall 
    invert x velocity 
if top-most position == top wall 
    invert y velocity 
if bottom-most position == bottom wall 
    invert y velocity 

move ellipse to next position 

를 용서 것이다 경우이 같은이 작동해야하지만, 그것은 당신에게 진행하고보다 정교한 모델을 개발하는 방법에 대한 아이디어를 줄 것이다. 희망이 도움이!

관련 문제