Jump to content


Photo

Distance To Object X,y Values?


  • Please log in to reply
12 replies to this topic

#1 Darkflamewolf

Darkflamewolf

    GMC Member

  • New Member
  • 61 posts

Posted 10 May 2009 - 10:09 PM

I'm making a Mega Man game and there are some high-flying overhead enemies that depending on how close they are to MM's x variable, will drop bombs or whatnot. Thing is, the 'distance to object' function doesn't seem to like it when I specify Mega_Man.x as the variable in question, but does like just plain Mega_Man. But its highly inaccurate and if I set it too high. it drops its payload far before MM even gets close to the enemy. So is there a way for the enemy object to check if its close to either specifically the x or y values of the player and do actions based on that? (This also would apply for enemies that hop out of pits and hit you, thus giving them an easy kill as you fall into the pit after being damaged)

Edited by Darkflamewolf, 11 May 2009 - 12:25 AM.

  • 0

#2 Floor_

Floor_

    GMC Member

  • GMC Member
  • 62 posts

Posted 10 May 2009 - 10:22 PM

Yep, just use point_distance(x1,y1,x2,y2).

Like this:
if point_distance(x,y,Mega_Man.x,y)

  • 0

#3 Darkflamewolf

Darkflamewolf

    GMC Member

  • New Member
  • 61 posts

Posted 10 May 2009 - 10:29 PM

well the y value I don't care about, can I safely ignore it and just be (x, Mega_Man.x)? Or does it not work this way? How can I make that statement true when I simply don't care about one value? Like the y value, MM can be as close or as far away from the enemy for all I care y wise, but x wise, he can't.
Thanks for the help already though! You're already pointing me in the right direciton. A demo level should be forthcoming once I get all my questions answered, then people can judge if my project is worth pursuing after that. :D

Edited by Darkflamewolf, 10 May 2009 - 10:30 PM.

  • 0

#4 FoxInABox

FoxInABox

    GMC Member

  • GMC Member
  • 5586 posts
  • Version:GM:Studio

Posted 10 May 2009 - 10:34 PM

maybe like this
if abs(Mega_Man.x-x) <5 {

  // if 5 pixles from MM's x cordinate

}

  • 0

#5 Darkflamewolf

Darkflamewolf

    GMC Member

  • New Member
  • 61 posts

Posted 10 May 2009 - 10:40 PM

remind me again what 'abs?' is again? I'm more familiar with drag n drop terms. lol But thanks for the code! :D
  • 0

#6 Floor_

Floor_

    GMC Member

  • GMC Member
  • 62 posts

Posted 10 May 2009 - 10:46 PM

well the y value I don't care about, can I safely ignore it and just be (x, Mega_Man.x)? Or does it not work this way? How can I make that statement true when I simply don't care about one value? Like the y value, MM can be as close or as far away from the enemy for all I care y wise, but x wise, he can't.
Thanks for the help already though! You're already pointing me in the right direciton. A demo level should be forthcoming once I get all my questions answered, then people can judge if my project is worth pursuing after that. biggrin.gif


point_distance(x,y,Mega_Man.x,y) should only check the horizontal distance, since its checking from and to the same y value, only the x values are different.

If you tried it, did it not work?
  • 0

#7 Darkflamewolf

Darkflamewolf

    GMC Member

  • New Member
  • 61 posts

Posted 10 May 2009 - 10:50 PM

ah, I was wanting sort of an explanation of how to use it. Like do I put variables/numbers in where x, and y are?
That's kind of my problem. I'm insanely new to GML but pretty coherent with Drag n Drop. So excuse my noobness when it comes to asking stupid questions about GML. :D
So I merely put that line of code:
if point_distance(x,y,Mega_Man.x,y)
into the enemy object and then do my little
{
do stuff here
}
afterwards and everything should be fine?
Just asking how specifically its to be used? For if I want to check a 'y' variable, and not the x variable, do I merely swap the x and y positions in that statement? Thanks for the help! I'm learning! :(
  • 0

#8 Dusk

Dusk

    GMC Member

  • New Member
  • 8 posts

Posted 10 May 2009 - 10:50 PM

abs() means 'absolute'.
Its a mathematical term that basically changes any value into a positive value.

The absolute value of 2 is 2.
The absolute value of -5 is 5
  • 0

#9 Tarik

Tarik

    GMC Member

  • GMC Member
  • 2729 posts
  • Version:GM:Studio

