• Advertisement

Archived

This topic is now archived and is closed to further replies.

Moving a sprite

This topic is 5025 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi ya Im having trouble with moving a sprite in the direction that i wish. My sprite can rotate 360 degrees and it rotates in 10 degree intervals. My sprite has a x and y coordinate and i also have a variable that represents the angle that the sprite is at. I have been trying to use cos and sine but with no luck. Whta am i doing wrong. cos is for the x coordinate and sine is for the y - right. what other variable would i need to get it going thanks

Share this post


Link to post
Share on other sites
Advertisement
show us some code and maybe we can help
it does seem a bit odd that if u can find the x and y lengths u can''t move it...

Share this post


Link to post
Share on other sites

This is my code for moving the ship forward. The speed is just a value of 5. What else is needed to move it. I do get it moving but it only goes in a diagonal way no matter what way the ship is facing.








void cShip::ThrustShip(float speed)
{

double Radian;

Radian = (ShipAngle * PI) / 180.0;

if((ShipAngle >= 0) && (ShipAngle <= 180))
{
VelX = cos(Radian);
VelY = sin(Radian);

}
else
{
VelX = cos(Radian);
VelY = sin(Radian) * -1;

}

XPos = XPos + (VelX * speed);
YPos = YPos + (VelY * speed);


}

Share this post


Link to post
Share on other sites
can ShipAngle be changed or is it set and never changed - it sounds like the ship''s angle could be constant throughout.

"Did you hear the one about the nun and the rabbi?"
JUST PRINT (typed)
"Okay, okay, Asshole!!" - Wrongfully Accused

Share this post


Link to post
Share on other sites
The shipangle is changed when you press the arrow keys to tuen the ship. I have the ship ship turning on screen believe it or not.

Share this post


Link to post
Share on other sites
and the ship just keeps moving the same direction - like backwards and crap like that?

"Did you hear the one about the nun and the rabbi?"
JUST PRINT (typed)
"Okay, okay, Asshole!!" - Wrongfully Accused

Share this post


Link to post
Share on other sites
well the ship goes diagonally down when the ship is between 10 - 180 and goes diagonally up between 180 - 360. when ur at 10 degrees it moves really slowly and the same for 350. the more you move it then the bigger jumps it takes across the screen

Share this post


Link to post
Share on other sites
ok if u dont really need to use radians, try just degrees. ive done some working and it seems that it should work as planned using degrees instead of radians. rads give very small numbers which someone else said might be rounded - could be true and giving a bad movement pattern. also they seem to calculate to wrong directions... sound familiar
in short, if u can, use degrees and try it

"Did you hear the one about the nun and the rabbi?"
JUST PRINT (typed)
"Okay, okay, Asshole!!" - Wrongfully Accused

Share this post


Link to post
Share on other sites
It''s been a while since I''ve done this, but try only:


VelX = cos(Radian);
VelY = sin(Radian) * -1;


without all the Ifs and stuff. Let me know if it works.

Stay Clausal,

Red Sodium

Share this post


Link to post
Share on other sites
(Sorry, posted twice, ignore this post)

[edited by - red_sodium on April 15, 2004 10:17:50 AM]

Share this post


Link to post
Share on other sites

const double UnitSpeed = 1.0;
void ThrustShip(int speed)
{
int NewAngle; //used for calculating percentages

bool XNeg, YNeg;
double xdir, ydir;

XNeg = YNeg = false;

//find out what quad the angle is in

if (angle >= 90 && angle < 180)
{
XNeg = true;
NewAngle = 90 - angle;
}
else if (angle >= 180 && angle < 270)
{
XNeg = true;
YNeg = true;
NewAngle = angle - 180;
}
else if (angle >= 270 && angle < 360)
{
YNeg = true;
NewAngle = 360 - angle;
}

//calculate the precentages angle will always be in favour of the x direction.

xdir = UnitSpeed * (NewAngle / 90);
ydir = UnitSpeed * (1 - NewAngle / 90);

//check to see if we are negating

if (XNeg)
xdir = -xdir;
if (YNeg)
ydir = -ydir;

ShipPosX += xdir * speed;
ShipPosY += ydir * speed;
}


I believe this is correct off the top of my head. Try it out to see if it works and let me know. It also saves you from making calls to sin and cos which can be expensive if called enough times. If you want to have the motion constant then you need to have the unit speed like I want it to move one unit when I thrust then multiply that by the speed. If someone sees a mistake with my logic please point it out.

Thanks,

Richard

Share this post


Link to post
Share on other sites
Richard, if I''m not wrong, your sin/cos replacement is not correct, since sin( 0 ) -> sin( 90 ) ( degrees ) does not interpolate liniary.

you tried to map the angle''s raport with respect to it''s quadran and use it as a linear interpolation time parameter, ranging from 0 to 1.

I do belive that this will transform the trygonometric circle into a 45 degrees rotated square around the origin which will result in unnatural movement

so, you''ll have to use sin() and cos(). if you want to make it faster, use lookup tables. one more hint : recalc sin and cos ONLY when angleShip is changing.

hope it helps,

benishor

---
novocaine thru yer veinz

Share this post


