[英]Can't figure out the maths behind a quadratic curve needed for a slingshot
Would like to apologise if this is too maths based.如果这太基于数学,我想道歉。
I have a project where I have to create an AngryBirds game with our teacher's custom game engine, however I am stuck on the maths behind the slingshot.我有一个项目,我必须使用我们老师的自定义游戏引擎创建一个 AngryBirds 游戏,但是我被困在弹弓背后的数学上。 We are not allowed to use any standard libraries.
我们不允许使用任何标准库。 The top left is 0, 0 and the y-axis increases when you go down.
go 向下时,左上角为 0、0 和 y 轴增加。 The total width of the window is 1280 pixels and the height is 720 pixels.
window 的总宽度为 1280 像素,高度为 720 像素。 I am trying to make the bird travel further as you pull the bird further left from the sling origin which is 257, 524. I used the y value from release at the start so that the bird doesn't go somewhere else in the y-axis straight after letting go.
我试图让鸟走得更远,因为你将鸟从吊索原点向左拉,即 257、524。我在开始时使用了释放的 y 值,这样鸟就不会 go 在 y 的其他地方 -轴直后让 go。 Currently the bird increases in the y-axis, which is to be expected given that is exactly what my code does.
目前这只鸟在 y 轴上增加,这是可以预料的,因为这正是我的代码所做的。 I created a variable determining how far from the origin of the slingshot the bird is once the mouse has been let go and I would like to use this value in a speed calculation.
我创建了一个变量,用于确定一旦鼠标放开 go 后,鸟离弹弓的原点有多远,我想在速度计算中使用这个值。 I don't know what values to use in a quadratic formula to make the bird stay on the screen.
我不知道在二次公式中使用什么值来使鸟留在屏幕上。 I have tried to illustrate the window to make it clearer.
我试图说明 window 以使其更清楚。
float y = getY() + getX()/10 * getX()/10 * (game_time.delta.count() / 10000.f);
setY(y);
//window illustration
------------------------------------------------------------------------------
| (0, 0) |
| |
| |
| o o |
| o |
| o o |
| |
|bird-> o\ / (257, 524) o |
| | |
|_________|______________________________________________________(1280, 720)_|
You have two problems:你有两个问题:
For the first part, I'd suggest you read some article about an oblique shot physics, like kinematics of projectile motion .对于第一部分,我建议您阅读一些关于斜射物理的文章,例如抛射运动的运动学。
In brief:简单来说:
divide the bird motion into horizontal and vertical parts:将鸟的运动分为水平和垂直部分:
calculate horizontal and vertical components of velocity & position independently as a function of time独立计算速度和 position 的水平和垂直分量作为时间的 function
The second problem is easily solved by placing your coordinate system into the lower left part of the window, with y pointing up.第二个问题很容易解决,只需将坐标系放在 window 的左下部分,y 指向上方。 This way you have a "right-hand" coordinate system that will be used for all calculations using equations found on the aforementioned link.
这样,您就有了一个“右手”坐标系,该坐标系将用于使用上述链接上的方程式进行的所有计算。
When you need to actually 'draw' the bird, use the following transformation for y coordinate:当您需要实际“绘制”小鸟时,请对 y 坐标使用以下变换:
y_draw = window_height - y_calculated;
Don't forget to add appropriate offsets for x and y to compensate for the fact that the origin for calculus is different from the position of the slingshot.不要忘记为 x 和 y 添加适当的偏移量,以补偿微积分的原点与弹弓的 position 不同的事实。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.