Posted 10 May 2009 - 11:07 PM

That's right Darkwolf:

if (point_distance(x,y,Mega_Man.x,y) < 50)
{
drop bomb
}


Says, if it's less than 50 pixels away horizontally it'll drop a bomb. You can change the 50 of course. Or change it to an equal-sign or a greater than sign or anything really.
Though in this case point_distance isn't really required, you could simply write:

if (x > Mega_Man.x-25 and x < Mega_Man.x+25)
{
drop bomb
}

As you can see point_distance is a bit easier to use. Good luck!
  • 0

#10 Darkflamewolf

Darkflamewolf

    GMC Member

  • New Member
  • 61 posts

Posted 10 May 2009 - 11:14 PM

cool, I got the x variable when less than 50! Now how can you swap point_distance to check for y values between enemy and player? (I only have a few enemies that require this, not many) I highly doubt if I use the same code as above it'd work so well for y value comparisons. In which case, I may want to do the second method you chose.

EDIT: Wait a minute, I think I'm getting it:
If I want merely the y value only, it'd be like this right?
if (point_distance(x,y,x,Mega_Man.y) < 50)
{
drop bomb
}
And for Drag n Drop purposes, I can do this all in a Var (circle) function yes?

Edited by Darkflamewolf, 10 May 2009 - 11:17 PM.

  • 0

#11 NACGames

NACGames

    GMC Member

  • New Member
  • 151 posts

Posted 10 May 2009 - 11:18 PM

If I understand correctly, you want exactly the same thing but with y variable?

In that case:

If

if (point_distance(x,y,Mega_Man.x,y) < 50)
{
drop bomb
}

is the finds the distance between x variables,

if (point_distance(x,y,x,Mega_Man.y) < 50)
{
drop bomb
}

would do the same for y variables.

EDIT: Beat me to it yourself :D.

Edited by NACGames, 10 May 2009 - 11:19 PM.

  • 0

#12 Darkflamewolf

Darkflamewolf

    GMC Member

  • New Member
  • 61 posts

Posted 10 May 2009 - 11:20 PM

thanks, I understand now, I'm currently not on in a position to test it out now, but when I get home, I'll work through it and get back to let you all know if it worked or not. (Chances are it will, with such grand help you've all given me.) I still have one unsolved issue a few pages back (page 4) about MM contacting moving platforms that are already on a path. :( Would be awesome if my noob GML questions were answered there too. :D Thanks everyone! You've all been so nice! :P
With help like this, I'll soon be having a demo level ready for you all to play soon! :P
EDIT:

These work! At least, sorta. Better than they did before. When the enemy is coming from the right and MM from the left, it does what its supposed to do, drop the bomb inches in front of MM's nose so that he walks into it.
But on the flipside, if the enemy is coming from the left and MM from the right, the enemy drops the bomb a good distance away, not sure why that is.

Edited by Darkflamewolf, 11 May 2009 - 12:25 AM.

  • 0

#13 Arusiasotto

Arusiasotto

    GMC Member

  • GMC Member
  • 759 posts
  • Version:GM:Studio

Posted 25 November 2010 - 06:04 PM

thanks, I understand now, I'm currently not on in a position to test it out now, but when I get home, I'll work through it and get back to let you all know if it worked or not. (Chances are it will, with such grand help you've all given me.) I still have one unsolved issue a few pages back (page 4) about MM contacting moving platforms that are already on a path. :( Would be awesome if my noob GML questions were answered there too. :D Thanks everyone! You've all been so nice! :P
With help like this, I'll soon be having a demo level ready for you all to play soon! :P
EDIT:

These work! At least, sorta. Better than they did before. When the enemy is coming from the right and MM from the left, it does what its supposed to do, drop the bomb inches in front of MM's nose so that he walks into it.
But on the flipside, if the enemy is coming from the left and MM from the right, the enemy drops the bomb a good distance away, not sure why that is.



x0 and y0 seem to sit in the center of the sprite frame. Is your megaman sprite kinda off center?

By and by, working on a megaman game myself. this topic may have helped me as well.


EDIT: Ok, yeah, getting the problem myself. If you approach it from the left, it triggers at the right distance,but if you come from the right, you have to be inside the sprite before it tries to melee. think it has something to do with the origin setting in sprites... anyone know how to fix it?

Edited by Arusiasotto, 25 November 2010 - 08:29 PM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users