首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使物体反弹

使物体反弹
EN

Stack Overflow用户
提问于 2013-06-03 14:56:47
回答 1查看 973关注 0票数 1

我试着在长方形上做一个启示录。我用计时器在x和y方向移动启示录。我的想法是创建一个if语句,以查看“启示”的坐标是否与矩形的坐标相匹配。

以下是我到目前为止编写的代码:

代码语言:javascript
复制
  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. 有什么更简单的方法让启示录“反弹”吗?
  2. 这仅仅是启示录所遵循的曲线的数学问题吗?

我知道这个问题可能有些模糊或抽象,但任何帮助都是见仁见智的。如果你想让我在某些方面更清楚,请告诉我!谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-03 15:03:00

一个简单的方法,使椭圆反弹的边缘是检查它的边缘点与边界,然后只是反转正确的方向。所以,如果你能原谅伪代码的话,这样的东西应该可以用。

代码语言:javascript
复制
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

这是一个相当简单的模拟,但它应该给你一个如何进步和发展一个更复杂的模型的想法。希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16899710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档