xna-4.0


XNA: Identifying identical sprites created with for loop


G'day all,
In short, I'm using a for loop to create a bunch of identical sprites that I want to bounce around the screen. The problem is how do I write a collision detection process for the sprites. I have used the process of placing rectangles around sprites and using the .intersects method for rectangles but in that case I created each sprite separately and could identify each one uniquely. Now I have a bunch of sprites but no apparent way to pick one from another.
In detail, if I create an object called Bouncer.cs and give it the movement instructions in it's update() method then create a bunch of sprites using this in Game.cs:
for (int i = 1; i < 5; ++i)
{
Vector2 position = new Vector2(i * 50, i * 50);
Vector2 direction = new Vector2(i * 10, i * 10);
Vector2 velocity = new Vector2(10);
Components.Add(new Bouncer(this, position, direction, velocity, i));
}
base.Initialize();
I can draw a rectangle around each one using:
foreach (Bouncer component1 in Components)
{
Bouncer thing = (Bouncer)component1;
Rectangle thingRectangle;
thingRectangle = new Rectangle((int)thing.position.X, (int)thing.position.Y, thing.sprite.Width, thing.sprite.Height);
But now, how do I check for a collision? I can hardly use:
if (thingRectangle.Intersects(thingRectangle))
I should point out I'm a teacher by trade and play with coding to keep my brain from turning to mush. Recently I have been working with Python and with Python I could just put all the sprites into a list:
sprites[];
Then I could simply refer to each as sprite[1] or sprite[2] or whatever its index in the list is. Does XNA have something like this?
Please let me know if any more code needs to be posted.
Thanks,
Andrew.
One solution, which I use in my game engine, is to have a Logic code run inside the objects for every game Update, ie. every frame. It seems you already do this, according to the variable names, which indicate you run some physics code in the objects to update their positions.
You might also want to create the collision rectangle inside the Bouncer's constructor so it's more accessible and you make good use of object oriented programming, maybe even make it an accessor, so you can make it update every time you call it instead of manually updating the bounding/collision box. For example:
public Rectangle #BoundingBox {
get { return new Rectangle(_Position.X, _Position.Y, width, height); }
}
Whichever way works, but the collision checks can be run inside the Bouncer object. You can either make the reference list of the Bouncer objects static or pass it to the objects itself. The code for collisions is very simply:
foreach(Bouncer bouncer in Components) //Components can be a static List or you can pass it on in the constructor of the Bouncer object
{
if (bouncer.BoundingBox.Intersects(this.BoundingBox))
{
//they collided
}
}

Related Links

Xna Game referencing Game in update
XNA game studio simple sprite animation
XNA phase management
How to check BoundingSphere for one ModelBone?
MonoGame: Some VertexColorPosition dissappear while drawing user primitives(DrawUserPrimitives)
Testing XNA 4.0 games On the XBOX 360 without paying $100 [duplicate]
Detecting clicks in a non-rectangular shape
What's a good way to optimise rendering a 2D tile game in XNA?
How to make 3D racing game with XNA? [closed]
How to correctly accelerate a rotated 3d model in XNA?
What's the difference between world and view for purpose of camera?
Vertexbuffer Getdata VertexPositionNormalTexture
Converting from 3.1 to 4.0… gamestates
XNA large coordinates and float precision
Trying to make Gaussian Blur stronger/blurrier - XNA 4.0 HLSL
XNA, EmguCV and perfomance issue with webcam

Categories

HOME
ms-access
vim
wso2
netsuite
path-finding
tesseract
sd-card
android-4.4-kitkat
setup-deployment
spring-jdbc
leon
c#-2.0
serverspec
paradox
node-notifier
user-input
floating-action-button
quickbooks
timeout
moonmail
size
libtiff
progressive-web-apps
finite-automata
interop
plunker
paging
physics-engine
mps
chromium-embedded
microsoft-r
autosys
pepper
dynamic-featured-image
brunch
nhapi
strncpy
log4js-node
msys2
iframe-resizer
.net-4.0
noraui
azure-sql-database
hexo
fusionpbx
overriding
xcode-extension
libvpx
sharefile
reportbuilder
azure-ml
lumberjack
user-controls
core-plot
estimote
outlook-api
acoustics
slick-3.0
angular-resource
avconv
upstart
errordocument
hls.js
gcsfuse
color-profile
angular-strap
time-and-attendance
visual-c++-2008
rotativa
make-install
suffix-tree
design-by-contract
freelancer.com-api
qtableview
jms2
fouc
citrus-pay
ionic
event-bubbling
system32
dereference
modalpopup
nstableviewcell
applescript-objc
navigationservice
elliptic-curve
system.net.webexception
valuechangelistener
datagridviewcolumn
mcts
html5-notifications
gnu-smalltalk
jsctypes
tidy
free-variable
browser-detection
sublist
broken-links
javaspaces
ticoredatasync
buildr
work-stealing
zpt
coda-slider
infrastructure
meego
ctp4
avatar
sector
private-members
genealogy
lzh

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App