Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++/平台中的SDL重力_C++_Sdl - Fatal编程技术网

C++ C++/平台中的SDL重力

C++ C++/平台中的SDL重力,c++,sdl,C++,Sdl,我目前正试图在我的platformer游戏中加入一种重力(不需要精确的重力,不需要现实主义),但是我在这个问题上遇到了逻辑障碍 以下代码是我在按下向上箭头或W(跳跃)时使用的代码 在我的Player::Update()函数中,我有 velocity_.y += GRAVITY; 这个函数有更多的功能,但与实际情况无关 目前这两个常数如下:GRAVITY=9.8f和跳跃功率=150.0f 我在重力方面遇到的主要问题是,我无法在我的精灵能够跳跃和过于漂浮之间找到适当的平衡 长话短说,我的问题是,我

我目前正试图在我的platformer游戏中加入一种重力(不需要精确的重力,不需要现实主义),但是我在这个问题上遇到了逻辑障碍

以下代码是我在按下向上箭头或W(跳跃)时使用的代码

在我的
Player::Update()函数中,我有

velocity_.y += GRAVITY;
这个函数有更多的功能,但与实际情况无关

目前这两个常数如下:
GRAVITY=9.8f
跳跃功率=150.0f

我在重力方面遇到的主要问题是,我无法在我的精灵能够跳跃和过于漂浮之间找到适当的平衡


长话短说,我的问题是,我的精灵的跳跃以及他经常从一个平台到另一个平台的坠落都太飘浮了,有没有关于如何将其缩小到更现实一点的想法?

你需要正确地思考单位系统

  • 重力的单位是每秒平方米。(m/(s*s))
  • 速度的单位是米每秒。(米/秒)
  • 力的单位是牛顿。(N=kg*m/(s*s))
  • 概念示例:

    float gravity = -9.8; // m/(s*s)
    float delta_time = 33.333333e-3f; // s
    float mass = 10.0f; // Kg
    float force = grounded_ ? 150.0f : gravity; // kg*m/(s*s)
    float acceleration = force / mass; // m/(s*s)
    float velocity += acceleration * delta_time; // m/s
    float position += velocity * delta; // m
    

    它基于基本的牛顿运动方程和欧拉方法。

    不要根据实际值思考,而要考虑它们的后果

    所以,初始速度是跳跃的幂,加速度是重力。一点微积分就能给出答案

    y = -Height = -jump_power * t + 1/2 * gravity * t^2
    
    这假定时间步长很小

    然后,

    time_in_flight = 2 * time_to_vertex = jump_power/gravity
    
    顶点是

    height(time_to_vertex) = jump_power^2/(4 * gravity)
    
    解决这些问题,调整时间步长并修复负面问题

    jump_power = (4 * height / time) * timestep_in_secs_per_update
    
    gravity = (2 * jump_power / time) * timestep_in_secs_per_update
    
    这样,您就可以处理时间和高度,而不是不太直接的参数。只要用这些方程来计算重力和起跳力就行了

    const int time = 1.5; //seconds
    const int height = 100 //pixels
    
    const int jump_power = (4 * height / time) * timestep_in_secs_per_update;
    const int gravity = (2 * jump_power / time) * timestep_in_secs_per_update;
    
    这是一种来自数学的技术,通常用于根据“无量纲”变量重新排列微分方程族。这样,当您试图操纵方程特性时,变量不会发生干扰。在这种情况下,您可以设置时间并在更改功率时保持时间不变。精灵仍然需要同样的时间降落


    当然,“真实”引力可能不是最好的解决方案。可以将重力设置为较低,并在角色未固定时降低其高度

    不管我怎么做,它最终还是像以前一样飘浮。就像上面的答案一样,它仍然是飘浮的。@AndrewB这实际上只是你自己重新安排的解决方案,所以如果你不能用这个让它看起来很好,那么你就不能用任何“抛物线”轨迹让它看起来很好。就我个人而言(在修复语法错误之后),我得到了看起来不错的低跳转和快速跳转结果,当我添加了一个水平组件时,它们看起来更好
    const int time = 1.5; //seconds
    const int height = 100 //pixels
    
    const int jump_power = (4 * height / time) * timestep_in_secs_per_update;
    const int gravity = (2 * jump_power / time) * timestep_in_secs_per_update;