# v.mommersteeg

Member

11

106 Neutral

• Rank
Member
1. ## Help requested on Ray Triangle Collision

I just can't seem to get this implementation right.. does anyone have some other great example of sphere triangle collision? or maybe point out what is wrong with this implementation. Thanks
2. ## 3rd person camera rotation translation problem

You can close this topic. I fixed the problem. I shouldn't use the transform matrix for the offset. because it was giving me a delay. all I had to do was airplaneObject->rotation = camera->m_Rotation; airplaneObject->position = camera->m_Position + vector3 ( 0, 0, 1.5f ); And now it works just fine. Thanks anyway.
3. ## 3rd person camera rotation translation problem

Hi, I'm creating a 3rd person camera which should follow an aircraft over a plane. The problem is that when I make the rotation of the camera follow the rotation of my aircraft the camera follows my aircraft fine in the rotation but stops following the aircraft when i start translating. If I make the rotation of my aircraft follow the rotation of my camera, the translation works fine and my camera follows my aircraft, but when I start rotation, it stops following my aircraft... So I'm completely clueless why this is happening. An another thing is that when I make the rotation of the camera follow the aircraft I have to use the airplanes position and rotation to show effect, else it wont move. And if i make the rotation of the aircraft follow the camera, then I have to use the rotation and position of the camera, else it wont move either. Thanks in advance. Here is the code: // camera.cpp void Camera::updateMatrix(vector3 Position, vector3 Rotation) { m_Position = Position; m_Rotation = Rotation; m_WorldMatrix.Init(m_Position, m_Rotation.x, m_Rotation.y, m_Rotation.z); m_ViewMatrix = m_WorldMatrix; m_ViewMatrix.Invert(); } void Camera::lookAt(vector3 eye, vector3 target, vector3 up) { vector3 zaxis = (target - eye); // The "look-at" vector. zaxis.Normalize(); vector3 xaxis = up.Cross(zaxis);// The "right" vector. xaxis.Normalize(); vector3 yaxis = zaxis.Cross(xaxis); // The "up" vector. m_WorldMatrix.cell[0] = xaxis.x, m_WorldMatrix.cell[1] = yaxis.x, m_WorldMatrix.cell[2] = zaxis.x, m_WorldMatrix.cell[4] = xaxis.y, m_WorldMatrix.cell[5] = yaxis.y, m_WorldMatrix.cell[6] = zaxis.y, m_WorldMatrix.cell[8] = xaxis.z, m_WorldMatrix.cell[9] = yaxis.z, m_WorldMatrix.cell[10] = zaxis.z; m_ViewMatrix = m_WorldMatrix; m_ViewMatrix.Invert(); } // game.cpp update function void Game::Tick( float a_DT ) { m_Screen->Clear( 0 ); if(GetAsyncKeyState(VK_LEFT)) { camera->m_Position.x -= 0.1f; } if(GetAsyncKeyState(VK_RIGHT)) { camera->m_Position.x += 0.1f; } if(GetAsyncKeyState(VK_UP)) { camera->m_Position.z += 0.1f; } if(GetAsyncKeyState(VK_DOWN)) { camera->m_Position.z -= 0.1f; } if(GetAsyncKeyState(0x57)) // w move upwards { camera->m_Position.y += 0.1f; } if(GetAsyncKeyState(0x53)) // s move downwards { camera->m_Position.y -= 0.1f; } // rotation in the yaw, pitch, roll if(GetAsyncKeyState(0x41)) // a (roll angle around z) { camera->m_Rotation.z += 0.5f; } if(GetAsyncKeyState(0x44)) // d (roll angle around z) { camera->m_Rotation.z -= 0.5f; } if(GetAsyncKeyState(0x46)) // f (yaw angle around x) { //camera->m_Rotation.x += 0.5f; airplaneObject->rotation.y += 0.5f; } camera->m_Rotation = airplaneObject->rotation; // airplaneObject->rotation = camera->m_Rotation; // camera->m_Position = airplaneObject->m_Matrix.Transform ( vector3 ( 0, 0, -1.5f ) ); airplaneObject->position = camera->m_WorldMatrix.Transform(vector3(0,0, 1.5f)); camera->lookAt(camera->m_Position, airplaneObject->position, vector3(0,1,0)); camera->updateMatrix(camera->m_Position, camera->m_Rotation); } Now translation works fine but rotation doesn't. If I were to switch between: camera->m_Rotation = airplaneObject->rotation; // airplaneObject->rotation = camera->m_Rotation; // camera->m_Position = airplaneObject->m_Matrix.Transform ( vector3 ( 0, 0, -1.5f ) ); airplaneObject->position = camera->m_WorldMatrix.Transform(vector3(0,0, 1.5f)); And this: // camera->m_Rotation = airplaneObject->rotation; airplaneObject->rotation = camera->m_Rotation; camera->m_Position = airplaneObject->m_Matrix.Transform ( vector3 ( 0, 0, -1.5f ) ); // airplaneObject->position = camera->m_WorldMatrix.Transform(vector3(0,0, 1.5f)); Now the rotation would work fine, but the translation would not.

5. ## Help with SIMD

I already got a different part converted so this is not that important anymore. Thanks anyway!
6. ## Help with SIMD

