![]() |
Strafing in a 2D world!?
Well here comes non-math-guru stefan asking for some help from Aspirin type people:
I have a guy on the world, location x, y (double's). I can make it walk to any X,Y using sin and cos. I understand how it works , i have a tutorial. I convert real angels into Allegro degrees in order to draw my sprite correctly too. I thought that was hard, but its actually pretty easy... now the tough part: I want my guy to strafe all the time in a _STRAIGHT LINE_. meaning, It should strafe 90 degrees to the left/right. The problem is that i do not know how to compute this and make my character actually move. The processing function looks kinda like this: Code:
double newx, newy; x,y of mouse x,y of player any direction to left/right makes an x,y and voila, a triangle. The problem is here. Ie, when my character is on 100,100, my mouse is on 0,0. I press LEFT (strafe left). My character looks to the UPPER LEFT of the screen now. How do i calculate the 'strafing x,y'? I thought of circles, but i figured this is not right; at the moment i have some 'circular strafing' that does NOT work properly. Ie, it strafes, in circles, baaaddd. i want it to be 'straight'. So, lets say; i have this x,y of the mouse. I should start here; i can get the radians of this: Code:
Ok. So now i have the radians to the X,Y of the mouse. So you could say i already have the 2 coordinates right? So... how do i calculate the 90 degrees to the left? I feel like this is simple to solve, i just lack the knowledge... and the math brain. |
Re: Strafing in a 2D world!?
w00t. I figured it out myself. Actually i was close:
in theory: - calculate radians to the mouse x, y - convert to degrees - do + or minus 90 degrees - convert back to radians - calculate this position now (with length,etc) - now we know the 'destination x,y' , so we have our triangle. Now we can move straight to this line in code: (if you got any stuff to optimize it, please let me know) Code:
// calculate the angle ; increase it; calculate new x and y out from it.. phew |
Re: Strafing in a 2D world!?
you could also put that 2D coordinates to the x,y components of a 3d vector ( z = 0) and crossproduct it with (0,0,1). then you'd also get a vector perpendicular to your first vector in the 2d plane
|
Re: Strafing in a 2D world!?
EUH!? in code? ;)
|
Re: Strafing in a 2D world!?
Code:
the crossproduct of 2 vectors is perpendicular to both arguments and the length of the result is |length1| * |length2| * sin phi where phi is the angel between the arguments ... |
Re: Strafing in a 2D world!?
Why aren't you using matrix math for this?
|
Re: Strafing in a 2D world!?
why should one use matrices for this kind of operations ?
|
Re: Strafing in a 2D world!?
Although I still think floats are better than doubles (just don't use msvc), a few optimisations are:
Don't bother converting radians to degrees. Simply put in a number for pi/2 (90deg). From angles directly, or vector cross products, I'd probably say vector cross product - do you really need to know the magnitude if you're simply adding vector components btw? - as you can pull the x and y values needed to offset the movement directly then, without the need to even use sin/cos/tan/etc. |
Re: Strafing in a 2D world!?
yes, you can use radians instead of degrees with that sin stuff :) most ppl are used to degrees and even HL Engine is using degrees in most cases, that somehow strange ...
the length information was just for to stefan know it, often you just normalize it anyway :) |
Re: Strafing in a 2D world!?
Radians make much more sense to me.
But yet, normalisation is useful - speaking of which, time to go trawling through cmath to find those functions my little book here doesnt cover... |
All times are GMT +2. The time now is 20:55. |
Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.