Author |
Message |
SaintK
|
Posted: Tue Dec 08, 2009 9:28 am Post subject: Diagonal character movement |
|
|
So as our game requires a guy who can run around freely for his life I need to know how to move my character on a diagonal path so I was wondering:
Would moving diagonally be as easy to code as just making it so ASCII code left and up together cause the character to move diagonally in that direction? |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
mirhagk
|
Posted: Tue Dec 08, 2009 10:34 am Post subject: RE:Diagonal character movement |
|
|
to code diagonally all you have to do is make sure that each of the 4 directions of moving (up,down,left,right) is in a different if statement. for instance in pseudocode id be
code: |
if up then
move up
end if
if left then
move left
end if
if down then
move down
end if
if right then
move right
end if
|
then all the user has to do is press up and left and he will move up and to the left on a diagonal
voila, yep that easy |
|
|
|
|
 |
B-Man 31

|
Posted: Tue Dec 08, 2009 10:36 am Post subject: Re: Diagonal character movement |
|
|
it not that hard:
Turing: |
if up_arrow_pressed then %Up Arrow
character_y += 1
elsif down_arrow_pressed then %Down Arrow
character_y -= 1
end if
if right_arrow_pressed then %Right Arrow
character_x+= 1
elsif up_down_pressed then %Left Arrow
character_x -= 1
end if
|
So witht this, you can use all the arrows.
EDIT: DAMN, TOO SLOW BY 2 MINUTES  |
|
|
|
|
 |
Draymire
|
Posted: Tue Dec 08, 2009 10:39 am Post subject: Re: Diagonal character movement |
|
|
My solution for this problem was having to statements that get direction, one which gets up and down and one that gets left and right. After turing finds this you have technically moved diagonally in any direction. What you wouldn't see is the simple you moved left then up.
for example
code: |
if key('a') then
...
elsif key('d') then
...
end if
if key('w') then
...
elsif key('s') then
...
end if
|
If that makes any sense
EDIT: Same by 5 wow i type slow |
|
|
|
|
 |
mirhagk
|
Posted: Tue Dec 08, 2009 10:46 am Post subject: RE:Diagonal character movement |
|
|
lol one thing I would like to note is that if you do this
then if the player presses up and down then the character will move up only. If you put each one in a different if statement then the character will stay still, this is generally more desired since pressing both directions should negate each other and not move the character |
|
|
|
|
 |
B-Man 31

|
Posted: Tue Dec 08, 2009 10:55 am Post subject: Re: RE:Diagonal character movement |
|
|
mirhagk @ Tue Dec 08, 2009 10:46 am wrote: lol one thing I would like to note is that if you do this
then if the player presses up and down then the character will move up only. If you put each one in a different if statement then the character will stay still, this is generally more desired since pressing both directions should negate each other and not move the character
i guess, but if you dont wan the character to move why would you press the button, i understand why you did it that way, but it just makes little sense it form of that people woulnd press the down arrow to negate the up arrow if it was for movement, i understand if it had to do with velocities where the down arrow would negate the positive y velocity. but yeah, ither works for this,
EDIT: ALSO, lol at all the replies stating the same thing (well almost)  |
|
|
|
|
 |
Insectoid

|
Posted: Tue Dec 08, 2009 12:45 pm Post subject: RE:Diagonal character movement |
|
|
A little different...
code: |
if KEY_UP_ARROW and KEY_LEFT_ARROW then
x+= 1
y-= 1
end if
|
This will support 8 directions.
An alternative would be to have the character move to a degree value, and the left/right arrows change that degree, up arrow move forward along that degree and back arrow at the opposite degree. The hard part here is calculating how many pixels to move X and how many to move Y. |
|
|
|
|
 |
Zren

|
Posted: Tue Dec 08, 2009 1:13 pm Post subject: RE:Diagonal character movement |
|
|
I'll point out now that if you move diagonally, you'll actually be moving faster with the methods shown. Diagonal movement should try to be consistent with regular or you'll find people only moving diagonally. Since most of these have you moving 1.44 pixels/frame instead of the 1 pixels/frame.
In order to change it so it's consistent, we need to use Pythagorean Theorem.
When moving diagonally:
x_velocity = sqrt( speed^2 / 2 )
y_velocity = sqrt( speed^2 / 2 ) |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Insectoid

|
Posted: Tue Dec 08, 2009 1:39 pm Post subject: RE:Diagonal character movement |
|
|
Ah, well, I thought I'd keep it simple. Something I wrote just now that gives full 360-degree movement:http://compsci.ca/v3/viewtopic.php?p=199889#199889
It can be easily modified to be start-stop instead of accelerating. |
|
|
|
|
 |
mirhagk
|
Posted: Tue Dec 08, 2009 2:31 pm Post subject: RE:Diagonal character movement |
|
|
lol i was considering explaining something to that effect but I figured that it was a little too complex for this person maybe |
|
|
|
|
 |
gan gar

|
Posted: Sat Dec 12, 2009 2:50 pm Post subject: RE:Diagonal character movement |
|
|
this is going to help in my mario game thanks. |
|
|
|
|
 |
|