This site requires JavaScript, please enable it in your browser!
Greenfoot back
Entity1037
Entity1037 wrote ...

2014/5/8

Calculate speed and angle of movement FROM x and y movement variables?

1
2
Entity1037 Entity1037

2014/5/8

#
I had a change of plans. It's not x and y movement variables, but multiple movement/angle variables, and I'm having a hard time with this. I can't seem to figure out what to do in order to make code to do this, and I have little experience using trigonometry. Could someone please help me out? I need to take something like this:
1
2
3
4
5
6
7
int fmove=5;//player's overall momentum
double fface=45;//player's ultimate direction of movement
 
double wmove=1.0;//speed player walks/runs forward
int wface=0;//angle player walks/runs forward
double gmove=1.0;//additional speed from gravity
int gface=90;//angle (direction) gravity is pulling you towards
and then basically modify sonic's speed and angle based on his walking movement and angle so that he moves from the two movement and angle variables in the exact way he would move if I just set his location with the individual variables a bunch of times. The idea is to use sonic's falling movement and angle in order to get sonic to have an overall momentum and direction that every other movement and angle variable kind of *dump* into, enabling sonic to get boosts from gravity when he runs along a curve from falling and such. This is, yes, very complicated, however the point is to make a total momentum physics engine that could possibly be used in more things than just sonic.
bourne bourne

2014/5/8

#
Math.atan2(dy, dx) will get you your angle. As for the speed, time is missing from the given context. EDIT: Suppose the movement occurs every second, then to find the speed you would find the distance traveled per movement; Math.sqrt(dy*dy+dx*dx) This is your speed, distanceUnit per second. Then do the necessary conversions to the desired unit of speed i.e. mph
danpost danpost

2014/5/8

#
If you have fields holding 'xSpeed' and 'ySpeed' (or something like that), then just replace 'dx' and 'dy' of bourne's post with those fields.
Entity1037 Entity1037

2014/5/8

#
I just updated my post. I realized that using x and y speed variables alone wouldn't work out the way I need (although thank you! The information you gave IS helpful for another scenario I have). So... my problem is much more complicated now if I'm going to program it the way I stated above...
bourne bourne

2014/5/8

#
When calculating the overall movement, you can split your different movements into x and y components and add them together (xs with xs, ys with ys). Using the x and y sums, you can calculate the overall angle and speed like I mentioned above. That's my impression of the problem, but I don't know physics all that well.
Entity1037 Entity1037

2014/5/8

#
Wait!!!! I'll convert every move and angle variable (including the ultimate ones) into x and y positions like the Greenfoot api move() method, empty the speed variables, add all of the positions together, and then calculate the ultimate movement and angle variables from the sum x and y positions! HA!!! I have one question though: is that the right way to do it, or am I missing something?
Entity1037 Entity1037

2014/5/8

#
I made my post before I saw yours bourne. Although thank you anyway!
Entity1037 Entity1037

2014/5/8

#
I have another question (I know, I ask a LOT of questions)... How would you find gravity's pull on an object that's standing at a 306 degree angle on a 306 degree angled slope in greenfoot when gravity is pulling the actor downward with a force of 0.25 pix (answer as a double, and I'm not sure what the put the friction force at).
Entity1037 Entity1037

2014/5/8

#
I don't understand the equation for this... well at all (I'm only a sophomore!) I would really appreciate the help!
danpost danpost

2014/5/8

#
Gravity supplies a force. The object standing on provides a force equal to and opposite the force applied onto it at a perpendicular to its surface. The difference between these two forces is the remaining force that must be compensated by friction; so an equal and opposite force to the remainder should be the friction force. At least, that would be my assumption.
Entity1037 Entity1037

2014/5/8

#
I figured out what to do! Thank you!
Entity1037 Entity1037

2014/5/8

#
This is what I got:
1
2
3
4
5
6
7
int gface=/*Any int between -1 and 361*/;//this is the direction the player will move towards due to gravity
int gacc=0.25;//Amount gravity accelerates the player downards
int netface=315;//direction player is moving along the slope
int netmove=/*current speed*/;//amount player is moving
 
//code that figures out and applies the force of gravity while on slopes
netmove-=Math.sin(gface-(netface+90))*gacc;
My goal here is to apply the foce that gravity has on the player on slopes. So I'm just finding F// here. Also, please tell me if I'm wrong.
davmac davmac

2014/5/9

#
Math.sin works in radians, not degrees, so you need to convert first:
1
netmove-=Math.sin(Math.toRadians(gface-(netface+90)))*gacc; 
Other than that, I think you're correct.
Entity1037 Entity1037

2014/5/10

#
One more quick question. If I set the location of an actor, and get coordinates of nearby actors in the same cycle in the same bit of code, will the coordinates be the way they were before I moved the character, or will they be immediately updated? Also, thank you all for your help!
davmac davmac

2014/5/10

#
If I set the location of an actor, and get coordinates of nearby actors in the same cycle in the same bit of code, will the coordinates be the way they were before I moved the character, or will they be immediately updated?
The coordinate change takes effect immediately, so if you call eg getObjectsInRange, then getObjectsInRange will return actors that are close to the new location.
There are more replies on the next page.
1
2