首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何让物体在任意方向上移动?

如何让物体在任意方向上移动?
EN

Stack Overflow用户
提问于 2015-08-01 21:03:02
回答 1查看 462关注 0票数 0

我有一个360度旋转的雪碧枪,我不想让它朝任何方向发射子弹,我该怎么做?我不能简单地增加子弹的x,y坐标,因为它会向上,向下,向左,向右。

编辑:项目符号移动似乎只会旋转,但不会移动到任何地方或屏幕之外

代码语言:javascript
运行
复制
 bulletPosition.x=MathUtils.cosDeg(cannonObj.cannonAngle())*2;
        bulletPosition.y=MathUtils.sinDeg(cannonObj.cannonAngle())*2;
        bulletVelocity.x=MathUtils.cosDeg(cannonObj.cannonAngle())*10;
        bulletVelocity.y=MathUtils.sinDeg(cannonObj.cannonAngle())*10;

        bulletPosition.x+=bulletVelocity.x*deltaTime;
        bulletPosition.y+=bulletVelocity.y*deltaTime;

//spawning bullets
 public void draw(SpriteBatch batch){
        bulletIterator=bullets.iterator();
        while(bulletIterator.hasNext()){
            Sprite sprite=bulletIterator.next();
            sprite.draw(batch);
            sprite.setPosition(bulletPosition.x,bulletPosition.y);
        }
EN

回答 1

Stack Overflow用户

发布于 2015-08-02 16:42:50

代码语言:javascript
运行
复制
Vector gunPos; // the gun position,dah
Vector bulletPos; //the bullet position
Vector bulletVelocity; //need explanation?
float gunAngle; //the rotation of the gun in degrees
float distanceFromCenter=gunSize.x/2; //the distance from the gun center you want the bullet to appear

bulletPos.x=MathUtils.cosDeg((gunAngle)) * distanceFromCenter;
bulletPos.y=MathUtils.cosDeg((gunAngle)) * distanceFromCenter;
bulletPos.plus(gunPos);

bulletVelocity.x=MathUtils.cosDeg((gunAngle)) * bulletSpeed;
bulletVelocity.y=MathUtils.cosDeg((gunAngle)) * bulletSpeed;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31762144

复制
相关文章

相似问题

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