尝试使电弧与图像碰撞时遇到很多麻烦。我发现大多数教程都是圆形,正方形而不是弧形的。我想做一场比赛,将使用弧线作为目标的球保持在圆圈内,但我不知道发生碰撞的方法。请有人帮我很新。
var objectX = new Array(1);
var objectY = new Array(1);
var objectDX = new Array(1);
var objectDY = new Array(1);
var objectSize = 100;
var objectDirectionX = undefined;
var objectDiralX = undefined;
var objectDirectionY = undefined;
var objectDiralY = undefined;
var thenarrowing = undefined;
var imagse = new Array(1);
var angle = 1;
var bat = 90;
//var arcX = windowWidth-50;
//var arcY = windowHeight-50;
function preload() {
for (var i = 0; i < 4; i++) {
imagse[i] = loadImage("ball.png");
}
}
function setup() {
background("blue")
createCanvas(windowWidth, windowHeight);
imageMode(CENTER)
for (var i = 0; i < objectX.length; i++) {
//location
objectX[i] = windowWidth / 2;
objectY[i] = windowHeight / 2;
//random direction
objectDirectionX = Math.floor(random(0, 2));
if (objectDirectionX == 1) {
objectDiralX = -1;
} else {
objectDiralX = 1;
}
objectDirectionY = Math.floor(random(0, 2));
if (objectDirectionY == 1) {
objectDiralY = -1;
} else {
objectDiralY = 1;
}
//direction and speeds
objectDX[i] = random(8 * objectDiralX, 12 * objectDiralY);
objectDY[i] = random(8 * objectDiralX, 12 * objectDiralY);
}
}
function draw() {
background("grey");
ellipseMode(CENTER);
fill(0, 255, 0, 0.25);
strokeWeight(15);
stroke(0, 0, 255);
ellipse(windowWidth / 2, windowHeight / 2, windowWidth - 30, windowHeight - 30);
for (var i = 0; i < objectX.length; i++) {
if (imagse != 'underfined') {
image(imagse[i], objectX[i], objectY[i], objectSize, objectSize); // the pic inside arrays
objectX[i] += objectDX[i];
objectY[i] += objectDY[i];
if (objectX[i] > width - objectSize / 2 || objectX[i] < objectSize / 2) { //bounding off the walls right side || left side
objectDX[i] = -objectDX[i]; //changing direction
}
if (objectY[i] > height - objectSize / 2 || objectY[i] < objectSize / 2) {
objectDY[i] = -objectDY[i];
}
//player
angleMode(DEGREES);
stroke(20)
arc(windowWidth / 2, windowHeight / 2, windowWidth - 50, windowHeight - 50, bat - 5, bat + 5)
if (keyIsDown(LEFT_ARROW)) {
bat = bat + 5
}
if (keyIsDown(RIGHT_ARROW)) {
bat = bat - 5
}
//if (dist(objectX[i],objectY[i] )){
// objectDX[i] = -objectDX[i];
// }
}
}
}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>secondtry</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.1/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.1/addons/p5.dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.1/addons/p5.sound.js"></script>
<script src="codingAssignment.js"></script>
</head>
<body>
</body>
</html>