0
\$\begingroup\$

I am building a game in XNA 4.0, where a player moves about a 2 dimensional (vertical perspective) map consisting of blocks. My issue is creating proper collision between the payer and the blocks (basic game physics.) The player moves more than 1px per frame, so .Intersects() just isn't enough, I need physical contact collision that can function in a gravity environment. The current version I currently have is a piece of garbage and only works occasionally.

Basically, all that the collision system needs to do is stop gravity when the player lands on a block, and provide some decent physics when the player hits blocks (movement in that direction ceases). The idea behind my current solution is to move the next Position around until it finds a clear spot, but it doesn't work well. I have an idea why, just have no idea how to do it properly.

I know there must be a better way to do this. What would be the best method of making this kind of collision work properly?

Thanks

\$\endgroup\$
2
  • \$\begingroup\$ The way you've presented this question now really looks like a "give me the code, please" sort of question. I think there's a good question in here, but you need to clean up how you are asking it. Your current code is probably not useful and can be edited away -- can you instead describe more specifically how you want the collision to behave, and what the constraints on the player's motion currently are? For example, your game sounds like a side-scrolling game, so can players jump "up through" certain blocks but not fall back down through them? \$\endgroup\$
    – user1430
    Oct 27 '14 at 2:44
  • \$\begingroup\$ Revised the question. Sorry about that, just a bit frustrated with the problem :) \$\endgroup\$
    – macuser47
    Oct 27 '14 at 2:55
0
\$\begingroup\$

A couple of things I have found that have helped players in the past with collision : Prevent the game object from moving before it collides.

By checking for a collision, and preventing movement, you won't have to worry about a game object lapping into another. When your collision check is flagged (game object will hit another game object) you then handle the set of instances that can occur. In this case it sounds like the desired effect would be the character landing on a block.

When your collision check is flagged, initialize your character's y location to be on the said block, creating the appearance of him landing on the block, and preventing him from passing into the block (flagging the collision check to lock him in place). Once this is achieved you can set a grounded property to true on the game object, to indicate that gravity does not apply to it until another scenario sets his grounded property to false.

There are many collision detection examples on stack, and plenty of tutorials out there, but if you need a code base to pull from, feel free to use my code as a reference : https://github.com/shroeder/GameRPG/tree/LukesBranch/Game/Game. Collision detection can be found by searching for //character movement in the game class.

\$\endgroup\$
1
  • \$\begingroup\$ Used this: Link and it worked very nicely; Yes I know to prevent it from moving before it collides, the issue was how to properly respond when overlap occurs, because rough collision avoidance would not work well for the game I am making. \$\endgroup\$
    – macuser47
    Nov 1 '14 at 21:27
0
\$\begingroup\$

This does the job perfectly, you just need to sort through a few syntax errors. http://go.colorize.net/xna/2d_collision_response_xna/

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.