Jump to content

[SOLVED] Create randomly moving objects


photo

Recommended Posts

Perhaps this is a too simple question, but I have been sitting with this for a long time. 
The task is simple - I need to create a number of identical objects that move in a random direction. I use primitive boxes and add to them a property (c++ code) that generates a move in a random direction. But when launched, they all move in the same direction. Moreover, when restarting, the direction changes, but as before it is the same for everyone. Perhaps I am missing something, but I did not find a solution to this problem in the documentation.

 

Edited by NotNa19
Link to comment

I have a property where a random direction of movement is set:

Spoiler
#include "BoxMove.h"
#include <ctime>

using namespace Unigine;
using namespace Math;

REGISTER_COMPONENT(BoxMove);

void BoxMove::init()
{
}

void BoxMove::update()
{
	Unigine::Math::Random rd;
	vec3 dir;
	dir = vec3(rd.getInt(-1, 2), rd.getInt(-1, 2), 0);
	vec3 p0 = node->getWorldPosition();
	vec3 p1 = p0 + dir * speed * Game::getIFps();
	node->setWorldPosition(p1);
}

 

And I have three boxes with this property:

Spoiler

image.png.0fbb8c7539f4c632e3956393bd4bdd1d.png

I want the boxes to move in a random direction, but each box has a different direction.
But they all move in the same, although a random direction:

Spoiler

 

Link to comment

Will be there any difference if you will use Game::getRandomInt() instead of Math::Random? If that will not work, try to set different seeds each time via Game::setSeed().

I suppose that for Math::Random you need also specify different seeds each time via setSeed() method.

Thanks!

How to submit a good bug report
---
FTP server for test scenes and user uploads:

Link to comment
  • NotNa19 changed the title to [SOLVED] Create randomly moving objects
×
×
  • Create New...