Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7789719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:23:03+00:00 2026-06-01T21:23:03+00:00

Hello I am new to XNA and trying to develop a game prototype where

  • 0

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 32×32 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;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T21:23:05+00:00Added an answer on June 1, 2026 at 9:23 pm
    1. You need the initial postion too, not only direction
    2. Maybe you need more resolution
    3. ¿what? remove the “false” evaluation
    4. 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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I am new to XNA and Now I am developing a game. For
hello I'm new to PHP programming and I migrated from ASP .net to PHP..
Hello I'm new to ontologies, my assignment is to create an ontology using Protege
Hello I am new at Haskell and i'm having problems trying to get this
Hello I am new to Git and trying to setup server part and client
hello im new and learning javascript. I'm trying to make a program of addition
Hello im new to programming, im trying to construct my first simple application, im
class Hello @hello = hello def display puts @hello end end h = Hello.new
hello im new to python and have been reading over the documentation and am
Hello.Am new to ios development.What have to do for NSOpenPanel to work in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.