Xbox LIVE Indie Games
Sort Discussions: Previous Discussion Next Discussion
Page 1 of 1 (18 posts)

Make sprite move without input

Last post 2/27/2009 8:09 AM by Yakra. 17 replies.
  • 2/19/2009 9:40 PM

    Make sprite move without input

    Hey guys I am very new to XNA..I have done some programming so I can understand most code that I read I just have problems writing it.  What I am trying to do is create a battle scene that is totally automated, pretty much it is a video and not a game.  I can do basic stuff like add a sprite to the screen and saw some code where I can make it bounce around the screen kind of like a screensaver.  I have not been able to find any code on how to move a sprite to specific points on the screen without input.  I am guessing it is some kind of big for loop or something like that.  Just wondering if anybody has any examples of how to do this or could just help me out at all.  Thanks in advance
  • 2/19/2009 9:44 PM In reply to

    Re: Make sprite move without input

    beb:
    Hey guys I am very new to XNA..I have done some programming so I can understand most code that I read I just have problems writing it.  What I am trying to do is create a battle scene that is totally automated, pretty much it is a video and not a game.  I can do basic stuff like add a sprite to the screen and saw some code where I can make it bounce around the screen kind of like a screensaver.  I have not been able to find any code on how to move a sprite to specific points on the screen without input.  I am guessing it is some kind of big for loop or something like that.  Just wondering if anybody has any examples of how to do this or could just help me out at all.  Thanks in advance

    You could programatically change the coordinates of the sprite in the update routine and then display the sprite at those coordinates in the render or draw routine. 
  • 2/19/2009 10:09 PM In reply to

    Re: Make sprite move without input

    Ok I understand what you are saying but do you know where there is an example of something like this somewhere on the internet.  Like I said I understand what you are talking about just dont know how to put it into code.
  • 2/19/2009 10:09 PM In reply to

    Re: Make sprite move without input

    Hey,

    There is a sample you can download under Artificial Intelligence for Waypoint Navigation.
    Maybe that's what you're looking for?

    // A
  • 2/20/2009 12:03 AM In reply to

    Re: Make sprite move without input

    Yes it kind of is but i'm not sure if its exactly what I am looking for.

    Pretty much what my "game" will do is have a menu pop up and when you click play a battle scene starts with all the sprites moving on their own and is totally non-interactive by the user.  Thats what I mean when I say it is more of a video.
  • 2/20/2009 12:26 AM In reply to

    Re: Make sprite move without input

    So... from the sound of things you already have an idea where the units are going. In the direction of the other faction. You could make the factions mingle a little bit more by assigning one faction with a sequential set of numbers and the other with a randomly assigned number from the same sequence and then loop through the sequence sending both units with the same number towards each other. See the super duper example battle drawing below:

     

    Team A    B  
    ++++++++++++++++  
       X 1    2 X  
       X 2\   5 X  
       X 3 \  4 X  
       X 4  \ 6 X  
       X 5   \3 X  
       X 6    1 X  
    ++++++++++++++++ 

     

  • 2/20/2009 2:10 AM In reply to

    Re: Make sprite move without input

    Yes that is it pretty much....now how do I put that into code so that they start on the sides and stop when they get near each other....that is the part I can not fiigure out.

    P.S. nice drawing.....lol
  • 2/20/2009 7:29 AM In reply to

    Re: Make sprite move without input

    Assuming the units travel at the same speed, they will reach each other by walking half of the distance shown in the picture by the diagonal line. But you could make it less than half like 0.45f so they get a little space between them rather than moving right on top of each other. For each unit pair, subtract unit1's position by unit2's position. This gives you the velocity unit2 would travel to be at unit1's position. Multiply that vector by 0.45f to calculate the meeting point. Inverse the vector, (that is attach a minus (-) to it, or multiply by -1) to get the vector relative to unit1.

    The worldspace coordinate for these two target positions will be unit1.Position + unit1.vectorToUnit2 and unit2.Position + unit2.vectorToUnit1. Move the units positions from their origin towards this position little by little over the course of the simulation using perhaps the waypoint navigation sample (if you want them to go in an indirect line) or by simply lerping/smoothstepping the origin position and the target position by time to go straight. Lerp is a direct interpolation between the two vectors, Smoothstep will round off the beginning and ending a little bit, which would cause the units to start slow, pick up speed, and then slow down again just before they stop.

    As for setting up the original positions, for each side you'll have to set up a starting position for the first unit in the line. Then in a for-loop you set each unit to that vector plus (i * UnitSpacing) on the Y coordinate, or X if you want horizontal lines.

    To permute the numbers you'll need to make use of the Random class. An algorithm to permute a set is fairly straightforward, but as you might have guessed from the codeless format of this post, I'd really like you to try it out the solution yourself before I dump an answer on you.
  • 2/23/2009 7:26 PM In reply to

    Re: Make sprite move without input

    ok so it has been quite a few days but I still can not figure it out.  I get what your talking about just dont know how to put it into code.  I am just really struggling with this.  Maybe if somebody could show me some code that is not exactly what I need but one where I would need to change it a little would greatly help.  I just have a hard time writing my own code with barely anything to go on.  Anyways thanks for all the help so far.
  • 2/23/2009 8:39 PM In reply to

    Re: Make sprite move without input

    It seems to me what you want to try and accomplish is pretty easy you just are having trouble knowing what your 'conditions' should be. Normally your condition is whether or not a direction is being held on the control now your condition just needs to be something else.

    You will need multiple conditions or just have a very detailed plan for the script of actions your sprites should take and when.

    you could use a timer like this.

    int timer;
    float velocity = 5.0f
    bool isAttacking;

    update:

    timer++;

    if(timer > 50 && !isAttacking)
    {
        sprite1.X += velocity;
    }
    this makes the sprite wait 50 ticks then start moving in the x direction constantly

    then do something like:

    float distance = Math.Abs(sprite1.X - sprite2.X);

    if (distance < 20)
    {
        isAttacking = true;
        //run the attack animation on sprite1 and sprite 2
    }
    else
    {
         isAttacking = false;
         //run the walk anim
    }
    this makes them start playing the attack anims when they are a certain distance and also stop moving
    now you'll want to check the distance between all opposing sprites with something like a foreach loop.

    Now just add onto that idea and I think you'll get what you are looking for. Remember it only needs to look like something is happening, fake it. You don't need to write some sorta full blown AI routine.

  • 2/25/2009 4:09 PM In reply to

    Re: Make sprite move without input

    Ok ok ok I am understanding this more now.....but one more question and you might think I am stupid but what is a bool.  I do not think I have ever dealt with one. 

    PS you can call me stupid if it is something that is easy
  • 2/25/2009 4:35 PM In reply to

    Re: Make sprite move without input

    bool is a boolean data type. Variables that are declared as bool can be true or false only and default to false when they're declared unless you specify otherwise.
  • 2/26/2009 9:05 PM In reply to

    Re: Make sprite move without input

    Ok so I tried the above code and changing some of it to match my code and I am getting the following errors and do not know how to fix them so that the code would still work.

    1. Operator += cannot be applied to sprite1 += velocity because of Texture2D and float
    2. Operator - cannot be applied to Math.abs(sprite1 - sprite2) because of types Texture2D and Texture2D
  • 2/26/2009 9:44 PM In reply to

    Re: Make sprite move without input

    You really truly need to take a course in programming, C#, or something a little more fundamental than XNA. Tackling both at the same time is like...

    Competing in a hot pepper eating contest when the spiciest thing you've ever eaten is a taco salad.

    Read the errors its giving you. Really read them. The first is trying to take a TEXTURE plus a NUMBER. Does that make sense? Second one is trying to subtract a TEXTURE from a TEXTURE. 
    For both of those math operations to work, you would work with all numbers. But even if you do that you've got something wrong in your logic because your velocity and distance calculations are one-dimensional and I'm pretty sure you're working in two, if not three dimensions for this problem.

    Your position, velocity, and direction variables should be of type Vector2 or Vector3, not float, and especially not Texture2D.
  • 2/27/2009 12:06 AM In reply to

    Re: Make sprite move without input

    ok ok ok I know I am an idiot but I have been looking at some tutorials as well as the above code and I only have one error left and am not sure how to fiix it or most  likely I am doing something stupid in my code again.


    but for the distance loop i have

    float distance = Math.abs(attacker.position - defender.position)

    XNA says cannot convert from Vector2 to sbyte.


    do I need to change float to vector2 to get this to work or have to do something totally different
  • 2/27/2009 3:07 AM In reply to

    Re: Make sprite move without input

    You need to use the X value of the position vector on your attacker/defender. Position itself is a Vector2 but the two values it contains are floats. Since distance is a float it will not take a Vector2.

    Additionally if you need to find the distance when the two objects are not on the same Y/X axis you'll need to use the distance formula which will take into account both the X and Y position of the objects:

    d = sqrt((x2-x1)2 + (y2-y1)2)

    Google distance formula if there is any confusion.
  • 2/27/2009 3:56 AM In reply to

    Re: Make sprite move without input

    Yakra:

    Additionally if you need to find the distance when the two objects are not on the same Y/X axis you'll need to use the distance formula which will take into account both the X and Y position of the objects:

    d = sqrt((x2-x1)2 + (y2-y1)2)

    Google distance formula if there is any confusion.


    While the pythagorean theorem is really important to understand as a building block to any kind of vector math, you don't need to write it out for every distance calculation. That's what the Length method is for.

    float distance = (attacker.position - defender.position).Length();
  • 2/27/2009 8:09 AM In reply to

    Re: Make sprite move without input

    Oh didn't know about that method. Helpful.
Page 1 of 1 (18 posts) Previous Discussion Next Discussion
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG