Rockets to face in the direction it‘s going

本文介绍了一种游戏开发中实现游戏对象自动旋转的方法。通过记录物体的当前位置并与其旧位置进行对比,可以计算出物体的旋转角度,使得如火箭等游戏元素能够根据其运动方向进行动态旋转。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I've been wondering, is there a feature where you can, like for my example, set the rotation of sprites depending on the direction it is going?

For example :

When i shoot the rocket upwards, it will face upwards.
when the rocket starts to fall downwards due to gravity, the rocket's sprite would also start rotating and face downwards.

Or something to simulate behaviors similar to shuttlecocks in badminton.

 

A:

 

On each step you need to store the position of the object you want to rotate and then compare that to the current position, it would look something like this:

float angle = -CC_RADIANS_TO_DEGREES(atan2f(oldPos.y-myObject.position.y, oldPos.x-myObject.position.x)); 
angle = angle < 0 ? angle + 360 : angle; myObject.rotation = angle - 180.0f;        
 // I minus 180 so that my object is facing the right direction, depending on your objects initial layout y//ou may have to plus or minus a different number.  
oldPos = myObject.position;
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值