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

Particle System: XNA Framework HiDef profile does not support alpha blending on Vector4 textures
How to unset texture data in XNA 4.0
Detecting a Key Press in XNA 4.0?
How to add a start (splash) screen in XNA 4.0?
My program is being reported as a high-level security threat by AVG?
How to add an end screen in XNA 4.0?
Texture2D is way oversized in XNA 4.0?
How to set xna's TextureFilter to Point
Why doesnt my xna pixel shader turn this texture blue?
How to calibrate kinect skeleton data to video feed?
XNA game studio4.0 using c#
How to add collision detection in a 2D car game in XNA?
Texturing a terrain causes Windows Phone to crash (without an exception) or Models are not drawn afterwards
compiling and deploy window mobile phone platform games into xbox360 game platform with XNA 4.0
'Expected class, delegate, enum, interface,or struct' in XNA 4.0?
Having errors running JigLibX 0.3.1 on XNA 4.0

Categories

HOME
sendgrid
zeromq
azure-data-factory
server
deezer
plone
view
iterator
relayjs
bpmn
analysis
c#-2.0
uber-api
multiple-records
contact
basic
offline
php-7.1
quickfix
clearcase-ucm
django-simple-history
nodatime
plunker
jquery-ajaxq
pass-by-reference
directx-10
google-cloud-endpoints-v2
google-qpx-express-api
fog
noraui
calibre
ghost4j
wallpaper
mixture-model
theano.scan
thin
executenonquery
scrollable
filepicker
multipeer-connectivity
setter
windows-iot-core-10
sqldf
smart-table
background-service
google-cdn
objective-c-swift-bridge
nested-sets
slicknav
multi-level
google-web-starter-kit
jwplayer7
angstrom-linux
blackberry-10
qgraphicsview
ptrace
t4mvc
wordml
android-listview
twirl
appfabric-cache
remobjects
key-management
graph-api-explorer
sorl-thumbnail
ng-animate
c3
code-access-security
xojo
wp-query
doskey
mysqltuner
geos
jsctypes
jquery-mobile-dialog
random-seed
sqlperformance
android-hardware
javaspaces
code-cleanup
punbb
gnustep
django-tagging
locationlistener
gdlib
sudzc
coda-slider
ncqrs
database-management
phonon
geneva-server
dbisam

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App