I'm trying to learn SIMD, and I was trying to optimize this function below, but I can't get it quite to work. I think maybe I'm overlooking the problem. This is the original function without any SIMD which I'm trying to convert. void MetalBall::Tick() { if ((m_X -= .2f) < -50) { m_X = SCRWIDTH * 4; } m_Sprite->Draw( (int)m_X, (int)m_Y, m_Surface ); Pixel* src1 = m_Sprite->GetBuffer() - 1; for ( int y = 0; y < 50; y++ ) { int ty = (int)m_Y + y; if((ty < 0) || (ty >= SCRHEIGHT)) { src1 += 50; continue; } float dy = a[y]; int sy = (int)(((m_Y + 25) + (int)(sinxy[y] * dy) + SCRHEIGHT)) % SCRHEIGHT; for ( int x = 0; x < 50; x++ ) { src1++; int tx = (int)m_X + x; if ((tx < 0) || (tx >= SCRWIDTH)) { continue; } if (!(*src1 & 0xffffff)) { continue; } float dx = a[x]; float l = presqrt[y][x]; int sx = (int)(((m_X + 25) + (int)(sinxy[y] * dx) + SCRWIDTH)) % SCRWIDTH; Pixel* src2 = m_Surface->GetBuffer() + sx + sy * m_Surface->GetPitch(); Pixel* dst = m_Surface->GetBuffer() + tx + ty * m_Surface->GetPitch(); *dst = AddBlend( *src1, *src2 & 0xffff00 ); } } } This is the attempt in SIMD I tried to do. void MetalBall::Tick() { if ((m_X -= .2f) < -50) { m_X = SCRWIDTH * 4; } m_Sprite->Draw( (int)m_X, (int)m_Y, m_Surface ); Pixel* src1 = m_Sprite->GetBuffer() - 1; static union{float my[4]; __m128 my4;}; static union{float dy[4]; __m128 dy4;}; static union{int ty[4]; __m128i ty4;}; static union{int tx[4]; __m128i tx4;}; static union{float mx[4]; __m128 mx4;}; static union{float dx[4]; __m128 dx4;}; mx4 = _mm_add_ps(_mm_set_ps1(m_X), _mm_set_ps1(25)); my4 = _mm_add_ps(_mm_set_ps1(m_Y), _mm_set_ps1(25)); for ( int y = 0; y < 50/4; y+=4) { ty4 = _mm_add_epi32(_mm_set1_epi32(m_Y), _mm_set1_epi32(y)); for(int k=0; k<4; k++) { if((ty[k] < 0) || (ty[k] >= SCRHEIGHT)) { src1 += 50; continue; } dy4 = _mm_set_ps1(a[y]); int sy = (int)((my[k] + (int)(sinxy[y] * dy[k]) + SCRHEIGHT)) %SCRHEIGHT; for ( int x = 0; x < 50/4; x+=4 ) { src1++; tx4 = _mm_add_epi32(_mm_set1_epi32(m_X), _mm_set1_epi32(x)); if ((tx[k] < 0) || (tx[k] >= SCRWIDTH) ) { continue; } if (!(*src1 & 0xffffff)) { continue; } dx4 = _mm_set_ps1(a[x]); float l = presqrt[y][x]; int sx = (int)((mx[k] + (int)(sinxy[x] * dx[k]) + SCRWIDTH)) %SCRWIDTH; Pixel* src2 = m_Surface->GetBuffer() + sx + sy * m_Surface->GetPitch(); Pixel* dst = m_Surface->GetBuffer() + tx[k] + ty[k] * m_Surface->GetPitch(); *dst = AddBlend( *src1, *src2 & 0xffff00 ); } } } } If someone would be kind enough to help me out, I would be very grateful.
7. ## Problem with arrays

[source='l'][/source][type]l[/type]Oh thats why they all arc at the same time. But if I would put countTimer = rand() % 100; in my Invader constructor, they would not start arcing at the same time, but still very close to each other. So you recomend me to make a function that sets the state of my enemy? Something like?: [source] void SetState(EnemyState enemyState); [/source]
8. ## Make an array of pictures move?

Why did you use a 2D array? you can just use a 1D array and loop through them. And you could make a struct that contains a xpos and a ypos and even place a surface in there. Something like: Struct enemy { int xpos; int ypos; Surface *graphic; }; And then what you need to do is make a boolean that decides the direction of all your bad guys are going in your array. and then change this boolean when they hit the end of the screen. Something like: for(int i = 0; i < theamountofenemies; i++) { if(enemy.xpos > screenwidth - the width of your enemy) {movingLeft = true; } else if( enemy.xpos < 0) { moveLeft = false; } if(moveLeft) { enemy.xpos -= 1;} else { enemy.xpos += 1;} } So what this code does is that when they hit the 0 (the right side of the screen), you change that boolean to false so that they move in opposite direction, until they hit the other side. And with the moveLeft boolean you decide if the x position of your enemy is going left or right. And you do that for each invader in your array.
9. ## Problem with arrays

Oh I thought I needed to loop through them in the Invader class so that the function in the Game class would have the correct index of the array, so of only one invader. Oke I have deleted all the for loops in my Invader class, but now they are all arcing again and not just one. I guess I'm still doing something wrong. Because I want them all to move but only one to arc. So I have the conflict that only one invader should be aware of doing something "arcing, falling, rejoining" while the others keep doing their business by just moving left and right. So how could I achieve that?
10. ## Problem with arrays

And how would I make them update only one? Because I was trying to only to update the index of the array, but I think I don't understand the concept very well about how parameterized arrays work. Because now I am updating them all I guess... Oh thanks for the tip. I will delete the enemy bullet in my destructor. I'm still learning the concept about destructors and deleting objects. Because I first learned some C# and XNA before I now started with C++, so I'm used to the garbage collector.