[SOLVED] Threading problems


photo

Recommended Posts

I'm trying to get a camera to follow an object.

 

In my init function I create an array of cameras then kick of a thread

 

It looks like the thread is executed once

 

Then the camera never moves, never retargets, just looks like the thread runs once and never again.

 

I cannot find anyway of debugging the script, no log is ever created, can't find any way of getting a debug console to open.

 

I'm lost.

 

Any body got any ideas?

 


#include <core/unigine.h>
#include <games/trackdemo/car.h>


Car Cars[0];
Player player;
Node Cameras[0];
Node car;
/*
 */
int init() {
    int index = engine.editor.findNode("chevy_body_0");
    car = engine.editor.getNode(index);
    Cars.append(new Car(car));
    
    index = engine.editor.findNode("camera_0");
    Node cam = engine.editor.getNode(index);


    player = new PlayerSpectator();
    player.setPosition(cam.getWorldPosition());
    Vec3 target = car.getWorldPosition();
    player.setDirection(target-cam.getWorldPosition());
    engine.game.setPlayer(player);

   int i=0;
   int r=0;
   while (r!=-1)
   {
         string name = "camera_" + i;
         r = engine.editor.findNode(name);
         if (r!=-1)
         {
            Cameras.append(engine.editor.getNode(r));
         }
         i++;
   }
   thread("update_scene");
   return 1;
}


/*
 */
int shutdown() {
return 1;
}


/*
 */
int update() {
return 1;
}


void update_scene() 
{


while(1) {


Vec3 target = car.getWorldPosition();
float distance = 788787888787.0f;
int tc =-1;
forloop(int j=0; Cameras.size())
{
Vec3 rel = Cameras[j].getWorldPosition() - target;
float fl = length(rel);
if (fl<distance)
{
distance=fl;
tc=j;
}
}
player.setPosition(Cameras[tc].getWorldPosition());
player.setDirection(target-Cameras[tc].getWorldPosition());
engine.game.setPlayer(player);
if(engine.game.isEnabled()) 
{
forloop(int i = 0; Cars.size()) 
{
Car robot = Cars[i];
robot.Update();
}
}


wait;
}
}

 

Link to post

Debug console requires running of main_x86/x64d.exe instead of main_x86/x64.exe. Also you should insert some log.message() statements at critical code points to show initial/follow-up function invocations (maybe you already did but removed this code here in the code example for clarity)

 

Also you should check the general functioning of your update_scene() code by calling it from update() every frame without threading. Of course you will have to deactivate/remove the while(1) { ...; wait; }. BTW from my point of view this is in general a better code pattern than threading approach.

 

Just as additional remarks the rc camera index is unchecked for -1 initial value. This might lead to invalic index error on Camera array access when there are no cameras . Also the assigned value of 788787888787.0f might exceeds float precision limits.

Link to post

I left the -1 in with out checking deliberately, I hoped it would crash indicating that my code hadn't worked. A very, very rudimentary attempt at debugging.  :wacko:

 

I copied the threading code from the examples I got with the SDK. I tried putting my thread code in the update method and the world failed to load saying something about update returned 0 even though it returned 1.

 

I'll try using the x64d exe and see if I can get some logging.

Link to post

I tried using the debug version, it just throws up an error "cannot find c:\Unigine binary SDk\bin\unigine.cfg" and then crashes

Link to post

Okay, I have logging working. The good news is that the threading is working and the code finds the correct camera and gets the correct position for the car.

 

The bad news is that the display does not change.

 

Does the editor override the changes I am making to the camera?

Link to post

The editor override the changes on active camera, so if you want check your changes, you must quit from UnigineEditor.

Link to post

Shit. Thanks for telling me.

 

I'll have to get the whole lot into visual studio now rather than later.

 

[edit]

Well I got it into VS pretty quickly and cleanly, only problem is the game is frozen.

 

I have tried calling engine.physics.setEnabled(1);  no change.

 

Can't find any methods on engine to run the game, lost again.

 

This is really like knitting fog.

Link to post

made no difference

 

log.message("Physics "+engine.physics.isEnabled());  is showing "Physics 1"

 

ahh engine.game.isEnabled() is false.

 

engine.game.setEnabled(1);  starts everything up and my camera code works.

 

Now to find out why my AI is not driving the car properly.

 

Getting somewhere at last.

Link to post

well I know why my code isn't working. 

 

Node.getDirection()   is wrong.

 

My car is moving along the X axis so it's direction should be something close to (-1,0,0)  it's reporting something close to (0,0,-1)

 

Thus my car is trying to turn around

Link to post

The rotation value you enter in the editor appears to go into a model matrix instead of the world matrix.

 

So if you want to add a mesh facing a particular direction, you cannot use the editor.

 

Very, very, poor

Link to post

https://developer.unigine.com/en/docs/1.0/scripting/library/nodes/class.node#setWorldDirection_vec3_vec3_void

void setWorldDirection (vec3 dir,vec3 up)

Updates the direction of the node. By default, a direction vector points along the mesh local -Z axis (vec3(0,0,-1)) and is set in world space coordinates. This function changes its direction and reorients the mesh.

 

https://developer.unigine.com/en/docs/1.0/scripting/library/nodes/class.node#setDirection_vec3_vec3_void

void setDirection (vec3 dir,vec3 up)

Updates the direction vector of the node. By default, a direction vector points along the mesh local -Z axis (vec3(0,0,-1)). This function changes its direction and reorients the mesh.

 

PS: Please use "one issue - one forum thread" strategy, thus it will be easier to help you and for other developers to use the knowledge base.

Link to post

Those two functions are available in the editor?

 

No. Didn't think so.

 

Seems to me the editor is a disaster waiting to happen.

Link to post

When I added the node it was facing the wrong way on the track, so I opened the node panel and rotated the object to the correct orientation.

 

As I was adding an object to a scene, I assumed this would be stored in the world matrix for the node.

 

Looks like this goes into the model matrix instead. That meant that everything after that is wrong, getDirection is wrong, getWorldDirection is wrong.

 

I can't see any other way to change the orientation of a node in the editor.

Link to post

Hi,

I'm afraid, it's difficult to provide a solution without full test scene.
It would be very helpful if you can send us a test scene with descriptions "what you get" - "what you want to achieve"

For now it's not totally clear what do you mean by "world matrix for the node" and "model matrix" and what do you expect to get using getDirection/getWorldDirection.

Thanks!

Link to post