如何使球顺利滑出转角? [英] How to make the ball roll smoothly off the corner?

查看:89
本文介绍了如何使球顺利滑出转角?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此程序中,当鼠标在另一侧时,矩形应将球挡住。当球和鼠标位于相邻侧时,球应从角部滚落。问题是这不会发生,球被卡在角落。这是完整的程序:

In this program, the rectangle should block the ball when the mouse is on the opposite side. When the ball and the mouse are on adjacent sides, the ball should roll off the corner. The problem is that this doesn't happens, the ball is stucked on the corner. Here is the complete program:

PVector p = new PVector(100, 100); //position
PVector v = new PVector(0, 0); // velocity

void setup()
{
  size(600, 600);
  frameRate(120);
}

void draw()
{
  background(0);
  rect(250, 250, 200, 100);
  ellipse(p.x, p.y, 20, 20);
  v.setMag(2.5);
  p.add(v);

  if (p.x + 10 >= 250 && p.x - 10 <= 450 && p.y + 10 >= 250 && p.y - 10 <= 350) // ball is inside box
  {
    if (p.y  <= 250 || p.y  >= 350) // ball came from above 
    {
      v.y = -v.y;
    } 
    if (p.x <= 250 || p.x >= 450) // ball came from sides
    {
      v.x = -v.x;
    }
  } 
  else // move ball
  { 
    PVector dir = new PVector(mouseX, mouseY);
    v = PVector.sub(dir, p);
  }
}

我试图通过检查是否有拐角来解决此问题被击中,但球仍然被卡在角落。

I tried to fix this by checking if a corner has been hit but the ball still gets stuck on the corner.

PVector p = new PVector(100, 100); // Pisition
PVector v = new PVector(0, 0); // Velocity

void setup()
{
  size(600, 600);
  frameRate(120);
}

void draw()
{
  background(0);
  rect(250, 250, 200, 100);
  ellipse(p.x, p.y, 20, 20);
  v.setMag(2.5);
  p.add(v);

  if (dist(p.x, p.y, 250, 250)<10) // corner has been hit
  {
    p = new PVector(239, 239); // move ball away from corner
  } 
  else if (dist(p.x, p.y, 450, 250)<10)
  {
    p = new PVector(461, 239);
  } 
  else if (dist(p.x, p.y, 450, 350)<10)
  {
    p = new PVector(461, 361);
  } 
  else if (dist(p.x, p.y, 250, 350)<10)
  {
    p = new PVector(239, 361);
  } 
  else if (p.x + 10 >= 250 && p.x - 10 <= 450 && p.y + 10 >= 250 && p.y - 10 <= 350) // ball is inside box
  {
    if (p.y  <= 250 || p.y  >= 350) // ball came from above 
    {
      v.y = -v.y;
    } 
    if (p.x <= 250 || p.x >= 450) // ball came from sides
    {
      v.x = -v.x;
    }
  } 
  else // move ball towards mouse
  { 
    PVector dir = new PVector(mouseX, mouseY);
    v = PVector.sub(dir, p);
  }
}

有没有办法使球顺滑滚落角落?任何帮助表示赞赏。谢谢。

Is there a way to make the ball roll smoothly off the corner? Any help is appreciated. Thanks.

推荐答案

我已经修改了草图以解决您的问题。诀窍是在每个角上添加一个小间隙,以使球可以越过直角。

I've modified your sketch in order to solve your problem. The trick is to add a little gap at each corner such that the ball can cross the right angle.

PVector p = new PVector(100, 100); //Position
PVector v = new PVector(0, 0);     //Velocity
PVector dir;                       //Direction
int sizeX = 600, sizeY = 600;      //Window size
int rectX = 250, rectY = 250;      //Rectangle position
int rectW = 200, rectH = 100;      //Rectangle size
int ballR = 10;                    //Ball radius
int gap = 2;                       //Corner gap

void setup(){
  size(sizeX, sizeY);
  frameRate(120);
}

void draw(){
  background(0);
  rect(rectX, rectY, rectW, rectH);
  dir = new PVector(mouseX, mouseY);
  v = PVector.sub(dir, p);
  if((v.x>0 && p.x+ballR > rectX         && p.x+ballR < rectX + rectW &&
               p.y+ballR > rectY + gap   && p.y-ballR < rectY + rectH - gap)
   ||(v.x<0 && p.x-ballR < rectX + rectW && p.x-ballR > rectX &&
               p.y+ballR > rectY + gap   && p.y-ballR < rectY + rectH - gap)){
      v.x = -v.x;
  } 
  if((v.y>0 && p.y+ballR > rectY         && p.y+ballR < rectY + rectH && 
               p.x+ballR > rectX + gap   && p.x-ballR < rectX + rectW - gap)
   ||(v.y<0 && p.y-ballR < rectY + rectH && p.y-ballR > rectY &&
               p.x+ballR > rectX + gap   && p.x-ballR < rectX + rectW - gap)){
      v.y = -v.y;
  }
  v.setMag(2.5);
  p.add(v);
  ellipse(p.x, p.y, 2*ballR, 2*ballR);
}

这篇关于如何使球顺利滑出转角?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