collision detection for template

Post Reply
shinysup
Posts: 4
Joined: Fri Aug 12, 2011 6:03 pm

collision detection for template

Post by shinysup » Thu Aug 18, 2011 2:04 pm

Hi,
i'm referring to Patater's manual to learn nds programming,
and I need to implement a little collision detection.

I have a ship whose position is given by a template as following:
template <class T>
struct MathVector2D {
T x;
T y;
};
Ship::Ship(SpriteInfo * _spriteInfo) {
................
MathVector2D<float> position;
/* Place the ship in an interesting part of the screen. */
position.x = SCREEN_WIDTH / 2 - spriteInfo->width * 2 +
spriteInfo->width / 2;
position.y = SCREEN_HEIGHT / 2 - spriteInfo->height;

....
}



I need to make something happen once the ship reaches a certain area of the screen,
using conditionals like this, for example:
if((ship->position.x>SCREEN_WIDTH / 3)&&(ship->position.x<SCREEN_WIDTH / 2 )&&(ship->position.y>SCREEN_HEIGHT / 3)&&(ship->position.y<SCREEN_HEIGHT / 2)){...}

but then since the positions are template, its number are in different scheme, and are not stable.
Thus, the conditions don't work...

Is there anyway I could read the sprite's ACTUAL position info on the screen?

Please help me out.
And thanks in advance.

mtheall
Posts: 211
Joined: Thu Feb 03, 2011 10:47 pm

Re: collision detection for template

Post by mtheall » Thu Aug 18, 2011 10:40 pm

For one thing, you should avoid using floats because the NDS does not have a floating point unit, thus all float operations are really emulated. Next, using a templated coordinate struct is a bit overkill for just storing your XY position. Finally, I have no idea why you think using a templated struct/class makes the values it contains unstable.

The conditions you show seem perfectly valid to me. You must be missing a part of the picture if it's not working. The first thing that comes to mind is this code block you show:

Code: Select all

Ship::Ship(SpriteInfo * _spriteInfo) {
................
MathVector2D<float> position;
/* Place the ship in an interesting part of the screen. */
position.x = SCREEN_WIDTH / 2 - spriteInfo->width * 2 +
spriteInfo->width / 2;
position.y = SCREEN_HEIGHT / 2 - spriteInfo->height;
....
}
Why are you declaring "MathVector2D<float> position" again? This hides the class's 'position' member, and therefore the subsequent assignments essentially have no effect. How are you updating the ship's position? The answer to this question might show why your conditions fail.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests