Sign in to follow this  
hellaeon

Direct x 9.0c a shader approach

Recommended Posts

Goodaye guys, I am currently reading this book and I am at chapter 5. I dont expeect you all to know about it, but for those who have it, there is the reference. I am wondering some thoughts on implimenting a simple alorgithm to detect sprite bullet collisions with another object: If both are looped via iterator lists I need to remove the bullet and the object it hits I assume to use the distance formula (simple sphere collision) void SpriteDemo::updateBullets(float dt) { .... other code ... // Now loop through each bullet, and update its position. std::list<BulletInfo>::iterator i = mBulletList.begin(); while( i != mBulletList.end() ) { // Accumualte the time the bullet has lived. i->life += dt; // If the bullet has lived for two seconds, kill it. By now the // bullet should have flown off the screen and cannot be seen. ////////////////// MY COLLISION CHECK ////////////// // For each Asteroid... std::list<AsteroidInfo>::iterator j = mAsteroidList.begin(); while( j != mAsteroidList.end() ) { // return sqrt ( (x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2) if ( (float)sqrt( pow(i->pos.x - j->pos.x,2) + pow(i->pos.y - j->pos.y,2) + pow(i->pos.z - j->pos.z,2)) <20) { i = mBulletList.erase(i); j = mAsteroidList.erase(j); j = mAsteroidList.end(); } ++j; } ////////////////// END OF MY COLLISION CHECK ////////////// if(i->life >= 1.0f) i = mBulletList.erase(i); // Otherwise, update its position by moving along its directional ... update position code ... } Any thoughts on my brief post? Each time I run it, it crashes, I cant seem to debug, it hangs. This is fairly brief, I am unsure what else to put. The original code that works without the checking is of course fine: void SpriteDemo::updateBullets(float dt) { // Make static so that its value persists across function calls. static float fireDelay = 0.0f; // Accumulate time. fireDelay += dt; // Did the user press the spacebar key and has 0.1 seconds passed? // We can only fire one bullet every 0.1 seconds. If we do not // put this delay in, the ship will fire bullets way too fast. if( gDInput->keyDown(DIK_SPACE) && fireDelay > 0.1f ) { BulletInfo bullet; // Remember the ship is always drawn at the center of the window-- // the origin. Therefore, bullets originate from the origin. bullet.pos = mShipPos; // The bullets rotation should match the ship's rotating at the // instant it is fired. bullet.rotation = mShipRotation; // Bullet just born. bullet.life = 0.0f; // Add the bullet to the list. mBulletList.push_back(bullet); // A bullet was just fired, so reset the fire delay. fireDelay = 0.0f; } // Now loop through each bullet, and update its position. std::list<BulletInfo>::iterator i = mBulletList.begin(); while( i != mBulletList.end() ) { // Accumualte the time the bullet has lived. i->life += dt; // If the bullet has lived for two seconds, kill it. By now the // bullet should have flown off the screen and cannot be seen. if(i->life >= 1.0f) i = mBulletList.erase(i); // Otherwise, update its position by moving along its directional // path. Code similar to how we move the ship--but no drag. else { D3DXVECTOR3 dir(-sinf(i->rotation), cosf(i->rotation), 0.0f); i->pos += dir * BULLET_SPEED * dt; if ( i->pos.x < -900 ) { i->pos.x = 900; } else if ( i->pos.x > 900 ) { i->pos.x = -900; } if ( i->pos.y < -675 ) { i->pos.y = 675; } else if ( i->pos.y > 675 ) { i->pos.y = -675; } ++i; } Its obivous my style of code in removing items is poorly built....any clues? Cheers

Share this post


Link to post
Share on other sites
j = mAsteroidList.end();

What is this line for? If it's to exit the loop, just use break, as that's what it's meant for.

Also, it doesn't make sense to increment the bullet's life before the collision check and test it after the collision check - at that point, i might no longer be valid.

I think the collision check belongs after the bullet's position is updated (i.e., at the end of the loop), but I'm not sure. My brain is kind of fried right now.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this