strange error with a ray

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Seraph
Posts: 68
Joined: Wed Oct 21, 2009 5:58 pm

strange error with a ray

Post by Seraph »

i'm going crazy, i cannot understand where is the problem :(

this code works and draws all the rays in the correct position.

Code: Select all

//inside a function with a parameter "i"

line3df ray[i][4];
line3df ray2[i][4][2];   
vector3df temp;      
int offset=-90; 
for(int j=0;j<4;j++)
      {ray[i][j].start = vector3df(enemy[i]->getPosition().X,enemy[i]->getPosition().Y+30,enemy[i]->getPosition().Z);
       temp=vector3df(enemy[i]->getRotation().X,enemy[i]->getRotation().Y+offset,enemy[i]->getRotation().Z);
       ray[i][j].end =ray[i][j].start + (temp).rotationToDirection() * 100.0f;	                             
                 } 
for(int j=0;j<4;j++)
     game->driver->draw3DLine(ray[i][j].start,ray[i][j].end,SColor(255,0,100,0));
but when i insert ray2:

Code: Select all

line3df ray[i][4];
line3df ray2[i][4][2];   
vector3df temp;      
int offset=-90; 
for(int j=0;j<4;j++)
      {ray[i][j].start = vector3df(enemy[i]->getPosition().X,enemy[i]->getPosition().Y+30,enemy[i]->getPosition().Z);
       temp=vector3df(enemy[i]->getRotation().X,enemy[i]->getRotation().Y+offset,enemy[i]->getRotation().Z);
       ray[i][j].end =ray[i][j].start + (temp).rotationToDirection() * 100.0f;

       ray2[i][j][0].start=ray[i][j].start + (temp).rotationToDirection()*50.0f;
       ray2[i][j][0].end=game->player->player->getPosition();	                             
                 } 
for(int j=0;j<4;j++)
     game->driver->draw3DLine(ray[i][j].start,ray[i][j].end,SColor(255,0,100,0));


the ray (not ray2!) is in a different position. why?
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: strange error with a ray

Post by randomMesh »

Did you set the proper material and transformation? Your code doesn't show this. Also, where do you draw the rays? Inside the render loop?
"Whoops..."
Seraph
Posts: 68
Joined: Wed Oct 21, 2009 5:58 pm

Post by Seraph »

Code: Select all

SMaterial m; 
m.Lighting = false; 
game->driver->setMaterial(m); 
game->driver->setTransform(video::ETS_WORLD, core::matrix4()); 
the rays are draw in the last line of the code (in the first post)
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

Seraph wrote:the rays are draw in the last line of the code (in the first post)
Yeah, i can see that the last line has a draw3DLine in it. But where do you draw it? Inside the render loop?

And try getAbsolutePosition instead of getPosition, since the latter only returns the relative position. Dont know if enemy has a parent, your code doesn't show this.

Please post some minimal compilable snippets in the future.
"Whoops..."
Seraph
Posts: 68
Joined: Wed Oct 21, 2009 5:58 pm

Post by Seraph »

thanks for the help :D

well, this code is inside the function Update of the enemy and the render loop is this:

Code: Select all

while(device->run() && driver)
	{
          driver->beginScene(true, true, SColor(0,0,0,0));           
	       smgr->drawAll();	       
	       guienv->drawAll();	       
          enemy->Update(i);
	       driver->endScene();
	}

i have isolated the problem. This code works:

Code: Select all

ray[0][0].start = enemy[0]->getAbsolutePosition();
ray[0][0].end =ray[0][0].start + (enemy[0]->getRotation()).rotationToDirection() * 100.0f;	 
game->driver>draw3DLine(ray[0][0].start,ray[0][0].end,SColor(255,255,255,0));
and this not:

Code: Select all

ray[0][0].start = enemy[0]->getAbsolutePosition();
ray[0][0].end =ray[0][0].start + (enemy[0]->getRotation()).rotationToDirection() * 100.0f;	 
ray2[0][0][0].end =ray[0][0].start + (enemy[0]->getRotation()).rotationToDirection() * 100.0f;                                                 
game->driver->draw3DLine(ray[0][0].start,ray[0][0].end,SColor(255,255,255,0));
the ray in the second part of code is in a different position
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Ok, My bet is that you simply overwrite some values. The arrays make absolutely not sense this way (because you don't iterate over i, but only get a parameter, what for?) And these multi-arrays are simply bad to handle.
Seraph
Posts: 68
Joined: Wed Oct 21, 2009 5:58 pm

Post by Seraph »

this is a simple code with the error, obviously in the complete function there are array with parameters. The problem is that the ray is use only in this part and therefore i cannot overwrite nothing...

The row that i have added in the second code should not influence the ray.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Oh, short confusion on my side. You simply fail to allocate ray[0][4], because the array size needs i+1. Right now, if i is 0 you don't get any fields allocated.
As I said, multi-arrays are evil :P
Seraph
Posts: 68
Joined: Wed Oct 21, 2009 5:58 pm

Post by Seraph »

i'm sorry but i believe that i haven't understood, in the first post with the 'for' for each enemy there are:

ray[0],ray[1],ray[2],ray[3].

i don't allocate ray[0][4]. In fact the first example works, is the second that don't works...
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

The problem is that you overwrite ray when writing to ray2 in the case where i is 0. Simply because they point to the very same position (at least some fields in those arrays)
Seraph
Posts: 68
Joined: Wed Oct 21, 2009 5:58 pm

Post by Seraph »

well, if i create ray2 with the first parameter different by i, it works but, why they point to the same poisition? they are different array...
I can create the same function with a different structure?
for example with array<line3df>ray i can create a multiple array?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

No, when using [0] in an array definition you will always fail. This is (well someone else might know the exact wording from the standard, but I'd say:) undefined. Since your compiler swallows this code it's at least not illegal. The problem is that you define a nullsize array, which means you don't want space for it. However, you access the field [0] later on, which means you handle it as if there is space. And by that, you simply read/write over the array bounds.
But I'd suggest to clean up the code. To me it looks like you don't need the first and the third array index. And when just using ray[4] and ray2[4] it will work without errors. Or write [i+1] in the declaration, as I suggested before.
Seraph
Posts: 68
Joined: Wed Oct 21, 2009 5:58 pm

Post by Seraph »

:D ok i have understood thanks
Post Reply