调用函数当图像碰撞时

huangapple go评论61阅读模式
英文:

Call out function when images collide

问题

我不确定如何在两个图像碰撞时调用函数。以下是我的代码:

var arbol;
var poma;
var pomaicono;
var cistell;
var y = [];
var x = [];
var v = [];
let font;

function setup() {
  createCanvas(400, 550);
  arbol = loadImage("ARBOL.png");
  poma = loadImage("POMA.png");
  pomaicono = loadImage("POMA.png");
  cistell = loadImage("CISTELL.png");
  font = loadFont("dogica.otf");
  
  for (i = 0; i < 3; i = i + 1) {
    x[i] = int(random(0, 400));
    y[i] = int(random(0, 400));
  }
  
}

function draw() {
  background(65,105,255);
  image(arbol, 120, -320, 500, 500);
  image(arbol, -200, -320, 500, 500);
  image(cistell, mouseX, 440, 100, 100);
  image(pomaicono, 10,10,30,30);
  
  textFont(font);
  fill(220);
  textSize(25);
  stroke(0);
  strokeWeight(3);
  text("x1",47,37);
  
  for (i = 0; i < 3; i = i + 1) {
    image(poma, x[i], y[i], 50, 50);
    y[i] = y[i] + 3;
    if (y[i] > 440) {
      y[i] = 0;
      x[i] = int(random(0, 400));
    }
  }
  
  var d = dist(poma.x, cistell.x, poma.y, cistell.y);
  
  if (d < poma.x + cistell.x) {
    print("hola");
  }
}

我希望苹果触碰篮子(取决于鼠标指示的位置)时,可以调用一个选项,并在那里我可以放入我想要发生的事情。

英文:

PD: I'M PRETTY NEW AT THIS SO I'M ONLY DOING THE BASICS

I have no idea on how to call out a function when two of my images collide. Here's my code:

var arbol;
var poma;
var pomaicono;
var cistell;
var y = [];
var x = [];
var v = [];
let font;
function setup() {
createCanvas(400, 550);
arbol = loadImage (&quot;ARBOL.png&quot;);
poma = loadImage (&quot;POMA.png&quot;);
pomaicono = loadImage (&quot;POMA.png&quot;);
cistell = loadImage (&quot;CISTELL.png&quot;);
font = loadFont (&quot;dogica.otf&quot;);
for (i = 0; i &lt; 3; i = i + 1) {
x[i] = int(random(0, 400));
y[i] = int(random(0, 400));
}
}
function draw() {
background(65,105,255);
image (arbol, 120, -320, 500, 500);
image (arbol, -200, -320, 500, 500);
image (cistell, mouseX, 440, 100, 100);
image (pomaicono, 10,10,30,30);
textFont(font);
fill(220);
textSize (25);
stroke(0);
strokeWeight(3);
text(&quot;x1&quot;,47,37);
for (i = 0; i &lt; 3; i = i + 1) {
image(poma, x[i], y[i], 50, 50);
y[i] = y[i] + 3;
if (y[i] &gt; 440) {
y[i] = 0;
x[i] = int(random(0, 400));
}
}
var d = dist(poma.x, cistell.x, poma.y, cistell.y);
if (d &lt; poma.x + cistell.x) {
print(&quot;hola&quot;);
}
}

What I wanted to happen is that when an apple touches the basket (depending on the position indicated by the mouse) it could call out an option and there's I could put what I wanted to happen after

答案1

得分: 1

你可以处理圆形。例如,在苹果上放一个看不见的圆,再放一个在篮子上;如果它们相互接触,就调用你想要的函数。

检查圆形之间的碰撞方式是分析它们中心之间的距离是否小于它们半径之和。我建议在相同的交互中进行碰撞检查和移动。

通常,逐像素的碰撞检测对计算机来说成本太高,解决这个问题。

英文:

What you can do is work with circles. For example, an invisible circle on the apple and another on the basket; if they touch each other, you call the function that you wanted.

The way to check collision between circles is to analyse that the distance between their centers is less than the sum of their radii. I recommend to do the collision check in the same interaction as the movement.

Generally, pixel-by-pixel collision check is far too costly to the computer and to solve this problem.

huangapple
  • 本文由 发表于 2023年2月24日 03:28:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549473.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定