Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>XNA already have a <code>Rectangle</code> class with a method to return if a rectangle collide with another rectangle.</p> <p>An exemple:</p> <pre><code>Rectangle rec1 = new Rectangle(0, 0, 10, 10); Rectangle rec2 = new Rectangle(5, 5, 10, 10); rec1.Intersects(rec2); //Return true if a rectangle intersects another one </code></pre> <p>What people normally do, is a <code>List</code> of <code>Rectangle</code> and test one by one to know if they collide, or they implements a <a href="http://en.wikipedia.org/wiki/Quadtree" rel="nofollow"><code>QuadTree</code></a> but that is a bit more difficult in the beginning.</p> <p>An exemple of a <code>List</code> of <code>Rectangle</code>:</p> <pre><code>List&lt;Rectangle&gt; allObjects = new List&lt;Rectangle&gt;(); Rectangle rec1 = new Rectangle(0, 0, 10, 10); Rectangle rec2 = new Rectangle(10, 15, 10, 10); Rectangle mainCharRec = new Rectangle(10, 20, 10, 10); allObjects.Add(rec1); allObjects.Add(rec2); </code></pre> <p>To test if main character rectangle intersects some other rectangles you can do a method with a foreach like this:</p> <pre><code>foreach (Rectangle rec in allObjects) if(mainCharRec.Intersects(rec)) return true; return false; </code></pre> <p>About getting slower, I think you could have a rectangle to the object and another to the "close region", and when the character enter in the region you slow his speed by the distance you give to the region, for example, if you have a rectangle with 10 pixels larger for each side, when character collides with larger rectangle he loses spriteSpeed/10 for step, until he stops. When the value is too much low, it turn 0:</p> <pre><code>if (spriteSpeed &lt; 0.5) spriteSpeed = 0; else spriteSpeed -= spriteSpeed/10; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload