Tuesday, February 11, 2020

mathematics - Move a sprite in a sine wave along different angles


I understand this piece of code


Enemy.position.Y = StartPosition.Y + (-(float)Math.Cos(position.X / 100) * 100);


However it only works along the X axis. What I want to be able to do is move it along an arbitrary axis; Set an angle as the x axis. How would I go about this?



Answer



I'm not sure if I understand your question but here goes:



I think you want the sprite to wave over an angle back and forth: Not this is pseudocode (as you didn't state the language):


t=(t+wavespeed)%TwoPi;
a = Sin(t)*amplitude;
Position.X=StartPosition.X + Cos(angle) * a;
Position.Y=StartPosition.Y + Sin(angle) * a;

t controls the waving motion; the wavespeed variable is a small value like 0.01 (note that it should slowly range from 0 to TwoPi). amplitude is the maximum you want your sprite to move, a is calculated as the amplitude for that frame. angle is the angle of the axis you want your sprite to wave along.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...