Link to post
Share on other sites
quote:
Original post by benishor
Richard, if I''m not wrong, your sin/cos replacement is not correct, since sin( 0 ) -> sin( 90 ) ( degrees ) does not interpolate liniary.

you tried to map the angle''s raport with respect to it''s quadran and use it as a linear interpolation time parameter, ranging from 0 to 1.

I do belive that this will transform the trygonometric circle into a 45 degrees rotated square around the origin which will result in unnatural movement

so, you''ll have to use sin() and cos(). if you want to make it faster, use lookup tables. one more hint : recalc sin and cos ONLY when angleShip is changing.

hope it helps,

benishor

---
novocaine thru yer veinz



I really don''t think that my code is wrong as it calculates the amount of movement in the x direction and y direction based on the angle percentage. say your angle was 30 degrees:

x direction percentage: 30 / 90 = 0.3333...
y direction percentage: what is left over is 0.6666...
so if you have a velocity of 1 unit you just multiply the velocity by the percentage in the x direction and the y direction to get the new (x, y) coordinates. Again correct me if I am wrong but I do believe that I am not. I just tried to keep it as simple as possible.

Thanks,

Richard

Share this post


Link to post
Share on other sites
Hi Richard,

sadly, I have to say you are wrong. I took some minutes to write a small php demo of both ideas. here''s the link :

http://benny.kappa.ro/sprive_movement/

p.s. even if you got the neg boolean variables wrong it still wouldn''t work as intended : you''re taking a value for xdir and then take the y dir value by substracting it from 1. the ship, though, will move on the distance represented by the hypotenuse. THAT distance has to be 1, not it''s projection on both axis added. thus, pythagora theorem brings us to : 1 * 1 = x_dir * x_dir + y_dir * y_dir, not 1 = x_dir + y_dir.

hope it helps,

benishor


---
novocaine thru yer veinz

Share this post


Link to post
Share on other sites
quote:
Original post by benishor
Hi Richard,

sadly, I have to say you are wrong. I took some minutes to write a small php demo of both ideas. here's the link :

http://benny.kappa.ro/sprive_movement/

p.s. even if you got the neg boolean variables wrong it still wouldn't work as intended : you're taking a value for xdir and then take the y dir value by substracting it from 1. the ship, though, will move on the distance represented by the hypotenuse. THAT distance has to be 1, not it's projection on both axis added. thus, pythagora theorem brings us to : 1 * 1 = x_dir * x_dir + y_dir * y_dir, not 1 = x_dir + y_dir.

hope it helps,

benishor


---
novocaine thru yer veinz


Good Stuff. It is a good approximation if XDir and YDir were switched around ie:

XDir = UnitSpeed * (1 - NewAngle / 90)
YDir = UnitSpeed * (NewAngle / 90)

Also I think I made a mistake here

quote:

if (angle >= 90 && angle < 180)
{
XNeg = true;
NewAngle = 90 - angle;
}



I think I should have put 180 - angle not 90 - angle.

Thanks for pointing out my error but would it really look the movement really look awckward if it were implemented this way? [EDIT 2]I don't think that it would matter too much dealing with small velocities but it would make a huge deal with larger values. [/EDIT 2]

[EDIT]
But if we are dealing with exact values then you are 100% absolutley right. Don't think that I am arguing that I am right anymore . This is a link to the file containing my code changes. The link will be fixed later as I am in the process of installing my new server.
[/EDIT]

thanks,

Richard

[edited by - BigR on April 20, 2004 7:34:24 PM]

[edited by - BigR on April 20, 2004 9:55:15 PM]

[edited by - BigR on April 21, 2004 11:05:14 AM]

Share this post


Link to post
Share on other sites
You don't need those ifs and elses.

const double DEG_TO_RAD = 0.01745; // PI / 180

void ship::thrust( float speed )
{
double rad_angle = angle * DEG_TO_RAD;

velx = cos(rad_angle) * speed;
vely = sin(rad_angle) * speed;

xpos += velx;
ypos += vely;
}

Will work just fine.

Oh, btw, use vectors, not std::vectors, but math/physics vectors. Much cleaner.

struct vector
{
float i;
float j;
};

void ship::rotate( float angle, float speed )
{
double rad_angle = angle * DEG_TO_RAD;

movement_vector.i = cos(rad_angle) * speed;
movement_vector.j = sin(rad_angle) * speed;
}

void ship::thrust()
{
xpos += movement_vector.i;
ypos += movement_vector.j;
}


[edited by - alnite on April 21, 2004 2:42:51 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by BigR
I don't think that it would matter too much dealing with small velocities but it would make a huge deal with larger values.




I'm afraid it does because in that function we're actually calculating the direction vector of the ship ( vector which by the way would need to be normalised in your code so that it will have the length of one ). think of it this way : those xdir and ydir values get added to the ship's position each frame, establishing the trajectory of the ship. if those xdir and ydir values aren't accurate, you will get unaccurate trajectories for different angle values.

alnite's code is correct. also remember to calculate the direction vector only when the angle changes.

regards,

benishor
---
novocaine thru yer veinz

[edited by - benishor on April 22, 2004 4:21:26 AM]

Share this post


Link to post
Share on other sites

  • Advertisement