im trying to make Space Invaders as a first learning project. and got to the point where i have a moving player, some enemies and a bullet to shoot. however, the bullet’s hitbox does not align with its texture.
i dont know what to do with it anymore. and ive come here with good hopes that anyone here can help me
here is a screenshot of what i mean
How do you know it’s not aligned?
Maybe draw a rectangle with the same location as the hitbox and that might reveal something.
Other than that, post the update / draw code in here and some folks might be able to spot something.
Hey Jonathan, its drawing the texture at the exact location of the hitbox
how i call draw
where it moves
at at last where it checks colision with the enemies ( this one might be inefficent tho lol )
So spritebatch.draw has a number of different overloads.
One of them takes a rectangle for a location to draw. Another one takes a vector2 (its recommended to use a vector for more smooth precise drawing because it uses floats)
One of the overloads for drawing with a vector2 is the origin of the sprite your drawing as a vector. Try and set the origin of the sprite to a vector of half the size of your sprite
okay, i shall convert the draw to use the position Vector.
how would i go about setting a sprites origin? i havent figured that out yet.
I think what boot is saying is, the Hitbox rectangle is relative to where the GameObject is.
So if your Hitbox is 32x32 then it’s going to be:
X = GameObject.X
Y = GameObject.Y
Width = 32 Height= 32
This means in your Update() you need to keep updating the Hitbox (X,Y) as each GameObject moves.
I can’t see from the code you posted whether you are doing that or not (it should be in the Update()).
It looks like you are drawing each bullet into its own texture at position 0,0 (guessing).
But what’s important is the location where you draw the textures later on the game screen.
This location is the topleft (x,y) of your hitbox.
ive learned that this sould be enough to keep the hitbox at the game object
might be wrong about that tho…