[XNA] Accounting for those few extra pixels
Hello all,
I am still working on a 2-d side scroller, tile based game. I have worked out some kinks between sprite collisions and tiles. However, I am still having a slight problem. Currently I take my sprites position, value of the leftthumbsticks x and y multiplied by the sprites speed (which is 3 for now), and assign to temporary position variables.
I use the temp variables to look up the Upper Left, Lower Left, Upper Right, and Lower Right cell(tile) values that the sprite may be moving into to check for collision with other objects(world based, like walls, doors, floors). The issue I am having is accounting for the fact that I have a speed value of 3. Let's say I am moving to the right, my X value for the sprite could be a value of 3. If the current position of the sprite, plus the 3 pixel move, indicates a cell that is not -1 the sprite will not move. If you are 3 pixels away, releasing the thumbstick and pressing right again eventually moves you directly against the other object, but it does so in a jerk kind of move. In other words, the next update kind of jerks the sprite to the right to account for the 1-3 pixels that actually still exist (it looks really stupid on screen I must say).
My question is how to account for the speed of a sprite to handle a collision that may be about to happen, but still has a few pixels before it happens, to account for the jerk into place that I am seeing?
Thanks for any help!
I'm not 100% sure what you're doing, but hopefully I understand enough.
You'd typically do all the moves before displaying. You can detect collisions then, and correct the positions as necessary, reverting to the previous position or moving back a little, depending on your desired effect. Making this look good would be a matter of tweaking.
You'd typically do all the moves before displaying. You can detect collisions then, and correct the positions as necessary, reverting to the previous position or moving back a little, depending on your desired effect. Making this look good would be a matter of tweaking.
It's hard to tell from your question, but I'm assuming what you're doing is something like this:
If Sprite Position < Limit
Move Sprite At Speed of Control Stick Offset
Else
Move Sprite Back To Limit
But if your speed is, say, 20, and your only 5 pixels away from the limit, while you're still technically not at the limit, and can still move, moving at your normal speed will result in moving past the limit, and in the next frame you will be moved back because you are at the limit. This is easily fixed by accounting for the movement speed. In this case we'll say that SpriteX/SpriteY/SpriteWidth/SpriteHeight is the bounding box of the sprite, RectX/RectY/RectWidth/RectHeight is the box you're colliding with, and SpeedX/SpeedY is your movement speed on both axes.
C/C++/C#
Visual Basic
If Sprite Position < Limit
Move Sprite At Speed of Control Stick Offset
Else
Move Sprite Back To Limit
But if your speed is, say, 20, and your only 5 pixels away from the limit, while you're still technically not at the limit, and can still move, moving at your normal speed will result in moving past the limit, and in the next frame you will be moved back because you are at the limit. This is easily fixed by accounting for the movement speed. In this case we'll say that SpriteX/SpriteY/SpriteWidth/SpriteHeight is the bounding box of the sprite, RectX/RectY/RectWidth/RectHeight is the box you're colliding with, and SpeedX/SpeedY is your movement speed on both axes.
C/C++/C#
//If the sprite is moving leftif (speedx < 0) { //If the sprite is further away from the box than it's movement speed if (spritex =< rectx - speedx) { spritex = spritex + speedx; } else { spritex = rectx - spritewidth; }//If the sprite is moving right} else if (speedx > 0) { //If the sprite is further away from the box than it's movement speed if (spritex => rectx + rectwidth - speedx) { spritex = spritex + speedx; } else { spritex = rectx + rectwidth; }}//If the sprite is moving upif (speedy < 0) { //If the sprite is further away from the box than it's movement speed if (spritey =< recty - speedy) { spritey = spritey + speedy; } else { spritey = recty - spriteheight; }//If the sprite is moving down} else if (speedx > 0) { //If the sprite is further away from the box than it's movement speed if (spritey => recty + rectheight - speedy) { spritey = spritey + speedy; } else { spritey = recty + rectheight; }}
Visual Basic
'If the sprite is moving leftIf Speed.X < 0 Then 'If the sprite is further away from the box than it's movement speed If Sprite.X =< Rect.X - Speed.X Then Sprite.X = Sprite.X + Speed.X Else Sprite.X = Rect.X - Sprite.Width End If'If the sprite is moving rightElseIf Speed.X > 0 Then 'If the sprite is further away from the box than it's movement speed If Sprite.X => Rect.X Rect.Width - Speed.X Then Sprite.X = Sprite.X + Speed.X Else Sprite.X = Rect.X + Rect.Width End IfEnd If'If the sprite is moving upIf Speed.Y < 0 Then 'If the sprite is further away from the box than it's movement speed If Sprite.Y =< Rect.Y - Speed.Y Then Sprite.Y = Sprite.Y + Speed.Y Else Sprite.Y = Rect.Y - Sprite.Height End If'If the sprite is moving downElseIf Speed.X > 0 Then 'If the sprite is further away from the box than it's movement speed If Sprite.Y => Rect.Y Rect.Height - Speed.Y Then Sprite.Y = Sprite.Y + Speed.Y Else Sprite.Y = Rect.Y + Rect.Height End IfEnd If
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement