Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
Círculo dentro de una resolución de colisión de círculo en p5.js

Estoy creando un programa en el que un círculo rebota alrededor del interior de un círculo más grande. Descubrí cómo calcular cuándo y dónde chocan los círculos, pero no cómo calcular el vector resultante en el círculo de rebote que se debe aplicar. CUALQUIER ayuda sería muy apreciada.

código a continuación o en el editor web .

 let pos, vel, acc; function setup() { createCanvas(400, 400); pos = createVector(width / 2 + 20, height / 2 - 10); //inital position of ball vel = createVector(0, 0.5); //inital velocity acc = createVector(0.1, 0.1); //inital acc } function draw() { background(220); fill(255) //vel.add(acc); //acceleration disabled due to testing pos.add(vel); //adds velocity to the position ellipse(pos.x, pos.y, 20); //draws the ball noFill(); circle(width / 2, height / 2, 100); //draws the main ball (border) if (dist(pos.x, pos.y, width / 2, height / 2) >= 50 - 10) { //if the ball hits the circle theta = atan2(pos.y - height / 2, pos.x - width / 2); pX = 50 * cos(theta) + width / 2; pY = height / 2 + 50 * sin(theta); POI = createVector(pX, pY); //point of intersection vector fill(255, 0, 0); circle(pX, pY, 2); //a vector that when applied, *should* bounce the circle back oppositeForceVector = p5.Vector.sub(POI, (width / 2, height / 2)); vel.add(oppositeForceVector); vel.limit(1); } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

 let pos, vel, acc; function setup() { createCanvas(400, 400); pos = createVector(width / 2 + 20, height / 2 - 10); //inital position of ball vel = createVector(0, 0.5); //inital velocity acc = createVector(0, 0.1); //inital acc } function draw() { background(220); fill(255) vel.add(acc); //acceleration disabled due to testing pos.add(vel); //adds velocity to the position ellipse(pos.x, pos.y, 20); //draws the ball noFill(); circle(width / 2, height / 2, 100); //draws the main ball (border) if (dist(pos.x, pos.y, width / 2, height / 2) >= 50 - 10) { //if the ball hits the circle theta = atan2(pos.y - height / 2, pos.x - width / 2); // Make our POI coordinates relative to the center of the circle let pX = 50 * cos(theta); let pY = 50 * sin(theta); let POI = createVector(pX, pY); //point of intersection vector // Find the tangent of the circle at the point of intersection let tangent = POI.copy().rotate(PI / 2); // Check the angle between or velocity, and the vector towards the POI let angle = vel.angleBetween(POI); // Don't "collide" if the circle is already moving back towards the center if (abs(angle) <= PI / 2) { // When we collide, or velocity vector should be mirrored, if we were pointing // outwards at a 45 degree angle, we should now be pointed inward at a 45 // degree angle vel.rotate(vel.angleBetween(tangent) * 2); } fill(255, 0, 0); // when we draw the point of collision, offset from the center of the circle circle(pX + width / 2, pY + height / 2, 2); } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda