gpt4 book ai didi

java - 移动和非移动物体之间的碰撞检测

转载 作者:行者123 更新时间:2023-11-30 03:37:40 25 4
gpt4 key购买 nike

我正在开发一个简单的处理应用程序,每当锤子敲击不移动的形状时,它就会产生声音效果。然而,我在尝试让代码检测锤子对象与形状对象碰撞时遇到了困难,并且已经开始诉诸不专业的解决方法,如“测试”下注释的代码块中所示。任何有关创建此问题解决方案的帮助将不胜感激

import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress myRemoteLocation;

float bx;
float by;
int boxSizeX = 160;
int boxSizeY = 30;
boolean overBox = true;
boolean locked = false;
float xOffset = 0.0;
float yOffset = 0.0;
float angle = 4.70;

BeatBall b1 = new BeatBall(210,425,60, 1);

void setup()
{
size(800, 600);
smooth();
frameRate(120);
bx = width/2.0;
by = height/2.0;

oscP5 = new OscP5(this,12001);

/* myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
* an ip address and a port number. myRemoteLocation is used as parameter in
* oscP5.send() when sending osc packets to another computer, device,
* application. usage see below. for testing purposes the listening port
* and the port of the remote location address are the same, hence you will
* send messages back to this sketch.
*/
myRemoteLocation = new NetAddress("127.0.0.1",12000);
}

void draw()
{
background(0);

pushMatrix();
translate(400, 425);
rotate(angle);
fill(222,223,255);
Hammer h = new Hammer(135, -67, boxSizeY+25, boxSizeX-25, 1);
h.displayHammer();
rect(-25, -15, boxSizeX, boxSizeY);
popMatrix();

b1.displayBall();

//Testing
if(angle < -2.6561418 && angle > -3.043227)
{
background(120);
b1.collide();
}

println(angle);
}



void mousePressed()
{
xOffset = mouseX-bx;
yOffset = mouseY-by;
}

void mouseDragged()
{
bx = mouseX-xOffset;
by = mouseY-yOffset;
angle = atan2(mouseY - 400, mouseX - 400);
}

//BEATBALL CLASS

class BeatBall {
float x, y;
float diameter;
float vx = 0;
float vy = 0;
int id;

BeatBall(float xin, float yin, float din, int idin) {
x = xin;
y = yin;
diameter = din;
id = idin;
}

void collide()
{

/* Collision Example
float dx = Hammer.x - x;
float dy = Hammer.y - y;
float distance = sqrt(dx*dx + dy*dy);
float minDist = others[i].diameter/2 + diameter/2;
if (distance < minDist)
{
float angle = atan2(dy, dx);
float targetX = x + cos(angle) * minDist;
float targetY = y + sin(angle) * minDist;
float ax = (targetX - others[i].x) * spring;
float ay = (targetY - others[i].y) * spring;
vx -= ax;
vy -= ay;
others[i].vx += ax;
others[i].vy += ay;
*/

OscMessage myMessage = new OscMessage("/bubble");
print(diameter + " ");
myMessage.add( 1/(diameter*diameter) * 1000000); /* add an int to the osc message */

/* send the message */
oscP5.send(myMessage, myRemoteLocation);

//}
}

void displayBall()
{
fill(191,89,0);
ellipse(x, y, diameter, diameter);
}


}

//HAMMER CLASS

class Hammer {
float x, y;
float sizeX, sizeY;
float vx = 0;
float vy = 0;
int id;

Hammer(float xin, float yin, float sxin, float syin, int idin) {
x = xin;
y = yin;
sizeX = sxin;
sizeY = syin;
id = idin;
}

void displayHammer()
{
fill(222,223,255);
rect(x, y, sizeX, sizeY);
}

}

最佳答案

我已经构建了 a suite of collision detection functions for Processing这可能会有所帮助。

如果您可以简化事物并将物体视为圆,则可以使用毕达哥拉斯定理来检查它们的距离。 (根据要求更新为函数。)

// variables for your objects - where are they and how big?
float ballX, ballY;
float ballRadius;
float hammerX, hammerY;
float hammerRadius;

void setup() {

// check for a collision
boolean hit = ballBallCollision(ballX, ballY, ballRadius, hammerX, hammerY, hammerRadius);
if (hit) {
// hit!
}
else {
// not :(
}
}

// a function to check for ball-ball collision
boolean ballBallCollision(float ballX, float ballY, float ballRadius, float hammerX, float hammerY, float hammerRadius) {
// calculate distance between the objects using the Pythagorean Theorem
float xDist = hammerX - ballX;
float xDist = hammerY - ballY;
float dist = sqrt( (xDist*xDist) + (yDist*yDist) );

if (dist < ballRadius + hammerRadius) {
return true;
}
return false;
}

关于java - 移动和非移动物体之间的碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27471063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com