Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Ruby point a event in a direction (8way)

I'm working on both a C++/SDL and RPG Maker Xp abs system and while most of my code transfered over nicely my code for pointing a direction did not.

Basically for a abs when you hit your enemy it will point the direction where the Hero's X/Y is located. So for an example I'll basically just show you how it'd be done in Game Maker(won't give out my C++ code yet)

while(enemy_hit=true) {
speed=2;
direction=point_direction(herox,heroy,x,y)
}

should be simple enough.

Edit: did some playing around in C# this is what I'm thinking but don't know RPG Makers api well enough to be certain.

x = x + cos(hero_x) * direction
y = y + sin(hero_y) * direction
dt = ((x2 - x)^2 + (y2 - y)^2) ^ 0.5
new position
x = x + (x2 - x) * (direction / dt)
y = y + (y2 - y) * (direction / dt)
 
I just finished a script in GM that translated the point_direction function of course ruby doesn't have this so it'd have to be scripted.

If anyone want's to help with this that'd be awesome, and I wish RM had a direction function that didn't just go up to 8 but had all 360 degrees..

Code:
x1 = argument0;

y1 = argument1;

x2 = argument2;

y2 = argument3;

 

dt = sqrt(sqr(x2 - x1) + sqr(y2 - y1));

dir = 0;

 

if (x2 > x1){

   if (y2 > y1){

       dir = ceil(arcsin((y2 - y1)/dt));

   }else{ 

       dir = ceil(360 - arcsin((x2 - x1)/dt));

   }

}else{

   if (y2 > y1){

       dir = ceil(180 - arcsin((y2 - y1)/dt));

   }else{

       dir = ceil(180 + arcsin((x2 - x1)/dt));

   }

}

return (((arctan2(argument0-argument2,argument1-argument3)* (180/pi))) + 450) mod 360;

x1 and y1 = hero's x/y, x2 and y2 = enemy ai's x/y
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top