Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1] 2   Go Down

Author Topic: [GM7] 8-Way Movement?  (Read 4150 times)

0 Members and 1 Guest are viewing this topic.

Mirby

Drifter
[GM7] 8-Way Movement?
« on: October 23, 2010, 01:12:10 am »
  • To bomb or not to bomb...
  • *
  • Reputation: +6/-0
  • Offline Offline
  • Gender: Female
  • Posts: 4162
I was trying to figure out a way to code a game so that diagonal movement was allowed, but I can't seem to figure out how to make it work. ;_;

In the game I'm making, 8-way movement would be a boon, but nothing I try works. Can someone here help me?
Logged

Mirby Studios | Share & Enjoy now available! (Links above!) | Games I've Beaten
Quote from: Mamoruanime
I like-like it :D
  • Mirby Studios
Re: [GM7] 8-Way Movement?
« Reply #1 on: October 23, 2010, 05:22:44 am »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
If you are trying to for a Zelda style gameplay, I suggest you take a look at the basic movement of the MCS Engine or Goodnights movement engine. Remember that when moving in a diagonal direction you are needing a fraction of the normal speed in both x and y.
Logged

Mirby

Drifter
Re: [GM7] 8-Way Movement?
« Reply #2 on: October 23, 2010, 06:38:16 pm »
  • To bomb or not to bomb...
  • *
  • Reputation: +6/-0
  • Offline Offline
  • Gender: Female
  • Posts: 4162
I'm making a horizontally scrolling shmup, actually. I'll look into those in case those help, though.
Logged

Mirby Studios | Share & Enjoy now available! (Links above!) | Games I've Beaten
Quote from: Mamoruanime
I like-like it :D
  • Mirby Studios

DJvenom

super-sage
Re: [GM7] 8-Way Movement?
« Reply #3 on: October 25, 2010, 01:42:55 am »
  • Colbydude was here.
  • *
  • Reputation: +7/-0
  • Offline Offline
  • Gender: Female
  • Posts: 2898
right: x+=1
up: y-=1
left: x-=1
down: y+=1

You can now move in 8 directions! :D
Logged
I do art
I ermmm... DID do art
I do art

Mirby

Drifter
Re: [GM7] 8-Way Movement?
« Reply #4 on: October 25, 2010, 02:00:15 am »
  • To bomb or not to bomb...
  • *
  • Reputation: +6/-0
  • Offline Offline
  • Gender: Female
  • Posts: 4162
Well I added that to the script I figured out, and it works like a charm! Thanks, DJ!!

While this topic is open, I have another GML question I wanted to ask. Is there any way to code something so the object would spawn within a random area?

In case that doesn't make sense, I mean like this.

Code: [Select]
                   ______________________
                   |                     |
                   |                     |
                   |                     |
                   |                     |
                   |                     |
                   |                     |
                   |                     |
                   |_____________________|
Say the code box is a level, and the square is the random area. Is there some way to code something to appear in a random location in the box, but not anywhere outside of the box?

Logged

Mirby Studios | Share & Enjoy now available! (Links above!) | Games I've Beaten
Quote from: Mamoruanime
I like-like it :D
  • Mirby Studios

DJvenom

super-sage
Re: [GM7] 8-Way Movement?
« Reply #5 on: October 25, 2010, 02:07:28 am »
  • Colbydude was here.
  • *
  • Reputation: +7/-0
  • Offline Offline
  • Gender: Female
  • Posts: 2898
x="left side of box"+random("right side of box")
y="top of box"+random("bottom of box")
Logged
I do art
I ermmm... DID do art
I do art

Brutus

Kirby Master
Re: [GM7] 8-Way Movement?
« Reply #6 on: October 25, 2010, 02:22:58 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 134
right: x+=1
up: y-=1
left: x-=1
down: y+=1

You can now move in 8 directions! :D

right: x+=1
up: y-=1
left: x-=1
down: y+=1

You can now move in 8 directions! :D

It's different on gm8
Logged
if place_free(x,y,z) {wrong number of arguments to function or script}

Mamoruanime

@Mamoruanime
Re: [GM7] 8-Way Movement?
« Reply #7 on: October 25, 2010, 02:34:44 am »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
right: x+=1
up: y-=1
left: x-=1
down: y+=1

You can now move in 8 directions! :D

right: x+=1
up: y-=1
left: x-=1
down: y+=1

You can now move in 8 directions! :D

It's different on gm8

Pseudocode is different in gm8? Amazing! I wasn't aware that could change depending on the tools you're using! Pretty awesome stuff here :D
Logged
Re: [GM7] 8-Way Movement?
« Reply #8 on: October 25, 2010, 02:59:57 am »
  • Doesn't afraid of anything
  • *
  • Reputation: +42/-0
  • Offline Offline
  • Gender: Male
  • Posts: 7002
It's different on gm8

Fail.

Also, DJ, there's a bit more to it since you have to get the proper directions for proper collision detection.
Logged



i love big weenies and i cannot lie
Re: [GM7] 8-Way Movement?
« Reply #9 on: October 25, 2010, 04:49:46 am »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
x="left side of box"+random("right side of box")
y="top of box"+random("bottom of box")
GML (GM8) has a nifty function called irandom_range(lowerbound, upperbound).

right: x+=1
up: y-=1
left: x-=1
down: y+=1

You can now move in 8 directions! :D

You still have to compensate the diagonal movement. When horizontal is x +/-= 1 and vertical y +/-= 1, then in diagonal movement the addition to x and y has to be smaller then 1 or it would appear that walking diagonal goes faster. And like MG said you need to consider directions for collision detection.
Logged

DJvenom

super-sage
Re: [GM7] 8-Way Movement?
« Reply #10 on: October 25, 2010, 04:51:03 am »
  • Colbydude was here.
  • *
  • Reputation: +7/-0
  • Offline Offline
  • Gender: Female
  • Posts: 2898
I was always under the assumption that GM converted an objects movement into a direction via the direction function :P
GML (GM8) has a nifty function called irandom_range(lowerbound, upperbound).
Topic title
Quote
[GM7]
Logged
I do art
I ermmm... DID do art
I do art
Re: [GM7] 8-Way Movement?
« Reply #11 on: October 25, 2010, 04:58:16 am »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
I was always under the assumption that GM converted an objects movement into a direction via the direction function :P
GML (GM8) has a nifty function called irandom_range(lowerbound, upperbound).
Topic title
Quote
[GM7]
Dooh! Missed that. Still a random_range(lower, upper) is a part of GM7 as well. It just gives doubles instead of integers.

And direction yes, but it is not automatically done. And with collision detection for setting the object back it still needs to be set. And all that.
Logged

DJvenom

super-sage
Re: [GM7] 8-Way Movement?
« Reply #12 on: October 25, 2010, 05:20:50 am »
  • Colbydude was here.
  • *
  • Reputation: +7/-0
  • Offline Offline
  • Gender: Female
  • Posts: 2898
This actually may explain why Flaggan's collision was pretty shoddy... Like I said tho, I'm a lazy coder :P
Logged
I do art
I ermmm... DID do art
I do art
Re: [GM7] 8-Way Movement?
« Reply #13 on: October 25, 2010, 11:20:35 am »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Posts: 160
i used Goodnight's engine but it still gives me problems with other things -_-
Logged
Nubcake now uses Purebasic!

Mamoruanime

@Mamoruanime
Re: [GM7] 8-Way Movement?
« Reply #14 on: October 25, 2010, 11:22:43 am »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
>_< he's making a shmup; what's a zelda engine have to do with this at all o.0
Logged
Re: [GM7] 8-Way Movement?
« Reply #15 on: October 25, 2010, 05:31:24 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
Mirby, if you want to know what value you have to add or subtract from x and y just calculate:

dx = cos( 1/4 pi )
dy = sin( 1/4 pi )

Then multiply it with the speed. That is for diagonal movement. Oh but don't put cos and sin in your code that is pretty computational intensive.



Wait a minute both sin( 1/4 pi ) and cos( 1/4 pi ) will result in the same value. Just calculate it, store it, and add a sign to it based on the user input.
Logged

Mirby

Drifter
Re: [GM7] 8-Way Movement?
« Reply #16 on: October 25, 2010, 07:25:45 pm »
  • To bomb or not to bomb...
  • *
  • Reputation: +6/-0
  • Offline Offline
  • Gender: Female
  • Posts: 4162
... ACK! TRIG! *head explodey*

At least, I think that's trig.

Also, as I said, I took DJ's pseudocode and worked it into the nonworking code; it was the missing piece to my code to make it work.

I'll check out the other one later. Not on my lappy right now.
Logged

Mirby Studios | Share & Enjoy now available! (Links above!) | Games I've Beaten
Quote from: Mamoruanime
I like-like it :D
  • Mirby Studios
Re: [GM7] 8-Way Movement?
« Reply #17 on: October 26, 2010, 03:42:15 am »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
Don't need to think of it using trig, just some simple vector mathematics (you get the same value but this one may be easier to understand).

1^2 = 1 = i^2 + j^2
Diagonal so i = j
1 = 2(i^2)
(1/2) = i^2
(1/2)^(1/2) = i

root(0.5) = 0.70710678118654752440084436210485, but you could just approximate that to 0.71 or 0.707 if you preferred.

Due to the use of floating points you'll get a tiny "rounding error" creep into things but it's negligible for these purposes. Obviously the more accurate a value the less rounding error but again, negligible.

I'll call whatever value you use for the root of 0.5 "n" for now. and the speed "s"

Up: x, y - s
Down: x, y + s
Left: x - s, y
Right: x + s, y

Up+Left: x - sn, y - sn
Up+Right: x + sn, y - sn
Down+Left: x - sn, y + sn
Down+Right: x + sn, y + sn

Editors Note: I have had something in the region of 5 vodka and red bulls, two tequila shots, half a bottle of white wine and some as-yet-unidentified licorice-flavoured drink a friend handed me tonight. Not a lot really, but on a close-to-empty stomach...well, as far as I can tell I've sobered up enough to be reasonably sure as to my maths here...maybe I've even hit the Ballmer Peak, who knows? :)
« Last Edit: October 26, 2010, 02:28:59 pm by TheDarkJay »
Logged
Re: [GM7] 8-Way Movement?
« Reply #18 on: October 26, 2010, 05:29:58 am »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
trig or vector graphics. It does not matter, because the only thing you add to your game is the final approximated value. And I think Mirby is smart enough to use a calculator and hit some buttons.
Logged
Re: [GM7] 8-Way Movement?
« Reply #19 on: October 26, 2010, 02:28:22 pm »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
True. I just thought he may find the vector way easier to remember and understand (It's better to understand the code you use than blindly copy and paste, after all).

As for the box

top left corner of object is placed at a random position between top left corner of box's position and the bottom right corner of the box's position - the width of the object - 1.

Pseudocode:
object.x = random(box.x, box.x + box.width - object.width - 2);
object.y = random(box.y, box.y + box.height - object.height - 2);
« Last Edit: October 26, 2010, 02:33:12 pm by TheDarkJay »
Logged
Pages: [1] 2   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.177 seconds with 75 queries.

anything