Jump to content
  • Advertisement

Waylander

Member
  • Content Count

    25
  • Joined

  • Last visited

Community Reputation

136 Neutral

About Waylander

  • Rank
    Member

Personal Information

  1. Waylander

    Need help with 2D oscillation

    Waterlimon, thank you for the definitions!  Short and to the point.  Your mention of dt is what I was thinking of when I was asking Buckeye earlier whether or not the time had to be calculated in some way before being used (and he said it didn't and that the gameTime could just be fed in there).  I just didn't know what to call it.  I'd seen it before, but just couldn't remember.   I'll see about implementing your version of this and see what I can come up with .  As far as dt, I'm assuming that it is simply a case of storing a time variable and using subtraction to get what I need.  I'm still getting a feel for how sprites move this way whereas before what I was doing was simply updating a vector by a certain amount every frame by adding or subtracting and not messing with anything like direction, velocity, etc.   Buckeye, I know what perpendicular is.  What is throwing me off is when the terminology changes (telling me to use a direction, then telling me to use a speed, then telling me I need to use a direction again...things like that slow down my attempts at understanding what is going on).  On the plus side, since I've had to (involuntarily) take a day or so away from this, I think I can look at it with a fresh set of eyes.   Up to this point, for whatever reason my sprite is being kept at the upper left-hand corner of the screen (0, 0) and isn't moving.   EDIT   I think I'm getting somewhere.  My sprite is moving now but at a VERY slow rate.  I'm not sure why and no matter what I try to adjust it doesn't get any faster.  On the plus side it is going side to side a tiny bit (I changed my amplitude and frequency to 0.5 per Buckeye's suggestion) :).  Any suggestions on a range of numbers to work with for this sort of thing?
  2. Waylander

    Need help with 2D oscillation

    In your first post, you put:     I'm trying to see how that can be incorporated into what I am doing.   You lost me when you said "GetDirection() isn't perpendicular to GetDirection()."  If you are talking about the GetDirection() inside the Normalize function, that is just the latest version of what I tried.  In other attempts, I made the X negative per Alvaro's example.   Here is another way I tried that particular part of the formula: Vector2 perpVector = new Vector2((-sprites.GetDirection().X), (sprites.GetDirection().Y)) / sprites.GetDirection().Length();   When I had that, I replaced the Vector2.Normalize line with that perpVector with no success.   I removed GetSpeed() and replaced it with a hard-coded number so I could directly control what is going into it.  I plan to put GetSpeed() back once I get this figured out.   Which reminds me...your other post talks about a direction vector.  Do I need that or what I was using for speed?  My "speed" was how I was moving my sprites before by adding them to their position to update them.  So that's what I'm iffy about now with all this terminology that is confusing to me...what am I supposed to be working with in Alvaro's formula?  He mentions speed and then you mentioned a UnitDirectionalVector.  Then you mentioned that if GetSpeed() is a vector that I need to be using that.  I'm just needing to be sure what I'm supposed to be using.
  3. Waylander

    Need help with 2D oscillation

    Ok, I think I'm getting somewhere.  I was already using the "gameTime" but was unsure (like I mentioned) if that was correct which is why I asked about it.   I've been doing some searching on working with directional vectors and have been attempting to adapt my code to it.  I've been successful in getting my sprites to move in a straight line but I still need to tweak a few things.  Before you said something about picking a point and going to it.  That wasn't something I was doing but I'm attempting to do it now.  I've altered my code to create two random points...first is where to start at and the second is the destination.   Now I'm trying to see why my code isn't updating my sprite's position.  Here is what I have now:   sprites.SetPosition(sprites.GetOriginalPosition() + // ORIGINAL POSITION new Vector2(10, 0) * gameTime.ElapsedGameTime.Seconds + // GAMETIME Vector2.Normalize(sprites.GetDirection()) * // ATTEMPT AT USING XNA TO NORMALIZE VECTOR 5 * // AMPLITUDE (float)Math.Sin(gameTime.ElapsedGameTime.Seconds * // SIN(GAMETIME * FREQUENCY 5)); // FREQUENCY           Ok, I've been trying to go through what you told me about Alvaro's formula step by step but it isn't updating my sprite's position.  So here it goes:   You said my call to my sprite's original position looked ok.   The next item, the Vector2(10, 0) was placed in there to match Alvaro's mention of the (speed, 0) vector.  So I'm just plugging in a number here to try to get it to work.   Next is the call to the elapsed time, just as you said.   I'm not positive, but I'm thinking that this step, creating a perpendicular vector and normalizing it, is what is causing me trouble.  I've tried doing this a number of ways.  The code above is an attempt to use XNA's built-in normalization function.  I've also tried it manually by creating a vector, placing the directional vector in it, making the X part of it negative (per Alvaro's post) and then dividing it by its length.  Then I tried plugging this into that formula with no effect.  This brings me to another question that I am fuzzy on...what needs to be normalized here?  The speed or the direction?   Last is the amplitude of 5, the gameTime again, and then the frequency of 5 which you said was ok.   I also have a question about getting the direction.  The code I've found online has it updating along with the sprite's position.  I think this is because they were making an enemy follow the player and since the player would change direction, then the enemy would need to as well.  I've kept the direction updating anyway even though I don't think I need it.  I figure it isn't hurting anything right now since I'm trying to get this to work.  But once I get it working, I think it will be sufficient to set the direction once since the destination won't be moving.   I've been working on this off and on all day and my mind is full of jumbled code and all the various permutations I've tried.  Does anything stand out to you?   I'm not worried about the "tough love" :).  I am well aware that I need to learn more.  What is sad is that I have recently completed an online game math course and even came out of it with a respectable grade.  Yet I still can't do something like this which is, as you say, basic math.  If nothing else, hopefully the course will have planted a few seeds in my mind that will grow as I continue to try to learn.
  4. Waylander

    Need help with 2D oscillation

    I know it is in seconds.  I guess what I am asking is how is "t" calculated before it is plugged into that formula?  I think that might be the last sticking point for me.
  5. Waylander

    Need help with 2D oscillation

    I'll look into the phrases you suggested.  I'm going to keep studying but learning alone means I often miss some very basic stuff that would make the rest of it so much easier.  But I'll keep at it...I'm stubborn that way :).   All the code I posted is mine.  So that GetSpeed() function does return a Vector2.  It is from the sprite class I made.   All that aside and since you know what I am trying to do, how would you go about doing that?  If I can figure out all the "pieces" that are needed then I can hopefully alter it to work in XNA.  For example, how would you get the time so you can use it for something like this?  I'm thinking I won't need specific code examples since you said you're not familiar with what I am working in but if I know what needs to be done, then perhaps I can code it myself.
  6. Waylander

    Need help with 2D oscillation

    Yes, that was a coding error and should have been addition.   That ElapsedGameTime.Milliseconds returns milliseconds.  I changed it to ElapsedGameTime.Seconds.  I had no idea that seconds were needed rather than milliseconds.  Indeed, I just plugged that in to experiment.  I had no idea what exactly belonged there.  It still seems odd to have it simply return seconds.  It might be my ignorance but my gut tells me that something needs to be done to count those seconds rather than just return an arbitrary number of seconds.   I honestly couldn't tell you if GetSpeed() returns a vector or scalar since I'm not sure about the terminology.  Does this help?   public     Vector2 GetSpeed() {     return m_Speed; }   What exactly is a UnitDirectionVector that is needed?  You said it is a unit vector pointing in the direction the sprite is to travel.  Would that not be what is determined via the SetSpeed function?  Or am I way off base here?   As far as -Vector2.Normalize, as I understand it, when Alvaro mentioned dividing a vector by its length to give it a length of one, I thought that was normalizing a vector.  So with that thought in mind, I went for the Normalize.   The mention of something depending on speed was my attempt at (perp.x, perp.y).  I didn't know how to go about that or (v.x, v.y) beyond trying to plug in the speed.   I have been studying up on this kind of math but being self-taught at this, I often don't know what to ask or how to ask it.  Or for that matter, what to look up for this sort of thing.  Dealing with the frustration of trying to hunt this stuff down is what caused me to post here.  Sure, it may be basic math if you are used to it but it won't be basic to me until I can get some explanations that tackle what I need head-on rather than just abstract concepts from a textbook (which this post is heading towards :) ).
  7. Waylander

    Need help with 2D oscillation

    Here is my function for basic, straight-line movement.       // Straight line behavior     private void MoveSprites() {     for (int i = 0; i < sprites.Count(); i++) {     // Update position of sprite sprites.SetPosition(sprites.GetPosition() + sprites.GetSpeed());     // If the sprite moves off the screen, use it again somewhere else.     if (sprites.GetPosition().X > screen.Width + 100 || sprites.GetPosition().X < -100 || sprites.GetPosition().Y > screen.Height + 100 || sprites.GetPosition().Y < -100) {       switch (rand.Next(4)) {     // TOP     case 0: sprites.SetPosition(   new Vector2(rand.Next(screen.Width), -64)); sprites.SetSpeed(   new Vector2(rand.Next(-5, 5), rand.Next(minSpriteSpeed, maxSpriteSpeed)));     break;     // BOTTOM     case 1: sprites.SetPosition(   new Vector2(rand.Next(screen.Width), screen.Height + 64)); sprites.SetSpeed(   new Vector2(rand.Next(-5, 5), rand.Next(-maxSpriteSpeed, -minSpriteSpeed)));     break;     // LEFT     case 2: sprites.SetPosition(   new Vector2(-64, rand.Next(screen.Height))); sprites.SetSpeed(   new Vector2(rand.Next(minSpriteSpeed, maxSpriteSpeed), rand.Next(-5, 5)));     break;     // RIGHT     case 3: sprites.SetPosition(   new Vector2(screen.Width + 64, rand.Next(screen.Height))); sprites.SetSpeed(   new Vector2(rand.Next(-maxSpriteSpeed, -minSpriteSpeed), rand.Next(-5, 5)));     break; } } } }         When a sprite moves off-screen, it is randomly placed on the outside of the screen and given a vector based on which side it is going to be coming from.  Since these vectors are random, I am having a hard time getting the "waves" to work.   Here we have the code to move my sprites:   sprites.SetPosition(sprites.GetPosition() + sprites.GetSpeed());   Here is one of my attempts:   sprites.SetPositionY(sprites.GetPosition().Y + (-(float)Math.Cos(sprites.GetPosition().X / 100) * 10));   This produces some odd behavior which I actually like and may try to use elsewhere but it isn't what I'm trying to do here.   Here is another attempt based on Alvaro's formula:   sprites.SetPosition(sprites.GetOriginalPosition() + sprites.GetSpeed() * gameTime.ElapsedGameTime.Milliseconds * -Vector2.Normalize(sprites.GetSpeed()) * 5 /* oscillationAmplitude */ * Math.Sin(gameTime.ElapsedGameTime.Milliseconds * 5 /* oscillationFrequency */));   Here you can see my misused attempt at implementing time and the amplitude and frequency.   And here is another attempt based on a suggestion from this thread:   //Position2D = sin(angle) * magnitude * vector2DDirection + point2D sprites.SetPosition((Math.Sin(45) * (5 * sprites.GetSpeed()));   Does this help?
  8. Waylander

    Need help with 2D oscillation

    Your first post was great but it did leave a few things unanswered for me...which is perfectly fine since I was unsure how to ask what I needed and you were working with what you could.  And after all, it was the first tottering steps in our communication.  But I figured I'd give it a go (there is definitely something to be said for making the attempt even when you don't fully understand what is going on).  After some experimenting, I had to ask for clarification.  That is where the incomplete answers (I had to ask about the variables being vectors twice) and comments (could have done without the high school/middle school comment...I've slept a few times since those days and don't remember everything) began.   If you have more input, I'd love to hear it.  However I'd like to mention again that I am very new to this and that it is very possible that I may not know what you assume I know (or should know).  With that thought in mind, I need simple, concise answers to help me reach that "Aha!" moment (and most likely cause me to smack myself in the forehead because I should have seen the solution).
  9. Waylander

    Need help with 2D oscillation

    Buckeye, yes that is what I am looking to do.  But my trouble comes from my vectors not being in a nice straight line along the X axis.  I give each of my sprites a random vector (within a specific range depending on which side of the screen they are coming from) so the angles of that red line aren't always straight along either the X or Y axes.  So while that code you referred to gave me an idea on how it would work, I don't know how to apply it to situations where the sprite isn't going in a perfectly straight horizontal line.  As I told Alvaro, I "think" I am close to implementing his formula but I am hung up on how to apply the amplitude and frequency as well as how to use the time.  I know that the gameTime is used for movement (after all, I use it when my sprites move in a nice straight line) but I don't know specifically what needs to be done with the time in order to use it in that formula.  I'm sure I can simply pull in the milliseconds but that doesn't seem right.  And thank you for the explanations on amplitude and frequency.  Very helpful for me trying to get my head wrapped around this :).   Alvaro, don't bother responding again.  Snippy comments and thinly-veiled insults are not helpful.  Neither is pointing out that I don't understand something.  If I understood then I would have little need to ask for help.  This may come as a shock to you, but with ignorance being a key component to someone asking a question, it is a very real possibility that ignorance may also play a role in articulating the question and/or situation.  If you want to be helpful, then help the asker get to the core of their problem rather than just referring them somewhere else (which they may have already read).  If you help them better articulate what they need, then they will be able to ask better, more detailed questions and will apply that to future questions.  Everyone is at various stages of learning and it is counter-productive to respond to someone's post to "help" only to respond with quips and answers that leave them none-the-wiser.   Bacterius, I am well aware of research and debugging but I made it clear that I am new to this.  That being said, and as I pointed out to Alvaro, ignorance plays a large role here.  And when it comes to research, if I don't know what to look for (ignorance), then I don't know how to look it up.  I did try to look it up.  I looked up many things before resorting to posting here.  I appreciate your responses but making the assumption that I didn't try to find the answer myself before posting is incorrect.  I am learning how program and with that I have to learn how to solve problems, figure out math, learn terminology, discover available resources and how to use them, learn to debug, and more.  Assuming someone didn't try first carries the connotation of "It's so simple, why don't you just do such-n-such?" when in reality it may be simple to you due to your experience but I haven't reached that point yet.  I am getting there, slowly but surely.  I'm going to see what happens when making the scalar value a float as you mentioned and see what happens.
  10. Waylander

    Need help with 2D oscillation

    As of right now, my sprites "race across the screen."  What I want to add to that behavior is a wave motion so while they are moving across the screen, they are oscillating (correct term?).  These sprites can come from anywhere offscreen (from any side) and move at any vector.   Alvaro, I asked you if those amplitude and frequency portions were vectors because I get the message "operator * cannot be applied to operands of type 'Microsoft.Xna.Framework.Vector2' and 'double'."  Then you responded by telling me to read Wikipedia.  Then I mentioned I was after how those variables were used in code because you didn't answer how they were used or if they were vectors and you responded with your formula was basically the code.  I appreciate your time but your last two responses haven't cleared up what I asked you about.  If you are responding to clarify your response, you can't expect the person you are talking to to understand that response if it is just a rehash of the original response (IE "What I posted is basically the code") that they didn't understand in the first place and/or still need help on.   This isn't a jab at you but rather an illustration of my ignorance.  I have come a long ways in my efforts to teach myself programming (and I've even had newbie programmers tell me that I am advanced...which makes me feel good!) but there is still so much I have to learn.  The latest area I am trying to learn is how sprites move and the math behind that movement (waves, arcing, gravity, etc.).   Buckeye, I hope my explanation in this post makes it clearer.  I'll post a picture if necessary.  Since I am still learning and getting used to the terminology, I might not be going about it the right way.  That said, I "think" I understand vectors (at least on a basic level) and I have studied them but this is my first foray into the actual application of something that didn't involve straight lines and making the transition from abstract textbook text and application isn't necessarily a smooth one.   Bacterius, most of what you posted I haven't come across before but I'm trying to use it as a jumping off point to tackle my problem.  If it doesn't come together for me, at least it may serve to plant a seed in my mind that I can refer back to later :).  You mentioned something that I asked Alvaro in that the frequency can't be a vector and I can accept that.  What I'm needing help with is how the pieces from that formula come together since my current implementation gives me an error (as mentioned above).   I also mentioned earlier that I am unsure how to implement time in this.  My only real experience with time comes from "pausing" something for a certain number of milliseconds and I don't see how that can help here.
  11. Waylander

    Need help with 2D oscillation

    What I am trying to understand is how they (amplitude and frequency) are used/implemented in code (specifically the code you posted).
  12. Waylander

    Need help with 2D oscillation

    Ok, I've been tinkering with this and unfortunately haven't gotten anywhere.  There are a few things I don't understand.  But I'll start with what I'm trying to do and hopefully it will help.   Essentially what I am doing is having sprites randomly spawn off screen (any side) and having them emerge onscreen, race across the screen, and then leave the screen.   Buckeye, what would the magnitude be?  An integer?  Is the vector2DDirection my sprite's speed?  And what is the point2D?  Is that the "pick a point" you talked about?   Alvaro, I "think" I am close to implementing your final formula but I am stuck on your oscillation_amplitude and oscillation_frequency.  What are those?  Other vectors?   And to both of you, how are you using t (time) in here?  I don't know how to implement that.   Again, thanks for your posts.  I get the feeling that I am on the cusp of understanding this stuff since I am moving from textbook stuff to actual implementation...which makes this all the more frustrating! :)
  13. Waylander

    Need help with 2D oscillation

    This is where it gets fuzzy for me (and also where I am getting used to the terminology).  I'm going to play around with this for a bit and see what I come up with.  Thanks guys for getting me started :).
  14. I'm working in XNA but my problem is the math so I posted here.   FYI I am very new to this kind of math and I've been studying hard.  I am to the point where I "think" I can solve this myself but once I try to work it out, I draw a blank.   Simply put, I am wanting sprites to oscillate (sine/cosine?) in a wave-like pattern along their vector.  I've found several options that work with going along in a straight line along the X axis and I believe I understand how those work.  However, my sprites aren't moving straight along the X axis and unfortunately I haven't found anything that is specific to what I'm after.  Their vector can vary in basically any direction.  What I don't know how to do is take the information from their vector and use it to create the oscillation I am after.   Any ideas?
  15. I'm wanting to take input from touch input and use it in my Windows phone game.  What I'm wanting to do is take in a player's freedrag and then apply it to a sprite.  I've gotten to the point where I can apply the freedrag to a sprite with the following code:     That code works but it moves the sprite while the drag is occurring and that isn't what I'm after.  What I'm wanting to do is have the player's freedrag finish before the game applies it.  I've tried using:   if (gesture.GestureType == GestureType.DragComplete)   in various ways but when I do, it doesn't ever register that the drag was complete to run the code in the if statement.   Anyone know how I can go about this?
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!