Jump to content
  • Advertisement

v.mommersteeg

Member
  • Content Count

    11
  • Joined

  • Last visited

Community Reputation

106 Neutral

About v.mommersteeg

  • Rank
    Member
  1. v.mommersteeg

    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. v.mommersteeg

    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. 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.
  4. (Sorry if the post says Ray triangle, I was first trying with a ray first, but that wouldn't work with my airplane, so I'm trying sphere triangle now.) Hi I am new to 3D and I'm trying to create a sphere triangle collision detection. I want to use this method because I have an airplane flying above a plane which is not flat. What I would want to check is if the airplane is crashing on one of the triangles of my plane. I have been researching all around and recapping my vector math a bit, and came up with this implementation. The problem of this implementation is that the dot product with the vertices gives me a negative number ( (pointB - pointA).Dot(pointA) > 0 && (pointC - pointA).Dot(pointA) > 0) which is smaller than 0, so it returns as a hit, which is not. But the distance to the faces works fine, and the distance to the vertices too. But it actually only finds a hit on one specific point on my plane which is strange. If someone could give me some suggestions what is wrong with this implementation I would be glad. Below I have the implementation. I load in an obj model and then push all the vertices in the vertex list which is a std vector, and all the triangles in the triangle list. the vertex list is a list of vector3 and the triangle list is a list of triangles which are compound out of three int indices. Thanks in advance. I got this implementation from this site: http://realtimecolli...net/blog/?p=103 // sphere - plane collision. face, edge, vertex acurate bool Game::isColliding(MeshObject* object, float radius, MeshObject* plane) { for(int f = 0; f < plane->mesh->triangleList.size(); f++) { vector3 spherePos = object->position; vector3 vertex0 = plane->mesh->verticeList.at(plane->mesh->triangleList[f].m_Indices[0]); vector3 vertex1 = plane->mesh->verticeList.at(plane->mesh->triangleList[f].m_Indices[1]); vector3 vertex2 = plane->mesh->verticeList.at(plane->mesh->triangleList[f].m_Indices[2]); vector3 pointA = vertex0 - spherePos; vector3 pointB = vertex1 - spherePos; vector3 pointC = vertex2 - spherePos; // -------------*** Face checks ***------------ // facedistance check float distanceFace = abs(pointA.Dot(plane->mesh->triangleList[f].m_Normal)); // face separation check float separatedf; if(distanceFace > radius) { separatedf = 1;}else {separatedf = 0;} // ----------*** end face checks ***------------ // ----------*** Vertex checks ***------------- // vertexdistance check float distanceVertex0 = sqrt(pointA.Dot(pointA)); float distanceVertex1 = sqrt(pointB.Dot(pointB)); float distanceVertex2 = sqrt(pointC.Dot(pointC)); // vertex separation checks float separatedv0; if(distanceVertex0 > radius && (pointB - pointA).Dot(pointA) > 0 && (pointC - pointA).Dot(pointA) > 0) {separatedv0 = 1;}else{separatedv0 = 0;} float separatedv1; if(distanceVertex1 > radius && (pointB - pointA).Dot(pointB) > 0 && (pointC - pointB).Dot(pointB) > 0) {separatedv1 = 1;}else{separatedv1 =0;} float separatedv2; if(distanceVertex2 > radius && (pointC - pointA).Dot(pointC) > 0 && (pointC - pointB).Dot(pointC) > 0) {separatedv2 = 1;}else{separatedv2 =0;} float separatedv = separatedv0 || separatedv1 || separatedv2; // --------*** end vertex check ***------------- // ----------*** Edges checks ***-------------- // B - A vector3 ABEdge = vertex1 - vertex0; float ABT = ((spherePos - pointA).Dot(ABEdge)) / (ABEdge.Dot(ABEdge)); vector3 projA = pointA + ABT * ABEdge; // projection center sphere onto plane edge A vector3 SphereProjA = spherePos - projA; vector3 v2Proj = pointC - projA; float separatedEdgeA; if(sqrt(SphereProjA.Dot(v2Proj)) > radius && SphereProjA.Dot(v2Proj)) {separatedEdgeA = 1;}else{separatedEdgeA = 0;} // C - B vector3 BCEdge = vertex2 - vertex1; float BCT = ((spherePos - pointB).Dot(BCEdge)) / (BCEdge.Dot(BCEdge)); vector3 projB = pointB + BCT * BCEdge; // projection center sphere onto plane edge B vector3 SphereProjB = spherePos - projB; vector3 v0Proj = pointA - projB; float separatedEdgeB; if(sqrt(SphereProjB.Dot(v0Proj)) > radius && SphereProjB.Dot(v0Proj)) {separatedEdgeB = 1;}else{separatedEdgeB = 0;} // A - C vector3 CAEdge = vertex0 - vertex2; float CAT = ((spherePos - pointC).Dot(CAEdge)) / (CAEdge.Dot(CAEdge)); vector3 projC = pointC + CAT * CAEdge; // projection center sphere onto plane edge C vector3 SphereProjC = spherePos - projC; vector3 v1Proj = pointB - projC; float separatedEdgeC; if(sqrt(SphereProjC.Dot(v1Proj)) > radius && SphereProjC.Dot(v1Proj)) {separatedEdgeC = 1;}else{separatedEdgeC = 0;} // edge separation check float separatede = separatedEdgeA || separatedEdgeB || separatedEdgeC; // ----------*** end edge check ***------------------- if(separatedf || separatedv || separatede) { return false; } else { return true; } } }
  5. v.mommersteeg

    Help with SIMD

    I already got a different part converted so this is not that important anymore. Thanks anyway!
  6. v.mommersteeg

    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. v.mommersteeg

    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. v.mommersteeg

    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. v.mommersteeg

    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. v.mommersteeg

    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.
  11. v.mommersteeg

    Problem with arrays

    Hi, So what I'm trying to make is a clone of Galaxian to learn more C++. The problem is that I'm not quite getting what I want. What I'm trying to do is make only one enemy arc and the others just move. I've been trying several things, but it is either that they all arc, or one keeps on arcing and doesn't fall down, or they all fall down. So if someone could give me some suggestions on how to solve my problem, that would make me one happy person Also a strange thing is that my bullet of my enemy does not seem to update. So I guess I have problems passing arrays as parameters and making the right updates. So what I have done now is that I made an Invader class where I try to handle all the logic movement of the invader so that I can add it in my game.cpp class. I tried to make the functions this way that I can use it on multiple enemies. So i.e. a blue drone, and a yellow drone and a purple drone. Because the original galaxian also had this. So this is what I did: Invader.cpp Invader::Invader() { enemyState = MOVING; enemyBullet = new Bullet(); enemyBullet->isFired = false; enemyBullet->yPos = 0; isAlive = true; countTimer = 10; countArc = 0; } Invader::~Invader() {} void Invader::EnemyMovement(Invader invader[], int length) { for(int i =0; i< length; i++) { switch (enemyState) { case MOVING: { invader.EnemyMoving(invader, length); break; } case ARCING: { invader->EnemyArcing(invader, length); break; } case FALLING: { invader->EnemyFalling(); break; } case REJOINING: { invader->EnemyRejoinging(); break; } default: NONE; } // switch } } void Invader::EnemyMoving(Invader invader[], int length) { //std::cout << "moving"<< std::endl; for(int i =0; i< length; i++) { if(moveLeft) { invader.Xpos -= 1; } else { invader.Xpos += 1; } if(invader.countTimer >= 0) { invader.countTimer--; if(invader.countTimer <= 0) { invader.countTimer = 0; enemyState = ARCING; } } } } void Invader::EnemyArcing(Invader invader[], int length) { std::cout << "arcing"<< std::endl; for(int i =0; i <length; i++) { int radius = 80; if(invader.countArc == 0) { PivotX = invader.Xpos + radius; PivotY = invader.Ypos; invader.countArc++; } // start arcing if (countArc != 0) { invader.Xpos = PivotX + (radius * cosf(countArc * 2 * PI/360 + PI)); invader.Ypos = PivotY + (radius * sinf(countArc * 2 * PI/360 + PI)); invader.countArc++; } // change state if(invader.countArc == 180) { invader.enemyState = FALLING; invader.countArc = 0; // ready for next time. } } } void Invader::EnemyFalling() { std::cout << "falling"<< std::endl; countTimer = 10; Ypos += 1; UpdateEnemyBullet(); if(Ypos > SCRHEIGHT) { enemyState = REJOINING; } } void Invader::EnemyRejoinging() { std::cout << "rejoining"<< std::endl; } void Invader::ChecksEnemyBorder(Invader invader[], int length) { for(int i = 0; i< length; i++) { if(invader.isAlive && invader.Xpos > SCRWIDTH - 50) { moveLeft = true; } else if(invader.isAlive && invader.Xpos < 0) { moveLeft = false; } } } //Updates the bullet movement and sets bullet draw to false if out of screen void Invader::UpdateEnemyBullet() { if(!enemyBullet->isFired && isAlive) { enemyBullet->isFired = true; enemyBullet->xPos = xpos + 16; enemyBullet->yPos = ypos; enemyBullet->yPos += 3; std::cout << "xpos = " << Xpos << "ypos = " << Ypos << std::endl; std::cout << "enemybullet ypos = " << enemyBullet->yPos << std::endl; if(enemyBullet->yPos > SCRHEIGHT) { enemyBullet->isFired = false; } } } game.cpp Invader blueDrones[BLUEDRONES]; bool Invader::moveLeft = false; void Game::Init() { int counter = 0; for(int j = 0; j< 2; j++)//colum { for(int i =0; i < 2; i++)// row { blueDrones[counter].Xpos = 100 + ( i * 60); blueDrones[counter].Ypos = 50 + (j * 50); blueDrones[counter].Graphic = blueDroneSpr ; counter++; } } } void Game::Tick( float a_DT ) { for(int t = 0; t < BLUEDRONES; t++) { blueDrones[t].ChecksEnemyBorder(blueDrones, BLUEDRONES); blueDrones[t].EnemyMovement(blueDrones, BLUEDRONES); blueDrones[t].EnemyMoving(blueDrones, BLUEDRONES); } Draw(); } void Game::Draw() { //Draw blue drones int counter = 0; for(int i = 0; i < BLUEDRONES; i++)// row { if(blueDrones.isAlive) {blueDroneSpr->CopyTo(m_Screen, blueDrones.Xpos, blueDrones.Ypos);} if(blueDrones.enemyBullet->isFired) {bulletSpr->CopyTo( m_Screen, blueDrones.enemyBullet->xPos, blueDrones.enemyBullet->yPos);} counter++; } }
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!