xna-4.0


XNA choosing a subgroup of Game.Components


G'day All,
My little game has 5 bouncing balls and 1 player. Initially I wrote the code for the bouncing balls first and each ball has a collision detection method:
foreach (Bouncer bouncer in Game.Components) //For each bouncer component in the game...
{
if (bouncer != this)// Don't collide with myself
{
if (bouncer.collisionRectangle.Intersects(this.collisionRectangle))
{
// How far apart of the positions of the top right hand corners of the sprites when they hit?
int deltaX = Math.Abs((int)this.position.X - (int)bouncer.position.X);
int deltaY = Math.Abs((int)this.position.Y - (int)bouncer.position.Y);
// This is the width and height of a sprite so when two sprites touch this is how far the corners are from each other.
int targetWidth = 80;
int targetHeight = 80;
// The following determins the type of collision (vert hit vs horiz hit)
// Because the app is driven by a game based timer the actual amount of sprite overlap when the collision detection occurs is variable.
// This bit of simple logic has a 10 pixel tollerance for a hit.
// If target - delta is > 10 it will be interpreted as overlap in the non-colliding axis.
// If both if statements are triggered it is interpreted as a corner collision resulting in both sprites rebounding back along the original paths.
if (targetWidth - deltaX < 10) // The hit is a side on hit.
{
this.velocity.X *= -1;
}
if (targetHeight - deltaY < 10) // The hit is a vertical hit
{
this.velocity.Y *= -1;
}
this.numberOfCollisions = this.numberOfCollisions + 1;
}
}
}
base.Update(gameTime);
}
Then I added my player component and the wheels fell off. The app compiles OK but when I run it I get an InvalidCastException and the message:
Unable to cast object of type 'Bounce2.Player' to type 'Bounce2.Bouncer'.
I don't want to include the player object in this collision detector.
Is there a way I can enumerate my way through the Bouncer objects and exclude any other objects?
Thanks,
Andrew.
You can use this:
foreach (Bouncer bouncer in Game.Components.OfType<Bouncer>())
Note that you can store the Bouncer instances in other list too.

Related Links

How do I fix 'No suitable method found to override' error on XNA 4.0?
When to use PresentationParameters.BackBufferWidth vs .Viewport.Width
What does the pixel shader function tex1D do with a Texture2d
How to add a lap timer in XNA?
Is there a Texture1D in XNA?
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#

Categories

HOME
openlayers
netsuite
omnet++
urbancode
json.net
layout
lodash
office365api
dot
fme
youtube-dl
gis
setup-deployment
maven-3
spring-jdbc
amortized-analysis
malloc
jsrender
directx
autotools
slurm
elasticsearch-hadoop
try-catch
quartz-scheduler
uitypeeditor
dosgi
opentracing
libtiff
reactive-cocoa
lldb
swingx
autoconf
tar
graphicsmagick
google-search-api
captiveportal
crosstab
android-widget
sqlcipher
vlsi
greendao
fopen
kendo-datasource
google-cloud-nl
librato
socialengine
http-get
caret
asset-pipeline
xmlreader
rotational-matrices
revolution-slider
space-complexity
optix
ensembles
fakeiteasy
flashair
executenonquery
long-polling
termination
winscp-net
togetherjs
yii2-extension
lowpass-filter
slickedit
knockout-components
dtexec
capacity
eventkit
probability-density
fody
underscore.js-templating
nsfilemanager
lttng
nsviewcontroller
hill-climbing
make-install
dataview
energy
historian
separator
google-places
retina
computer-algebra-systems
mmc
notify
csquery
python-green
tablelayout
codeigniter-routing
picturefill
jmapviewer
ril
gwt-rpc
delphi-6
chuck
dotnethighcharts
code-cleanup
gnustep
h.323
gin
forums
perfect-hash
yagni
port-number

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