开发者

Random direction without visually rotating shape opengl-es android

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 w开发者_运维技巧ithout altering the orientation of the shape?


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

Random rand = new Random(); int direction = rand.nextInt(1);

if(direction == 0) direction = -1;

Shape theShape.position.x += direction * theShape.speed

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