Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Robster | |
Old Version | Published before Godot 3 was released. |
Hi all,
Still making pong
In this thread I learned about finding where my ball hits my bat and normalising the result. All good fun.
Now I’m trying to get the ball to fly off at a different angle.
I have this code (thank you /u/avencherus) that is being called every frame (when there’s a collision):
var velocity = Vector2(cos(bounceAngle), -sin(bounceAngle)) * game.ballSpeed
If I try and use it though like so:
var velocity = Vector2(cos(bounceAngle), -sin(bounceAngle)) * game.ballSpeed
set_linear_velocity(velocity)
The ball bounces rapidly against the bat when it hits it. It’s crazy times but not what I’m after.
I had a research and the docs say:
Set the body angular velocity. Can be used sporadically, but DON’T SET THIS IN EVERY FRAME, because physics may be running in another thread and definitely runs at a different granularity.
I’m wondering if anyone has some advice on how to approach this?
EDIT: It also says Use _integrate_forces as your process loop if you want to have precise control of the body state.
but I got completely lost there. It’s as if _integrate_forces just happens all the time without being called, so I am not sure how to control that? Is it treated like a _fixed_process of sorts?
It’s not very clear what kind of result you want. Did you want a collision response based on elastic collisions? A simple reflection of velocity? There’s all kinds of ways to “bounce”.
A note about the docs remark. Angular velocity deals with the rotation or spin of an object. Think of fan blades spinning, that’s angular velocity. I don’t think that’s what you’re looking for. I imagine in your game of pong you’re not too interested in if the ball is spinning, unless you want to model some kind of ball spin from friction.
avencherus | 2017-02-24 04:27
Sorry I copy / pasted the wrong part. I was going for this from set_linear_velocity
:
Set the body linear velocity. Can be used sporadically, but DON’T SET THIS IN EVERY FRAME, because physics may be running in another thread and definitely runs at a different granularity
I’m after an effect where the ball hits the bat and bounces off at an angle that I determine based on the normalised position (-1 to 1). I’ve set my MaximumAngle to this:
const MAXBOUNCEANGLE = deg2rad(75)
… and then use it as such:
var bounceAngle = normalisedhitLocationY * MAXBOUNCEANGLE;
Essentially:
- hit the top of the bat, big angle
- hit a bit below the top, slightly lower angle
- etc
- hit middle of bat, ball bounces off on a flat horizontal angle
- etc to bottom of bat where the angle is large again
I was trying to play with _integrate_forces() but anything in there happened all the time. Then it started to dawn on me, is _integrate_forces() to be used INSTEAD of _fixed_process(delta)? So I’d put all my logic, key / mouse controls, etc into _integrate_forces() rather than _fixed_process(delta)? OR, can they run next to each other / at the same time? I’m a little confused about it obviously.
Robster | 2017-02-24 04:46
K so, an arbitrary angle based on where it hits.
I’ll give an example, but the other questions I’d recommend splitting that off into their own questions.
avencherus | 2017-02-24 04:50