 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
Frostbyt3 Master Cheater
Reputation: 0
Joined: 07 Jan 2008 Posts: 323 Location: Australia
|
Posted: Fri Mar 13, 2009 7:40 pm Post subject: [C#] Xna Collision Problems |
|
|
I've recently had fa coding brainfreeze and need to get my collision system working within a week. I don't see whats wrong, but It's really pissing me off.
Scene2dNode | Code: | /// <summary>
/// A 2d object
/// </summary>
public class Scene2DNode
{
private Texture2D texture;
private Vector2 worldPosition;
/// <summary>
/// Gets or sets the position.
/// </summary>
/// <value>The position.</value>
public Vector2 Position
{
get { return worldPosition; }
set { worldPosition = value; }
}
public Texture2D Texture
{
get { return texture; }
}
/// <summary>
/// Initializes a new instance of the <see cref="Scene2DNode"/> class.
/// </summary>
/// <param name="texture">The texture.</param>
/// <param name="position">The position.</param>
public Scene2DNode(Texture2D texture, Vector2 position)
{
this.texture = texture;
this.worldPosition = position;
}
/// <summary>
/// Draws the specified renderer.
/// </summary>
/// <param name="renderer">The renderer.</param>
/// <param name="drawPosition">The draw position.</param>
public void Draw(SpriteBatch renderer, Vector2 drawPosition)
{
renderer.Draw(texture, drawPosition, Color.White);
}
public virtual void Update()
{
//Stub
}
|
Collidable2DNode | Code: | public class Collidable2DNode : Scene2DNode
{
bool isColliding;
Rectangle CollisionBounds;
public bool Colliding
{
get { return isColliding; }
set { isColliding = value; }
}
public Rectangle CollisionRectangle
{
get { return CollisionBounds; }
}
public Collidable2DNode(Texture2D text, Vector2 pos)
: base(text, pos)
{
CollisionBounds.Height = this.Texture.Height;
CollisionBounds.Width = this.Texture.Width;
}
public override void Update()
{
base.Update();
}
public bool CheckCollision(Rectangle toCheck)
{
if (CollisionBounds.Intersects(toCheck))
{
return true;
}
else
{
return false;
}
}
public void Move(Vector2 toMove)
{
this.Position += toMove;
}
} |
A test object for collision
| Code: | public class CollisionTestSquare : Collidable2DNode
{
public CollisionTestSquare(Texture2D text, Vector2 pos)
: base(text, pos)
{
}
public override void Update()
{
//Emulate gravity
this.Position -= new Vector2(0, -1);
base.Update();
}
} |
Releveant part of the Update Method | Code: |
sceneManager.ActiveScene.Update();
foreach (Collidable2DNode node in sceneManager.ActiveScene.sceneNodes)
{
//For every collidable node, check against every other node for collision.
node.Colliding = false;
//It's not colliding yet, cos we've not checked.
foreach (Collidable2DNode node2 in sceneManager.ActiveScene.sceneNodes)
{
//If colliding is false, check every node to see if it collides. If it does, colliding is true.
if (!node.Colliding)
{
//Don't check against yourself!
if (node != node2)
{
if (node.CheckCollision(node2.CollisionRectangle))
{
node.Colliding = true;
}
}
}
}
if (node.Colliding)
{
node.Move(new Vector2(0, 1));
}
node.Colliding = false;
} |
There are 3 Colliding nodes in the activeScene sceneNodes list.
Any help is highly appreciated, I don't know how I got stuck on such an easy problem.
[/code]
|
|
| Back to top |
|
 |
Odecey Master Cheater
Reputation: 1
Joined: 19 Apr 2007 Posts: 259 Location: Scandinavia
|
Posted: Sat Mar 14, 2009 10:07 am Post subject: |
|
|
1.Exactly how is it "not working"? Do you mean that the nodes do not react at all when colliding with eachother?
2. Why do you automatically move the node upwards upon collision? Isn't there any possibility for it to collide upwards or horisontally?
3.If you ever decide on having stronger "gravity", the node might end up passing right through the node it's colliding with, instead of resting right on top of it. That might actually happen with the current code if the node is permitted to move more than 1 unit per update. The reason for this is that you don't make sure that the new position the node is moved to upon collision isn't still colliding with the other node.
Edit:
4.I would suggest that you put the hittesting as it's own function and call it when you move the node. I feel that that would be a tidier way to do it anyway.
Hope this helps =).
_________________
Never confuse activity with productivity. You can be busy without a purpose, but what's the point?- Rick Warren |
|
| Back to top |
|
 |
Frostbyt3 Master Cheater
Reputation: 0
Joined: 07 Jan 2008 Posts: 323 Location: Australia
|
Posted: Sat Mar 14, 2009 4:44 pm Post subject: |
|
|
Solved my problem, but thankyou very much for the tips. The movedown and up parts are jsut temporary, I'll fix that up later.
The problem was I was drawing to the Vector2, not the rectangle. The rectangles just stayed at 0,0, and were not updated.
|
|
| Back to top |
|
 |
Spawnfestis GO Moderator
Reputation: 0
Joined: 02 Nov 2007 Posts: 1746 Location: Pakistan
|
Posted: Sat Mar 14, 2009 5:14 pm Post subject: |
|
|
| Frostbyt3 wrote: | Solved my problem, but thankyou very much for the tips. The movedown and up parts are jsut temporary, I'll fix that up later.
The problem was I was drawing to the Vector2, not the rectangle. The rectangles just stayed at 0,0, and were not updated. |
Hehe, common fault.
_________________
CLICK TO HAX MAPLESTORAY ^ !!!! |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|