首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >XNA -如何提高我的精灵速度

XNA -如何提高我的精灵速度
EN

Stack Overflow用户
提问于 2014-04-08 00:58:14
回答 2查看 654关注 0票数 0

所以我是一个编程的初学者,我需要帮助如何使我的精灵速度增加(添加一个冲刺功能)

我试过了,但我失败了,如果有人能解释我怎么做,我会非常感激。

我想要做的是,如果我按下我的Xbox控制器上的右触发器,同时向右移动我的左手拇指杆,将速度从4改为6。

这是我试过的东西,我意识到这不会起作用,因为当我按下他的扳机时,它只会让精灵移动。

代码语言:javascript
复制
if (pad1.Triggers.Right < 0.0f)
  position += new Vector2(-6, 0);

if (pad1.Triggers.Right >= 0.4f)
  position += new Vector2(6, 0);

我别无选择了,请帮帮忙。

EN

回答 2

Stack Overflow用户

发布于 2014-04-08 03:19:20

这从根本上说是一个物理问题。向该位置添加一个常量只会使精灵移动得更多。你可能正在寻找一种在短时间内提高速度的方法,这就是所谓的加速度。

代码语言:javascript
复制
const float AccelerationForce = 0.ff;
const float MaxAccelerationForce = 0.8f;;
static readonly Vector2 MaxAcceleration
    = new Vector2(MaxAccelerationForce, MaxAccelerationForce);
static readonly Vector2 Acceleration = new Vector2(AccelerationForce, 0);
static readonly Vector2 Deceleration = new Vector2(-AccelerationForce, 0);
Vector _currentAcceleration = new Vector2(0, 0);
Vector _currentVelocity = new Vector2(0, 0);

Vector2 Clamp(Vector2 value, Vector2 min, Vector2 max)
{
    var x = MathHelper.Clamp(value.X, min.X, max.X);
    var y = MathHelper.Clamp(value.Y, min.Y, max.Y);

    return new Vector2(x, y);
}

bool RequiresAcceleration(float currentForce)
{
    return currentForce >= AccelerationForce;
}

var currentForce = GetCurrentForce();
// Calculate whether we need to accelerate or decelerated
var acceleration = RequiresAcceleration(currentForce)
                       ? Acceleration
                       : Deceleration;
var newAcceleration = _currentAcceleration + acceleration;
// acceleration is bounded MaxAcceleration > _currentAcceleration > 0
_currentAcceleration = Clamp(newAcceleration, Vector2.Zero, MaxAcceleration);

_currentVelocity += _currentAcceleration;

// Code to move the player

if (pad1.Triggers.Right < 0.0f)
  position += _currentVelocity);

if (pad1.Triggers.Right >= 0.4f)
  position -= _currentVelocity;
票数 1
EN

Stack Overflow用户

发布于 2014-04-08 02:09:54

我相信这就是你想要实现的。另外,编辑帖子是相当容易的,请编辑帖子,而不是评论与更新。

代码语言:javascript
复制
        if (pad1.Triggers.Right < 0)
            // This checks if left thumbstick is less than 0.
            // If it is less than zero it sets position.X += -6. 
            // If it is not less then it sets position.X += -4.
            position.X -= (pad1.ThumbSticks.Left.X < 0) ? 6 : 4;
        else if (pad1.Triggers.Right >= 0.4f) // shouldn't this be > 0.0f or > 0 ?
            // This is the same as above just in the right direction.
            position.X += (pad1.ThumbSticks.Left.X > 0) ? 6 : 4;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22918596

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档