Need help with 2D oscillation

Started by
30 comments, last by Waylander 10 years ago

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 :) ).

Ulfhedhin

Advertisement

A little tough love here, but, from the quantity and content of your questions, it appears you do, in fact, have to do some studying with regard to basic programming, vectors, dimensional analysis, basic physics, etc.

Googling for things like "equations of motion," "dimensional analysis," "angular motion," etc., should get you started.

I'm not particularly familiar with the language you're programming in, so you'll have to study up a bit on how that works, too. E.g., it appears GetSpeed() returns a Vector2, but you'll have to verify that m_Speed (the return value) fits the bill.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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.

Ulfhedhin


how would you go about doing that?

I would do it using Alvaro's equation, as explained in my previous post in which I discussed each of the terms in that formula.

Time is time in seconds. I don't know how to explain it any better than that.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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.

Ulfhedhin


how is "t" calculated before it is plugged into that formula?

"t" is just a variable that changes, normally it always increases. You can use real-time, your own "game time," anything that changes between one frame render and the next.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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.

Ulfhedhin


Before you said something about picking a point and going to it.

Hmm. I don't remember that nor can I find anything about that in previous posts. If you saw that somewhere you'll have to post a quotation.

Once again, here's equation you should be trying to simulate.


(x,y) = (x0,y0) + (v.x,v.y) * t + (perp.x,perp.y) * oscillation_amplitude * sin(t * oscillation_frequency)

and what I posted before about it:

for Alvaro's (perp.x,perp.y), as he explained, you need a vector that is perpendicular to the vector (v.x, v.y). When you come up with something for the UnitDirectionVector above, calculate a unit vector perpendicular to it and use it where Alvaro has (perp.x,perp.y).

From what you posted, you're using Vector2.Normalize(sprites.GetDirection()) for (perp.x,perp.y). Normalizing should get you a unit vector. However, go over the above equation and my comments on it. GetDirection() isn't perpendicular to GetDirection().

At one point in your efforts, you used GetSpeed() for (v.x, v.y) above, which you said was a 2D vector. I'm not sure why you abandoned that and replaced it with Vector2(10,0). If GetSpeed() is a Vector2, use that.

With regard to (float)Math.Sin(gameTime.ElapsedGameTime.Seconds * 5), maybe 0.5 would be better. If you have questions about that later, when you get it all working, that could be discussed then.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

In your first post, you put:


You don't provide much detail, but it can be as simple as choosing a 2D point and a direction.

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.

Ulfhedhin

Speed = scalar quantity (eg not a vector) usually, like 5 units/time

Unit vector = vector of length 1, a direction

Velocity = speed*direction where direction is an unit vector

Time = seconds, milliseconds, ticks, whatever

Delta time, dt = time passed since last update (eg, how much time passed since the previous update of this value)

Position = (x,y) vector here

1. You want to make your object go to a direction at a constant speed.

newPos = prevPos + velocity*dt

velocity*dt gives you the displacement of the object from the previous update, dt being time passed since the previous update.

Run this in a loop and you have a moving object.

2. You want to add the oscillation. You should probably implement this as an offset, so that the linear movement and oscillation are separate and dont mess with each other.

offset is a vector, just like position.

normal = perpendicular(normalize(velocity))

offset = normal * amplitude*sin(t*frequency)

3. Combine them to get a final position for the object (where you display the object)

displayPos = newPos + offset

Something like that. I suggest not working with x,y coordinates but only use vectors. If you need to normalize or get the perpendicular vector, find those functions from somewhere or make your own, but keep the logic as vector only so its simpler and easier to see whats going on.

o3o

This topic is closed to new replies.

Advertisement