![]() |
|
moving character diagonally, instead of walking x direction first, then y... |
red-dragon
Member #7,722
August 2006
|
Hey guys, I would like to change this so that he moves diagonally to that point. If user has left-clicked in northeast direction, I want him to move directly in that direction. Hope I'm being clear here. In updatesprite I have this:
Part of calculate_position: if(mario->x < x) //&& (mario->y > y)) { if(mario->y > y) { //Link will move northeast ydir = 5; ynew = mario->y - y; } ... .. . In while loop in main, if left-click is detected, do this:
OK, so variable names might not make sense since I'm changing code, i.e. Mario should be Link, ynew will be changed to new_dir if I can make this diagonal thing work. If I run this code, Link walks a perfect diagonal line, even though the cursor might be might be ten units to the left. Make sense? Hope so; if not, ask away. Or just give me general advice at least |
Rick
Member #3,572
June 2003
![]() |
Try vector movement. Do a forums search for vector movement. ======================================================== |
Steve Terry
Member #1,989
March 2002
![]() |
Simple trig should give you the direction angle, from there you can calculate deltax and deltay. ___________________________________ |
ImLeftFooted
Member #3,935
October 2003
![]() |
direction ranges 0 to (2 * M_PI) |
red-dragon
Member #7,722
August 2006
|
hey Steve, it's been a long time for me since I did simple trig. Dustin, thanks for the help. Though for the sin, cos, and M_PI (that's pi, right, ha?), is there a library I need to include? If I'm missing the point, please elaborate. Really appreciate your help, thanks. |
ImLeftFooted
Member #3,935
October 2003
![]() |
Use #include <cmath>
|
red-dragon
Member #7,722
August 2006
|
Dustin, So, here come the questions: "direction rangest 0 to (2 * M_PI)"... I'm a bit lost here. In my code I have the normal 8 directions. Sorry if I seem a bit dense. Explain away. |
ImLeftFooted
Member #3,935
October 2003
![]() |
sin and cos return a number ranging 0 to 1 The beauty of this system is that it takes care of all the in-between values, so (M_PI / 4) will go up and right. Be careful though, cause the screen is actually upside down (ie quadrant 4). Up might easily come out as down. Quote: "direction rangest 0 to (2 * M_PI)"... I'm a bit lost here. In my code I have the normal 8 directions. ok so if directions is a value ranging 0 to 8, you can pass it into sin and cos like so: x += sin(directions * (2 * M_PI) / 8) * some_value; [disclaimer]
[edit] Owell, hopefully this will clarify why you do that anyway. |
Paul whoknows
Member #5,081
September 2004
![]() |
Can you post your code? or perhaps a binary? so we can see what you are doing. ____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
ImLeftFooted
Member #3,935
October 2003
![]() |
Quote:
Can you post your code? or perhaps a binary? so we can see what you are doing. I believe he wants to move his character by an arbitrary angle, which the distance formula cant do. Distance formula is usefull for spherical collision detection. |
Paul whoknows
Member #5,081
September 2004
![]() |
sorry!, I meant the secant line between 2 points, using it little link would walk in a straight line from his current position to where the user left clicks. ____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
ImLeftFooted
Member #3,935
October 2003
![]() |
Ah yes, it would be usefull for that too |
red-dragon
Member #7,722
August 2006
|
Paul, ok, so that gives me d.... where would I go from here? ...what do I do with it? how will little ol' link walk diagonally? I have code right here: code |
Steve Terry
Member #1,989
March 2002
![]() |
double x, y; Here all you need to determine is direction which you can get in radians if you use the nice little function atan2. #include <math.h> Hope that helps, basically atan2 will return in radians the angle between two given points. Of course give a good speed variable, something less than 1.0 that will move your character smoothly between the two points. Now you have an angle in radians you can also convert that to a fixed point degree angle to pass into rotate_sprite so your character rotates at that angle as well. ___________________________________ |
ImLeftFooted
Member #3,935
October 2003
![]() |
Quote: d = sqrt(((x2-x1)^2) + ((y2-y1)^2)) formula? Instead, you should use this code: #ifdef MSVC #define hypot _hypot #endif ... d = hypot(x2 - x1, y2 - y1); Some CPUs may implement hypot as an instruction which would be way faster then the 6 or so instructions of the pre-mentioned method. |
Paul whoknows
Member #5,081
September 2004
![]() |
Quote:
Paul,
No no, I meant the secant line between 2 points {"name":"96215157b42adf1a03b4da860ca80770.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/e\/de5da42bd94caec2d100fcc40dcfb8ef.png","w":261,"h":39,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/e\/de5da42bd94caec2d100fcc40dcfb8ef"} ____ "The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner. |
|