xna-4.0


XNA Vector2 path contained inside rectangle


Hello I am new to XNA and trying to develop a game prototype where the character moves from one location to another using mouse clicks.
I have a Rectangle representing the current position. I get the target location as a Vector2 using player mouse input. I extract the direction vector from the source to the target by Vector2 subtraction.
//the cursor's coordinates should be the center of the target position
float x = mouseState.X - this.position.Width / 2;
float y = mouseState.Y - this.position.Height / 2;
Vector2 targetVector = new Vector2(x, y);
Vector2 dir = (targetVector - this.Center); //vector from source center to target
//center
I represent the world using a tile map, every cell is 32x32 pixels.
int tileMap[,];
What I want to do is check whether the direction vector above passes through any blue tiles on the map. A blue tile is equal 1 on the map.
I am not sure how to do this. I thought about using linear line equation and trigonometric formulas but I'm finding it hard to implement. I've tried normalizing the vector and multiplying by 32 to get 32 pixel length intervals along the path of the vector but it doesn't seem to work. Can anyone tell me if there's anything wrong in it, or another way to solve this problem? Thanks
//collision with blue wall. Returns point of impact
private bool CheckCollisionWithBlue(Vector2 dir)
{
int num = Worldmap.size; //32
int i = 0;
int intervals = (int)(dir.Length() / num + 1); //the number of 32-pixel length
//inervals on the vector, with an edge
Vector2 unit = Vector2.Normalize(dir) * num; //a vector of length 32 in the same
//direction as dir.
Vector2 v = unit;
while (i <= intervals & false)
{
int x = (int)(v.X / num);
int y = (int)(v.Y / num);
int type = Worldmap.getType(y, x);
if (type == 1) //blue tile
{
return true;
}
else
{
i++;
v = unit * i;
}
}
return false;
}
You need the initial postion too, not only direction
Maybe you need more resolution
┬┐what? remove the "false" evaluation
The calcs for next pos are a bit complicated
private bool CheckCollisionWithBlue(Vector2 source, Vector2 dir)
{
int num = 8; // pixel blocks of 8
int i = 0;
int intervals = (int)(dir.Length() / num);
Vector2 step = Vector2.Normalize(dir)*num;
while (i <= intervals)
{
int x = (int)(source.X);
int y = (int)(source.Y);
int type = Worldmap.getType(y, x);
if (type == 1) //blue tile
{
return true;
}
else
{
i++;
source+=step;
}
}
return false;
}
This will improve something your code, but maybe innacurate... it depends on what are you trying to do...
You maybe can find interesting the bresenham's line algorithm http://en.wikipedia.org/wiki/Bresenham's_line_algorithm
You should realize that you are not doing a volume collision but a line collision, if the ship or character or whatever that is at source position maybe you have to add more calcs

Related Links

Why my primitives are drawn on top of each other instead inside each other?
XNA 4.0 Spritbatch - render target must not be set on the device
Health Bar in XNA
Xact Visual Studio 2013
XNA 3D object is view as Correctly
Creating RenderTarget2D on runtime makes my screen flash in purple
Having FPS lags when drawing tiles in XNA
Basic Primitive - TriangleStrip - Monogame
XNA 4.0: terrain glitches caused by SpriteBatch
XNA 4.0 contentloadexception was unhandled
XNA 4 VertexPositionTexture triangleStrip sporadically disappearing
Simple way of getting BoundingBox?
XNA Sprite collision class partly not working
How do you publish so it works on other computers?
Simple 3D Collision guide please
XNA 4.0 InvalidOperationException was unhandeled

Categories

HOME
date
urbancode
oracle11g
react-router
xmpp
adb
tizen-web-app
rubygems
jxls
sql-server-2016
append
facebook-messenger-bot
zend-framework3
size
clojurescript
reactive-cocoa
viewport
autocad-plugin
one-hot-encoding
csrf-protection
dcevm
chromium-embedded
emgucv
p-value
numerical-methods
javacv
subdomains
primitive
quote
google-cloud-nl
fabric8
copying
normal-distribution
catalog
simplexml
withings
android-browser
password-encryption
turbogears
sencha-touch-2.3
return-value
iso8601
abcpdf9
rotativa
asp.net-4.5
py2app
etsy
bstr
react-native-listview
pagedlist
essence
pickadate
dlna
phpcas
teamcity-8.0
atk4
libressl
computer-algebra-systems
wyam
varargs
microbenchmark
ios9.1
angular-local-storage
inmobi
kineticjs
socketexception
code-access-security
bitcoinj
has-many-through
typo3-neos
viadeo
blending
ora-00911
dataadapter
flashvars
trusted
gdata-api
jspinclude
spring-portlet-mvc
subscript
hirefire
propagation
pyinotify
blitz++
paster
brewmp
procedural-music

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