Im thinking about making a simple game with a ball / projectile behavior similar to Pong or breakout, where the ball Is constantly moving and being deflected correctly of the walls and paddle surfaces In the opposite direction of its incoming direction Like a bouncing ball.
Whats the best way to approach this ? as I think using physics is not the best approach as I would not want to be using gravity as game will be based in 3d world using polygons and not sprite based
Depends if you want that 8bit nostalgia feel or not.
For 8 bit thingy:
if you hit horizontal surface (going up or down) flip vertical speed
if you hit some vertical surface (ball going left or right) flip horizontal speed.
That is all.
However do some fun projects with vector math, seems you need it.
PS for clarity:
surface like this ------------------- : vy=-vy
surface like thins | vx=-vx
and every tick (compensate for delta tick): pos_x=pos+vx posy=posy+vy
to compensate for delta either vx=vx*delta or vx=vx/delta (dont remember which one is right, google it).
I know how to do some vector math , But as Im at work I wanted to get a head start on research before I get home
I was wondering if it would be done with a hit surface normal and inverting the Incoming vector/velocity.
Hit surface normal, then normalize it (make sure vector is 1 unit long), then DOT product between normal hit vector and up vector and dot between right and normal hit vector.
Dot product is cosinus between vectors. This way you determine if normal hit vector is from horizontal or vertical surface.
Oh and use “Pure functions” for repeated code pieces here (like dot product , then normalize then compare fi its up/down or left/right). When you code you see duplicated code, its perfect for pure function
Or, if you decide to go down the Physics route, disable gravity, constrain to a plane and counter friction & impact energy loss with a physical material. Great potential for uncanny behaviours but can be fiddly.
I have made 2 ball actors one with the movement component like above and one with a projectile component . But I have the same issue on both where the velocity of the ball is not constant and keep slowing down to a stop.
I tried adding add impulse on each hit but its giving erratic results
I’ve actually been in production of a very similar game inspired by pong for over a year. I can tell you that after the initial testing I did a while back I too came to the conclusion that using default simulated physics just won’t work. What ended up happening was that the simulation would have various errors during bouncing and it cannot be counted on. Also trying to implement other types of movement while using simulation also became problematic. what I settled on is a manual approach instead of simulating physics I use kinematic physics and manually set the balls velocity and speed every frame (via AddLocalOffset()). I also use the OnHit events to detect the surface normal of the surface that was hit and then calculated a reflection vector that determines the desired output trajectory. hope this helps.
Using a floating movement comp means applying an input vector every frame (called TraverseDir there) multiplied by a Speed coefficient. So, technically, nothing can slow you down / speed you up since you override it anyway. Or perhaps this needs adjusting:
Velocity values for the above. Needs testing for more than a couple of mins, surely, though. The result seem OK at a glance but who knows how far you need to take it and what you’ll run into:
From my experience, the biggest gripe with this method is handling inter object collisions as movement comps don’t like other movement comps. It needs a separate calculation on impact.
CCD (optional, depending on top vel), position & velocity solvers may need adjusting, a physical material is a must. The thing is that the latter opens up so many gameplay possibilities that it’s hard to ignore what it offers.
My experiences with using the projectile movement component were OK, too; but leaning towards mixed. I don’t have a thread or example about that.
At least the projectiles bounce nicely off one another with this:
Now, if @Richmar1 adds their method, there’d be a nice set of ideas here. Good luck!
I’m super impressed with the amount of work You put in to your answers. I will Of coarse keep working on the different ball actors each with there own solution Until I can find one that works. My concern Like Richmar1 said is that Physics Might be unreliable for my goal As the players Is hitting the ball around In VR with a racket and it will need to feel right.
weirdly enough I cant even make my actor with the movement component move Not sure why . I have copied all your settings.
Thank for the info, and confirming your findings on the Potential physics issues.
I sure would like to take a look at that BP if the offers is still there. as This is a new area for me as Im mostly working on AI and VR Physics interactions.
never tried to do balls before
There’s no physics sim or gravity. Pawns ignore each other otherwise they get strangely sticky - not sure why; every time a I look at the movement component, it confuses me and a third of the code is deprecated.
That example is a simple BP_Ball Actor that inherits from AActor, nothing more. It’s not a pawn or anything like that so it doesn’t carry any overhead that pawns or projectiles do. to handle inter-object collision interactions, i simply created a new Collision Profile in projects settings called “Ball”, and set my BP_Ball to that. You can then specify what sort of actors the ball is allowed to interact with. For my game, balls cannot interact with other balls or power-ups that float around. Hope this helps