随机方向,无需视觉旋转形状 opengl-es android
我希望能够为一个形状指定一个随机方向,使其以常规速度进入。
我尝试为平移的 x 和 y 值分配一个随机数,但这导致形状有时移动得太快或者只是从屏幕上消失。
有没有办法在不旋转形状的情况下选择形状移动的随机方向(至少就用户所知)?
还有一种方法可以在调用事件(即:单击按钮)时重新生成随机数,从而允许改变方向?
编辑:刚刚检查。使用具有随机角度的旋转函数会将正方形沿随机方向射出,但是仍然有办法在不改变形状方向的情况下做到这一点吗?
I want to be able to assign a shape a random direction to go in at a regular speed.
I tried assigning a random number to the x and y values of a translation but that caused the shape to sometimes move way too fast or just blip off the screen.
Is there a way to choose a random direction for a shape to move in without rotating the shape (at least as far as the user can tell)?
Also is there a way of re-genereating the random number when an event is called (ie: button click) which would allow a change in direction?
EDIT: Just checked. Using the rotate function with a random angle will shoot the square off in a random direction, but still is there a way to do this without altering the orientation of the shape?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,那么您想要一个形状在 x、y 或 z 方向的正向或负向移动吗?
如果你希望它是随机的,那么使用 Random 类。由此,您可以使用 Random.nextInt(1),它要么是 0,要么是 1,如果结果是 0,则将值设置为 -1。
+1 或 -1 现在用于您的方向。
现在您知道物体的行进方向,您可以简单地将其乘以物体的速度
这是一些粗略的代码
这可能不是您的界面的设置方式(例如 theShape.position 或 theShape.speed),但想法是相同的
再次阅读此内容后,我认为您正在尝试旋转你的形状哈哈,旋转不仅仅是增加 x 和 y 值。您在应用程序中使用矩阵吗?如果不是,您应该使用 trig 来计算旋转 x 度后的 x 和 y 值
Ok, so you want a shape to move in either the postive or negative x, y or z directions?
If you want this to be random then use the Random class. From this you can use Random.nextInt(1) which will either be 0 or 1, if the result is 0 then set the value to be -1.
The +1 or -1 is now used for your direction.
Now you know which way the object travels you can simply multiply this by the speed of your object
Here is some rough code
This may not be how your interface is setup (such as theShape.position or theShape.speed) but the idea is the same
After reading this again I think you are trying to rotate your shape lol, rotation is not just adding to the x and y values. Do you use matrices in your application? If not your should use trig to figure out what the x and y values will be after a rotation of x degrees