Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: I need help with this  (Read 1243 times)

0 Members and 1 Guest are viewing this topic.

Yosef

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
I need help with this
« on: May 22, 2024, 08:03:37 pm »
I'm developing a pool game using SFML and Box2D, and I'm currently facing an issue with creating the cue ball outline when the player aims. I have three rays that are cast: two black ones and one white one. How can I determine the position of the outline of the cue ball using the shortest of these rays as the player aims. I am using the ray cast function of box2d.

I have attached picture of the outline for further reference


« Last Edit: May 23, 2024, 11:32:22 pm by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11008
    • View Profile
    • development blog
    • Email
Re: I need help with this
« Reply #1 on: May 23, 2024, 11:36:42 pm »
I'm not quite sure what you mean with "determine the position of the outline of the cue ball". Do you mean the shortest ray to the border of the cue ball? Wouldn't we also need to know where this is casted from to answer the question?

Maybe there's also an entirely different question hidden underneath. Can you describe what you're trying to solve currently?

Also might be useful if you could provide some code.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Yosef

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: I need help with this
« Reply #2 on: May 24, 2024, 07:52:47 pm »
I want to perform a circle cast for the cue ball as the player aims, stopping when it encounters an obstruction like other pool balls or the table's sides. Since Box2D doesn't support circle casting yet, how can I achieve this?
« Last Edit: May 24, 2024, 08:08:28 pm by Yosef »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11008
    • View Profile
    • development blog
    • Email
Re: I need help with this
« Reply #3 on: May 24, 2024, 10:43:46 pm »
For the table's side, you'd have to provide Box2D a table size minus the radius of the cue ball on each side, then do a line cast. If the line hits the wall, you know that ball would hit the side.

For ball to ball, it's a bit trickier, since balls can hit a different angles. Not sure what options Box2D provides here, but circle to circle collision check is really simple. If the distance of points is smaller than the combined radi of the two circles, you have a collision.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: I need help with this
« Reply #4 on: May 25, 2024, 03:10:16 am »
For ball to ball collision, you can increase the ball radius by the cue ball radius, now the cue ball can be treated as a single point and use raycasts.
But you don't want to actually grow all of the balls by that amount, because they would then collide with each other early, so you need a temporary larger shape.

I'm just guessing on the code off the top of my head, but I think you could do it like this:
Loop over every ball (not including the cue ball)
   Make a temporary b2CircleShape with the same position as the current ball and larger radius
   Do a shape raycast from the cueball position
End of Loop
Make 4 b2EdgeShape for the table sides that are moved inwards by the cue ball radius as eXpl0it3r said
Do shape raycasts against each edge.
Find which of the above raycasts had a hit closest to the cueball

https://box2d.org/documentation/classb2_circle_shape.html#a442e847b9fc3d1344b02b48d490eb0c6