繁体   English   中英

C++ 中的圆形与方形碰撞检测和需要响应 | OpenGL。 我的问题就是“我需要将圆圈保持在正方形之外”

[英]Circle vs Square Collision Detection AND NEED TO RESPONSE in C++ | OpenGL. My problem just this “I need to keep the circle outside the square”

我正在研究 2D Circle vs Square Collision Detection。 我做了圆形和方形碰撞检测的测试,没关系,碰撞检测没有问题,只是我需要在圆形与正方形接触时将圆形保持在正方形之外。 我想让圆圈不进入广场,圆圈必须留在广场之外? 我怎样才能做到这一点? 我怎样才能在广场之外保持圆圈?

Picture01我的问题就是这个“当他们相互接触时,我只需要将圆圈保持在正方形之外”我在这个网站上进行了这个碰撞检测测试 => http://jeffreythompson.org/collision-detection/circle- rect.php ,但在本教程中没有碰撞检测的响应

请向我解释写成代码

代码:

#include "shader.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include<vector>
#include<cmath>
#include <math.h>
using namespace std;

GLfloat blueSquareWidth = 0.3f, blueSquareHeight = 0.2f;
std::vector<glm::vec3>      vertices;

float x, y, z, r = 0.1f;
int num_segments = 48;

void DrawCircle()
{
  for (int i = 0; i < num_segments; i++)
 {
     float theta = 2.0f * 3.1415926f * float(i) / float(num_segments);
     //get the current angle
     x = r * cosf(theta);
     y = r * sinf(theta);
     z = 0.0f;

     vertices.push_back(glm::vec3(x, y, z));
    } 
}

  GLfloat blueSquareVertices[] =
  {
    0.0f, blueSquareHeight, 0.0f,
    0.0f, 0.0f, 1.0f,

    0.0f, 0.0f, 0.0f,
    0.0f, 0.0f, 1.0f,

    blueSquareWidth, 0.0f, 0.0f,
    0.0f, 0.0f, 1.0f,

    blueSquareWidth, blueSquareHeight, 0.0f,
    0.0f, 0.0f, 1.0f
 };

GLuint blueSqaureIndices[] =
{
 0, 1, 2,
 0, 2, 3
};

GLfloat rotationTime = 0.0f;

GLuint vbo[2], vao[2], ibo[2];

glm::vec3 blueSquarePosition(0.3f, 0.0f, 0.0f);
glm::vec3 spherePosition(0.660001f, 0.28f, 0.0f);

void inputKeys(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_TRUE)
{
    glfwSetWindowShouldClose(window, true);
}

if (glfwGetKey(window, GLFW_KEY_W) == GLFW_TRUE)
{
    blueSquarePosition.y += 0.001f;
}
else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_TRUE)
{
    blueSquarePosition.y -= 0.001f;
}
else if (glfwGetKey(window, GLFW_KEY_A) == GLFW_TRUE)
{
    blueSquarePosition.x -= 0.001f;
}
else if (glfwGetKey(window, GLFW_KEY_D) == GLFW_TRUE)
{
    blueSquarePosition.x += 0.001f;
}

if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_TRUE)
{
    spherePosition.y += 0.001f;
}
else if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_TRUE)
{
    spherePosition.y -= 0.001f;
}
else if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_TRUE)
{
    spherePosition.x -= 0.001f;
}
else if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_TRUE)
{
    spherePosition.x += 0.001f;
  }
}

bool circleRect(GLFWwindow* window) 
{
// temporary variables to set edges for testing
float testX = spherePosition.x;
float testY = spherePosition.y;

// which edge is closest?
if (spherePosition.x < blueSquarePosition.x)         
{
    testX = blueSquarePosition.x;      // test left edge
}
else if (spherePosition.x > blueSquarePosition.x + blueSquareWidth) 
{
    testX = blueSquarePosition.x + blueSquareWidth;   // right edge
}

if (spherePosition.y < blueSquarePosition.y)         
{
    testY = blueSquarePosition.y;      // top edge
}
else if (spherePosition.y > blueSquarePosition.y + blueSquareHeight) 
{
    testY = blueSquarePosition.y + blueSquareHeight;   // bottom edge
}

// get distance from closest edges
float distX = spherePosition.x - testX;
float distY = spherePosition.y - testY;
float distance = sqrt((distX * distX) + (distY * distY));

// if the distance is less than the radius, collision!
if (distance<= r) 
{
    if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS)
    {
        // WHAT I NEED TO DO HERE
    }

    if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS)
    {
        // WHAT I NEED TO DO HERE
    }

    if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS)
    {
        // WHAT I NEED TO DO HERE
    }

    if (glfwGetKey(window, GLFW_KEY_BOTTOM) == GLFW_PRESS)
    {
        // WHAT I NEED TO DO HERE
    }

    return 1;
}

  return 0;
}



int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

GLFWwindow* window = glfwCreateWindow(800, 800, "Game Engine", NULL, 
 NULL);

if (!window)
{
    cout << "Failed to create a window" << endl;
    glfwTerminate();
    return -1;
}

glfwMakeContextCurrent(window);

if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
    cout << "Failed to initialize GLAD" << endl;
    return -1;
}

DrawCircle();

glGenBuffers(1, &vbo[0]);
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(blueSquareVertices), blueSquareVertices, GL_STATIC_DRAW);

glGenVertexArrays(1, &vao[0]);
glBindVertexArray(vao[0]);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, 0);

glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, (void*)(sizeof(float) * 3));

glGenBuffers(1, &ibo[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo[0]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(blueSqaureIndices), blueSqaureIndices, GL_STATIC_DRAW);

glGenBuffers(1, &vbo[1]);
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * vertices.size(), &vertices[0], GL_STATIC_DRAW);

glGenVertexArrays(1, &vao[1]);
glBindVertexArray(vao[1]);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);

Shader shader("vs2.glsl", "fs2.glsl");
shader.useShader();

while (!glfwWindowShouldClose(window))
{
    inputKeys(window);
    circleRect(window);

    glClearColor(0.6f, 0.8f, 0.8f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glm::mat4 blueSquareTransform(1.0f);
    blueSquareTransform = glm::translate(blueSquareTransform, 
    glm::vec3(blueSquarePosition));

    GLuint blueSquareTransformUniform = 
    glGetUniformLocation(shader.shader, "transform");
    glUniformMatrix4fv(blueSquareTransformUniform, 1, GL_FALSE, 
    glm::value_ptr(blueSquareTransform));

    glBindVertexArray(vao[0]);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

    glm::mat4 redSquareTransform(1.0f);
    redSquareTransform = glm::translate(redSquareTransform, 
    glm::vec3(spherePosition));


    GLuint redSquareTransformUniform = glGetUniformLocation(shader.shader, 
    "transform");
    glUniformMatrix4fv(redSquareTransformUniform, 1, GL_FALSE, 
    glm::value_ptr(redSquareTransform));

    glBindVertexArray(vao[1]);
    glDrawArrays(GL_TRIANGLE_FAN, 0, vertices.size());

    glfwSwapBuffers(window);
    glfwPollEvents();
   }
}

为了执行碰撞检测和防止渗透,您需要

以前 position

当前 position

第一个问题是,在“我需要在这里做什么”这一行,您似乎已经错过了之前的 position。 两者兼得后,您需要执行以下操作:

如果当前 position 发生碰撞,则沿从当前到发生切换的前一个 position 的线进行二分搜索。

您需要能够对 position 进行线性插值,其中 0 是先前的,1 是当前的。

然后,如果你想做得更好,而不是仅仅OK,然后计算碰撞,更新方向并重复剩余长度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM