MFGG phpBB Message Boards Archive
https://phpbb.mfgg.net/

Engine / Tutorial Topic
https://phpbb.mfgg.net/viewtopic.php?f=8&t=12077
Page 10 of 12

Author:  Im the Red Spy [ Mon Feb 17, 2014 8:41 pm ]
Post subject:  Re: Engine / Tutorial Topic

Name: Smooth circular collision detection
Program: All versions of GameMaker (I think)
User: Im the Red Spy
Registered: I don't think so.

Syntax: [ Download ] [ Hide ]
Using gml Syntax Highlighting
//By Im the Red Spy
//execute this script via: scr_collision(object being collided with, radius of collision) or whatever you decide to name it. eg (collide(oSolid,16))
//This can be put in either a collision event or the step event. Works best in a step event.
var object, radius; //object
object=argument0;
radius=argument1
for(col=0;col<360;col+=20) //A for loop checks for every direction that is a multiple of 20 (0, 20, 40...etc)
//20 can be changed to higher numbers (faster but probably less efficient) or lower numbers (slower but more precise)
    {
    while(position_meeting(x+lengthdir_x(radius,col),y+lengthdir_y(radius,col),object)) //checks for the object colliding at a spot around the circle for which radius defines
        {
        x-=lengthdir_x(.2,col); //move the object back according to the direction (col)
        y+=lengthdir_y(.2,col); //move the object back according to the direction (col)
        }
    }

Author:  Magnemania [ Mon Feb 17, 2014 9:23 pm ]
Post subject:  Re: Engine / Tutorial Topic

I don't understand the intention of the above code. For determining if two circular regions are colliding, just see if the distance between their centers is smaller than their combined radii. If object1 has a radius r1 and object2 has a radius r2,

return (point_distance(object1.x,object1.y,object2.x,object2.y) < r1+r2)

If you want this object to move away from the other object until it no longer collides, simply do (in this example)

Syntax: [ Download ] [ Hide ]
Using gml Syntax Highlighting
intersectingDistance = point_distance(object1.x,object1.y,object2.x,object2.y) - (r1+r2)
if (intersectingDistance <= 0)
 return false;
angle = degtorad(point_direction(object1.x,object1.y,object2.x,object2.y))

x -= cos(angle)*intersectingDistance
y -= sin(angle)*intersectingDistance

return true;

No looping required.

Furthermore, why are you inconsistent with how you determine x and y components of vectors? In the while loop, you use lengthdir_x() and lengthdir_y() to find the vector (which does not require converting to radians) yet you use cos() and sin() to determine the vectors when moving back... x -= lengthdir_x(0.2,col) and y -= lengthdir_y(0.2,col) would've done the same thing with far fewer operations and less conversion gobbledygook. And degtorad is a far better way to convert degrees to radians than multiplying by pi/180, as the function takes into account how radians (in Game Maker) go clockwise, rather than counterclockwise as with degrees.

...And while I'm at it, here's a more legible version of my previous moving-closer-to-another-angle script:

Name: moveToAngle(startDirection,goalDirection)
Program: GameMaker 8.0 (Should be fine in later versions, too)
User: Magnemania
Registered: No

Syntax: [ Download ] [ Hide ]
Using gml Syntax Highlighting
//argument0: current direction
//argument1: direction to face

compareDirection = argument1;
compareDirection -= argument0;

if (compareDirection < 0)
   compareDirection = 360+compareDirection;

if (compareDirection > 180)
    plusMinus = -1;
else
    plusMinus = 1;

return plusMinus;

Author:  Im the Red Spy [ Mon Feb 17, 2014 10:23 pm ]
Post subject:  Re: Engine / Tutorial Topic

Maybe its just my interpretation of your post but it sounded a lot more harsh than it should have. Also, its not for two circular regions. just a circular object (for example, a player) colliding with an object (for example, a wall, or any type of solid). Ill fix my inconsistencies though

Author:  Magnemania [ Mon Feb 17, 2014 10:42 pm ]
Post subject:  Re: Engine / Tutorial Topic

Im the Red Spy wrote:
Maybe its just my interpretation of your post but it sounded a lot more harsh than it should have.


My apologies, I suppose my suggestions may have come off as a bit too aggressive.

Quote:
Also, its not for two circular regions. just a circular object (for example, a player) colliding with an object (for example, a wall, or any type of solid).


Hmm...I can't immediately think of any better solutions, but I think that there might be a better way to go about this. The way the angles are currently determined gives a bias towards moving in a circular fashion; if I were, say, moving up into a rectangular wall and I was using this method to avoiding colliding with it, I would end up sliding to the left, as (unless I was moving a single pixel at a time and 90 would be one of the values col becomes) I would detect the wall between 90 and 80 degrees or so, and the circular object would get pushed down and slightly to the left. While this would probably be a small amount, this could eventually result in quite a lot of leftward movement over time. It's a bit imprecise.

Author:  Im the Red Spy [ Mon Feb 17, 2014 11:14 pm ]
Post subject:  Re: Engine / Tutorial Topic

I believe the issue could be fixed by selecting a factor of 90, such as 15. Honestly, the code I made is imperfect. I'm still trying to find ways to make it better. It works speedwise and it does what I want it to, but it can be slightly jittery at times. Its not noticable in 2d, but in a 3d game I'm working on, the collisions can appear to shake back and forth. All i can do is lather rinse and repeat

Author:  Gatete [ Sun Mar 02, 2014 10:27 am ]
Post subject:  Re: Engine / Tutorial Topic

Making enemies respawn at the start position

1.- Create an object with these variables on it.

Syntax: [ Download ] [ Hide ]
Using gml Syntax Highlighting
move = 0;
respx = xstart; //Set the 'x' position where the enemy will respawn.
respy = ystart; //Set the 'y' position where the enemy will respawn.
respobj = object_index; //Sets the object that will appear after respawning. (I recommend using the name of the original object.)


2.- Add this code on 1 of the step events of the object.

Syntax: [ Download ] [ Hide ]
Using gml Syntax Highlighting
if (move == 0) {
    if (x > view_xview[0]-32)
    && (y > view_yview[0]-32)
    && (x < view_xview[0]+458)
    && (y < view_yview[0]+272) {
        move = 1;
        //Add here some movement code and make sure 'move = 1' is present on the code, otherwise it will not work.
    }
}
else if (move == 1) {
    if (x < view_xview[0]-32)
    || (y < view_yview[0]-32)
    || (x > view_xview[0]+458)
    || (y > view_yview[0]+272) {
        move = 2;
        instance_destroy();
        respawn = instance_create(respx,respy,obj_enemy_respawn)
            respawn.npc = respobj;
    }
    //Add here the movement code.
}


3.- Create a new object and add this code on the step event.
Syntax: [ Download ] [ Hide ]
Using gml Syntax Highlighting
if (x < view_xview[0]-32)
|| (y < view_yview[0]-32)
|| (x > view_xview[0]+458)
|| (y > view_yview[0]+272) {
    instance_create(xstart,ystart,npc);
    instance_destroy();
}


4.- If you want to make the original enemy appear on the start position, do this.
Syntax: [ Download ] [ Hide ]
Using gml Syntax Highlighting
enemy = instance_create(x,y,obj_enemy)
with (enemy) {
    move = 1;
    respx = respx;
    respy = respy;
    respobj = respobj;
}


Follow these steps and voila, your enemy objects will respawn on the start position :)

Author:  SunRaiZ [ Mon Mar 03, 2014 12:26 am ]
Post subject:  Re: Engine / Tutorial Topic

Is OSMBE2 download link fixed?

Author:  kremling [ Tue Mar 04, 2014 10:06 pm ]
Post subject:  Re: Engine / Tutorial Topic

Tiled Backgrounds w/ Movement

Want to have a moving background that is tiled throughout a room, throughout an entire level, and have full control over its movement and speed? Here is an example just for that.

The example below is for vertical tiling. I've yet to try both horizontal and vertical at the same time, however. It should still work.

Syntax: [ Download ] [ Hide ]
Using gml Syntax Highlighting
// In the Create Event of an object designated for backgrounds
step_vert = 0;
spd = 1;    // speed of the moving background
bg = background
 

Syntax: [ Download ] [ Hide ]
Using gml Syntax Highlighting
// In the End Step Event of an object designate for backgrounds
step_vert += spd;
if (step_vert > background_get_height(bg) step_vert = -background_get_height(bg);
 

Syntax: [ Download ] [ Hide ]
Using gml Syntax Highlighting
// In the Draw Event of an object designated for backgrounds
for (i=-background_get_height(bg) + step_vert; i < room_height; i += background_get_height(bg)) {
    draw_background(bg,view_xview[0],view_yview[0],i);
}
 

Author:  Im the Red Spy [ Tue Mar 04, 2014 11:31 pm ]
Post subject:  Re: Engine / Tutorial Topic

Is there any benefit in using that as opposed to background_hspeed[0...] and background_vspeed[0...]?

Author:  kremling [ Wed Mar 05, 2014 6:53 pm ]
Post subject:  Re: Engine / Tutorial Topic

I'm currently unaware of any right now. I made this because I forgot game maker actually had a method for doing it. :P

I guess if you had more than 8 backgrounds then this could be helpful. Seeing as game maker only allows their assigned methods to hold [0-7].

Author:  Gatete [ Sun Mar 09, 2014 4:45 pm ]
Post subject:  Re: Engine / Tutorial Topic

@kremling: I added your tutorial on the first post, it took long because I was kinda busy.

Author:  SunRaiZ [ Fri Mar 14, 2014 10:14 am ]
Post subject:  Re: Engine / Tutorial Topic

Sorry to ask,but when will u fixed the OSMBE2 link?

Author:  Gatete [ Fri Mar 14, 2014 10:49 am ]
Post subject:  Re: Engine / Tutorial Topic

SunRaiZ wrote:
Sorry to ask,but when will u fixed the OSMBE2 link?


It works for me.

Author:  SunRaiZ [ Sat Mar 15, 2014 7:30 am ]
Post subject:  Re: Engine / Tutorial Topic

But it's broken,right?

Author:  Gatete [ Sat Mar 15, 2014 8:00 am ]
Post subject:  Re: Engine / Tutorial Topic

SunRaiZ wrote:
But it's broken,right?


Nope

Author:  Hypernova [ Sat Mar 15, 2014 9:37 am ]
Post subject:  Re: Engine / Tutorial Topic

SunRaiZ wrote:
But it's broken,right?

I think that file hosting website might be block by Vietnamese Government. You might need to ask someone nicely to transfer a file via email or something.

Author:  SunRaiZ [ Sat Mar 15, 2014 10:20 am ]
Post subject:  Re: Engine / Tutorial Topic

Can you upload it on Mediafire?

Author:  Mors [ Sat Mar 15, 2014 12:39 pm ]
Post subject:  Re: Engine / Tutorial Topic

2shared is blocked on my country too. BUT I USE DNS! (evil laugh)
http://www44.zippyshare.com/v/99243062/file.html

Author:  Gatete [ Sat Mar 15, 2014 12:46 pm ]
Post subject:  Re: Engine / Tutorial Topic

Mors wrote:
2shared is blocked on my country too. BUT I USE DNS! (evil laugh)
http://www44.zippyshare.com/v/99243062/file.html


Mors, I already added a mirror on the first post.

Author:  onpon4 [ Sat Mar 15, 2014 1:00 pm ]
Post subject:  Re: Engine / Tutorial Topic

Mors wrote:
2shared is blocked on my country too. BUT I USE DNS! (evil laugh)

Uh... what? I use the domain name system too, but I don't get what it has to do with censorship...

One way to get past censorship of the Internet, by the way, is Tor:

https://www.torproject.org/projects/torbrowser.html.en

Page 10 of 12 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/