How do I show ball trajectory with wall bounces?

Not favoritedFavorited Favorited 0 favourites
  • 8 posts
From the Asset Store
Enjoy the powerful simulation with accurate ball physics, build with realistic ball spinning animations
  • Hey guys,

    I'm trying to create a physics system that displays dashed lines to preview the ball's path before shooting. When the path hits a wall (left or right), I want it to reflect realistically, and the dashed lines should show that reflection.

    When I shoot the ball, I want the velocityX and velocityY to match this predicted path exactly.

    I've looked around for examples but haven’t had any luck so far.

    Any help would be greatly appreciated!

    I've attached an image example for reference.

    Thanks!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • You can make a shooting mechanic that will shoot lines.

    1) Add Bullet behaviour to the line

    2) Check "bounce of wall" in the setting.

    3) Shoot the line from the red ball every X time

    4) Delete it after collision with a solid in X time

    5) ???

    6) Profit

  • I'm talking about physics only, not using any bullet behavior.

  • You can make a shooting mechanic that will shoot lines.

    1) Add Bullet Physics behaviour to the line

    ...

    This is a good and clever solution.

  • Getting the exact path isn’t as simple as you’d think. Objects are moved in steps with the timestep which can vary so when a collision is detected it will overlap by a varying amount. You can test that. Repeatedly launch a ball from the same spot to see the path. As long as the framerate is consistent the path will mostly be the same but the more dt varies the more the paths will deviate after a bounce.

    Anyways, maybe a close path would be good enough.

    One way to see the path of an object is to use a loop to simulate many frames at once. Basically use a clone object of the ball. Set the clone to the same position and velocity of the ball, then move the clone with a loop. The bounce logic can be involved if you do it yourself, but if you used the bullet behavior it would be simple.

    Every tick
    — clone: set position to ball
    — clone: set vx to ball.vx
    — clone: set vy to ball.vy
    
    Repeat 60*3 times
    — clone: set position to self.x+self.vx/60, self.y+self.vy*dt
    — clone: overlaps wall
    — — clone: bounce off wall

    Manually finding the collision normal for a bounce is more involved and a different question.

    Anyways, for the dashed line you only have the consider the positions where the ball bounces and stretch a tiledbg of the dashes between them (or maybe the drawing canvas lets you draw dashed lines)

    Another idea instead of using a loop to simulate motion in steps is to do a kind of shapecast (kind of like a ray cast but considering the shape of the object too). It’s an algorithm that would need to be implemented, but the result would be exact collision detection when the ball would just touch the wall. The con with that is it wouldn’t match how the ball actually moves, as mentioned in the first paragraph.

    Anyways, if very exact is required I’d also change how you move the ball. Basically, shapecast the ball to the walls to get an exact position and time of collision. Calculate the normal and reflect the velocity along that. Then repeatedly shapecast again. You’ll end up with a polyline path. Then it becomes just a matter of moving along a path which you can do exactly.

    But again, you probably want a simpler solution that’s good enough. The quoted pseudo code above is probably the simplest. Especially if you utilize the bullet behavior’s bounce action. However you will need to change from velocity from vx,vy to speed and angle of motion.

    Anyways, just some thoughts.

  • I understand. Thank you all for your answers.

  • Also, to avoid inaccuracies, instead of trying to predict the exact trajectory of the ball, perhaps you could draw the trajectory first, and then force the ball to follow that trajectory.

  • I will give a try, thanks!

Jump to:
Active Users
There are 0 visitors browsing this topic (0 users and 0 guests)