Getting XY speed from an angle/velocity
I'm writing some code with a vehicle in it. I need to know how I can get the x and y speeds for it from the current angle and speed of the car. No gravity or other natural forces included.
Let's say the angle is 180 degrees (facing user/south) and the speed is 5. Everybody should know that this means that the y speed is 5 and no x speed. What I need to know is how to get this if it's a noncardinal direction, such as 79 degrees.
Thanks in advance.
P.S. Use pseudocode or BASIC if possible.
Let's say the angle is 180 degrees (facing user/south) and the speed is 5. Everybody should know that this means that the y speed is 5 and no x speed. What I need to know is how to get this if it's a noncardinal direction, such as 79 degrees.
Thanks in advance.
P.S. Use pseudocode or BASIC if possible.
Basic trigonometry:
Code:
vx = v * cos(angle)
vy = v * sin(angle)
Quote:Originally posted by OneSadCookieHeh. Thanks. Haven't got into trig much yet in school.
Basic trigonometry:
Code:
vx = v * cos(angle)
vy = v * sin(angle)
Then you should be aware that sin & cos functions almost always take angles in radians, not degrees.
To convert degrees to radians, divide by 180 and multiply by π.
To convert degrees to radians, divide by 180 and multiply by π.
METAL uses radians. 45 degrees = 0 radians.
BTW, I've had NO trig. And I still haven't been able to determine something's angle in relation to something else.
I'll just continue this thread. Say I have a player x, y, and angle. px, py, and pangle, respectively. Given ex and ey (enemy x, enemy y) how do I find out what angle the enemy is in relation to the player? I'm trying to make the AI turn towards powerups in my entry in the METAL contest and I can't figger this part out.
BTW, I've had NO trig. And I still haven't been able to determine something's angle in relation to something else.
I'll just continue this thread. Say I have a player x, y, and angle. px, py, and pangle, respectively. Given ex and ey (enemy x, enemy y) how do I find out what angle the enemy is in relation to the player? I'm trying to make the AI turn towards powerups in my entry in the METAL contest and I can't figger this part out.
My web site  Games, music, Python stuff
Wait, the angle code doesn't work converting to radian or something.
It's moving to the right side (about +45 degrees)... here's my code:
[SOURCECODE]
xspeed = speed * cos((angle/180)*pi)
yspeed = speed * sin((angle/180)*pi)[/SOURCECODE]I'm using pi as 3.14.
It's moving to the right side (about +45 degrees)... here's my code:
[SOURCECODE]
xspeed = speed * cos((angle/180)*pi)
yspeed = speed * sin((angle/180)*pi)[/SOURCECODE]I'm using pi as 3.14.
Quote:Originally posted by diordna
how do I find out what angle the enemy is in relation to the player?
p1Y is the player's Y position
p1X is the player's X position
p2Y is the enemy's Y position
p2X is the enemy's X position
angle is the angle of the enemy in relation to the player in degrees
[sourcecode]
p1Y = 50
p1X = 50
p2Y = 25
p2X = 25
pi = atn(1)*4
if p1Y > p2Y then difY = p1Yp2Y else difY = p2Yp1Y
if p1X > p2X then difX = p1Xp2X else difY = p2Xp1X
angle = atn(difY/difX)*180/pi
[/sourcecode]
"Programmers are tools for converting caffeine into code."
diordna  atan2. This was covered a week or so ago by another METAL programmer.
macboy  precisely whether you should use sin() or cos() for x will depend on your coordinate system (you'll always use the other for y). You may also want to change the sign of either x or y to make the angle go the other way.
macboy  precisely whether you should use sin() or cos() for x will depend on your coordinate system (you'll always use the other for y). You may also want to change the sign of either x or y to make the angle go the other way.
Thanks everybody. Switching sin and cos then multiplying the y speed by 1 made it work.
Next dilemma, not the same, but it has to do with this project. How can I do some sort of drag physics, for instance, when you drive straight then turn right, how can I make the back end swerve when that happens?
Next dilemma, not the same, but it has to do with this project. How can I do some sort of drag physics, for instance, when you drive straight then turn right, how can I make the back end swerve when that happens?
Treat the front & back wheels of the car as two different particles, each with their own velocity. When the user steers, change only the velocity of the front particle, leave the back one doing what it was. Each frame, move the particles by their velocity * dt, then move the particles towards each other so they are some constant distance apart. Apply acceleration only to the front wheel particle.
http://www.gamasutra.com/resource_guide/...n_01.shtml (free registration required) might be of some use to you.
http://www.gamasutra.com/resource_guide/...n_01.shtml (free registration required) might be of some use to you.
Quote:Originally posted by OneSadCookieThanks.
Treat the front & back wheels of the car as two different particles, each with their own velocity. When the user steers, change only the velocity of the front particle, leave the back one doing what it was. Each frame, move the particles by their velocity * dt, then move the particles towards each other so they are some constant distance apart. Apply acceleration only to the front wheel particle.
Quote:Originally posted by OneSadCookieActually I just pressed back and it worked
http://www.gamasutra.com/resource_guide/...n_01.shtml (free registration required) might be of some use to you.
New problem... this time I don't have a velocity but instead a relative ending point. I need to figure out how to make it an offset of the parent object.
In English:
I have the car's current position, speed, and velocity.
The car can shoot a projectile (which doesn't show up) which creates a crater where it "landed".
I need to know how to figure out the crater's position without creating a moving projectile sprite.
Here's the current code:
[SOURCECODE] craterx = playerx + (gunrange * sin((angle/180)*pi)) + random(gunrandoffset*2)gunrandoffset
cratery = playery + (gunrange * cos((angle/180)*pi)) + random(gunrandoffset*2)gunrandoffset[/SOURCECODE]
But unfortunately this makes it start from the player's topleft corner, while the gun is in the middle.
Sorry if that didn't make much sense
In English:
I have the car's current position, speed, and velocity.
The car can shoot a projectile (which doesn't show up) which creates a crater where it "landed".
I need to know how to figure out the crater's position without creating a moving projectile sprite.
Here's the current code:
[SOURCECODE] craterx = playerx + (gunrange * sin((angle/180)*pi)) + random(gunrandoffset*2)gunrandoffset
cratery = playery + (gunrange * cos((angle/180)*pi)) + random(gunrandoffset*2)gunrandoffset[/SOURCECODE]
But unfortunately this makes it start from the player's topleft corner, while the gun is in the middle.
Sorry if that didn't make much sense
craterx = playerx + playerwidth/2 + ...
cratery = playery + playerheight/2 + ...
?
cratery = playery + playerheight/2 + ...
?
Sorry. Doesn't work because if the player turns around so it's 180 degrees it will be to the left of the player
So why not make playerx and playery the center of the player? That's usually more useful anyway...
Possibly Related Threads...
Thread:  Author  Replies:  Views:  Last Post  
Formula for converting angle to vector?  komirad  2  11,262 
Jul 29, 2011 07:29 AM Last Post: ThemsAllTook 

OpenGL Velocity and Grouping of Lines (C++, Xcode)  TheThirdL3g  2  3,868 
Jul 29, 2010 01:28 PM Last Post: SethWillits 

Question Regarding the Reflect Angle of a Transition  iBaby  3  3,944 
Apr 27, 2010 03:15 PM Last Post: JustinFic 

ending location from angle and speed  Kazooless  5  4,994 
Apr 3, 2009 02:40 PM Last Post: Gillissie 

Angle between two points?  Graphic Ace  6  6,004 
Nov 8, 2008 12:11 PM Last Post: macnib 