Am i the only one who is tired of seeing Spalding's collision code spammed everywhere?

Discussion in 'Game Design, Development And Publishing' started by Alessio, Apr 17, 2018.

  1. Alessio

    Alessio Member

    Joined:
    Jun 22, 2016
    Posts:
    109
    Hello all. Few may remember me as someone who used to post here a little over a year ago so i'm not really new here. I wanted to learn GML but had temporarily abandoned the objective to pursuing something that, sadly, i failed miserably. Closing these parentheses, i decided to pick learning GML again, this time with GM:S2. I tried to make a game in the first place, but i ultimately realized that i need to read the manual, which is something i'm currently doing, but that's not the main issue.

    You see, when i don't know how to do something, i usually look for tutorials, but i have to stick with videos because written ones are truly rare nowadays unless i go in the tutorial section of this forum. For example, i was very interested in collisions, and that's where i want to vent one frustration i have. Most of you can recognize this collision code, which is object-based:
    Code:
    if place_meeting(x+hspd, y, obj_wall)
       {
       while !place_meeting(x+sign(hspd), y, obj_wall)
       x += sign(hspd);
       hspd = 0;
       }
    Yeah, that's the one that was made by Spalding but i'm not a fan of it because of the object's bounding box not being correctly aligned with walls when velocity is set to a non-integer. In brief, it's not really accurate.

    The problem, though, is that this code is literally spammed everywhere!!! To be honest, that's not Spalding's fault but if you don't believe me, search "game maker collision" on Youtube or even on your preferred search engine, and see! I would expect 20+ or 30+ youtubers to use different approaches from each other, not everyone of them using the same damned code directly copied from Spalding! Even Heartbeast does that, to not mention his handling of physics not even a beginner like me could find barely acceptable.

    When i checked out few written tutorials or few ready-made engines (like Hello's or Martin Piecyk's, the latter being used for Spelunky), i could find a different way to handle collisions every time, but whenever i check video-tutorials (which, to my knowledge, outnumber written ones), it's always the same damned code copied and pasted from Spalding's tutorials!!!

    I mean, am i the only one who finds this frustrating??? If i want to learn something, not only should i be driven to get knowledge and to use my brain, but also able to see multiple approaches to the same problem, but the majority of tutorials fail to do that.

    In fact, it's almost like they do tutorials only to make an impression! Spalding has already made that collision code, why don't you write your own code instead of copying and pasting from Spalding? Is Spalding's codes the only thing you know? I would feel ashamed if i made a tutorial showing the same code written by someone else again and again.

    Sorry for the long read and for sounding somewhat like a jerkwad right after waking my dormant account up, but this is something that has been haunting me for a while, and i think this negative phenomenon (or trend?) needs to be called out whenever possible. I also know the topic about bad learning resources has already been open various times but this time i want it be exposed from a beginner's perspective. That's because i can't be the only novice user who is frustrated by uninformative tutorials.
     
  2. mimusic

    mimusic Member

    Joined:
    Nov 24, 2017
    Posts:
    98
    I don't know about "spammed," but it's definitely commonplace to see a snippet or a link to Shaun's code when somebody's asking about elementary tasks like collisions. The only problem I really have with this practice is that the snippets tend to be designed for general use and need to be adapted to the specific game they're being implemented in, but nobody really says this when sharing the code, so nobody bothers to personalize or adapt it. The main reason the code is popular and frequently used is because it probably works for a majority of people. A bit of a "if it ain't broke, don't fix it" situation.

    Here's some advice that I hope helps you and anybody else who shares your sentiment: Tutorials will be more effective to your general programming knowledge if you take a step back and view them abstractly before applying them. Being able to map out the process without actually using code is a skill that'll take you very far. For example, in the snippet you shared, the abstract idea is:
    Code:
    if place_meeting(x+hspd, y, obj_wall)
        {
        while !place_meeting(x+sign(hspd), y, obj_wall) {
            x += sign(hspd);
            }
        hspd = 0;
        }
    
    - - - - - - - - - - 
    
    if there is a wall object in the spot I'm moving towards...
        {
        while I haven't actually touched that wall yet...
            move me over by one pixel, until the wall is within one pixel of distance
        stop my horizontal movement.
        }
    
    Just by doing this, we can identity problem areas if your focus is on high-precision; primarily, the act of moving over by a whole pixel makes non-integer velocities a bit buggy. To remedy, you might change the movement from one pixel to one-hundredth of a pixel, where players are much less likely to notice the gap. Perhaps, instead, you'll say "don't bother moving me until I hit the wall; Instead, instantly snap me to a position relative to the wall" -- however, note that this solution requires you to be more mindful of the origins and bounding boxes of all objects colliding, and gets problematic when multiple walls are overlapping in a near vicinity.

    By stepping back and using tutorials for their general approaches AND the line-for-line code provided (as opposed to just focusing purely on the code), you're much less likely to get trapped by the lack of diversity in the tutorials, and also much more likely to grow as a programmer.

    I agree that more diversity among publicly shared solutions would be nice, but what we have is what we have. The best you can do is put yourself in a place where the narrow range of shared solutions doesn't hinder you. Then, perhaps you can create your own tutorials that show novel approaches to familiar problems, thus introducing the diversity that you wish to see.
     
    M3SS_21, JeffJ, Bentley and 3 others like this.
  3. Arheddis Varkenjaab

    Arheddis Varkenjaab Member

    Joined:
    Apr 18, 2018
    Posts:
    12
    Unfortunately the Yoyogames forums don't have control of the rest of the internet.
     
    Bentley, sitebender, Yal and 2 others like this.
  4. BaBiA Game Studio

    BaBiA Game Studio Member

    Joined:
    Jun 20, 2016
    Posts:
    845
    Who's to say that Shaun was the first person to write their collision code that way? Just because he put his online ahead of other people does not necessarily mean that he came up with it first.
    If it irks you so much, perhaps you would be better getting in touch with these other tutorial providers and seeing if they have other ways of doing their collision code so that you can learn to do it differently too?
     
  5. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,582
    The purpose of this code snippet and how it's being applied are sometimes far apart.

    The code is intended for beginners who just started out programming and have never programmed collisions, therefore cutting down on anything that would likely exceed a beginner's capabilities of understanding (or interest). With newbies, it's usually a matter of "I want it to work", not "I want it to work the best way possible" - they weren't even able to do it at all before watching the video, so that's already an achievement.

    Often, it's merely a wish that they will notice that it's not perfect and then try to adapt it on their own. Few people do this when they're not self-sufficient yet. I commend you for approaching your way of learning from tutorials like this, but it's a rarity - at least when one takes the Programming forum as an example - and the natural thing to do is to write tutorials (or create tutorial videos, as the trend dictates) which will be watched and used by the majority of the people in the target audience.

    To me, it seems like a big threat here is that Youtube and similar websites keep track of a handful of technically meaningless but somehow still personal values like views, likes and subscribers, and there seems to be a tendency in video creators to do whatever it takes to get those numbers to increase. I don't personally agree with this approach, since I help even if there's just a single person that will be able to benefit from it, but if all it takes to get that sweet meaningless number up by a few hundred more, someone will probably make a video about it, and multitudes of newbies who just want the damn program to stop throwing errors in their face and RUN THE DAMN THING will likely flock to it in droves, given enough prior exposure of the channel, creating a snowball-like effect that just keeps itself going once it has started.
     
    Bentley, mimusic, Yal and 3 others like this.
  6. Niels

    Niels Member

    Joined:
    Jun 22, 2016
    Posts:
    819
    If it works... I think many people struggling with collisions (which is one of the biggest obstacle of many beginner gamemakers) rather have this code than having to write their own... and it's better than in Unity where everyone gets told to make solid objects and depend on the physics system to take care of evrything.
     
    Simon Gust likes this.
  7. RichHopefulComposer

    RichHopefulComposer Member

    Joined:
    Jun 20, 2016
    Posts:
    1,377
    No, it doesn't bother me at all, because I studied the basics of programming, and used some logic to write my own collision code. How is this a problem big enough to need a thread about it? Do some research on general programming and then write your own collision code if you hate Spalding's code so much. Tutorials are supposed to be a jumping off point anyway. If Spalding's code is the only collision code you can find, decide why you dislike it and then improve it.

    And then make a tutorial about how you did it. :p
     
    M3SS_21, JeffJ, Bentley and 3 others like this.
  8. breakmt

    breakmt Member

    Joined:
    Sep 13, 2016
    Posts:
    168
    I don't see a problem. If you have better solution - you can post it here or somewhere else... And who knows maybe in 2-3 years someone will start thread about your collision code spammed everywhere :)
     
    Last edited: May 31, 2019
  9. sylvain_l

    sylvain_l Member

    Joined:
    Sep 18, 2016
    Posts:
    705
    Well the insentive for youtube creators are that more (minutes) views = more revenue. So it kinda imply that the content is not design to make the viewer an autonomous coder but would more favorise to make it dependent so he comes back to watch more.
    (more revenue for creator = also more revenue for google; that explain while popularity is so much more promoted over quality)

    If you like accuracy...
    first, it's a basic piece of code to do basic collision test. Already seen the same kind of algorithm used in darkbasic back in the days (nearly 2 decades ago) and I can assume that it was already used long before in C and older programming languages. And I'm confident there is more chance that the guy who created gamemaker was the first to write that in GML not Spalding (I could be wrong :p).

    and about that code, personnally it's more it's inefficiency then its presumed innaccuracy(*) that bother me. But honestly, it does the job well enough for the basic use case it should be used for (which in general is the case in the basic tutorials you see it).
    problems is how it is reused then XD.

    (*) the smallest unit on your screen is the pixel. generally in standart situation the rounding of collions and display should match (so no "visible inaccuracy").
     
  10. Simon Gust

    Simon Gust Member

    Joined:
    Nov 15, 2016
    Posts:
    3,166
    That sounds like a conspiracy theory. I don't think they make that much money anyway, but let's not get into that :)
     
  11. sylvain_l

    sylvain_l Member

    Joined:
    Sep 18, 2016
    Posts:
    705
    it's not compiracy, it's what hapens in a free market under the business model of google. (I didn't mean also that spalding, heartbeast, etc... are malicious and want to to make viewers dumber, it's the opposite as they want to help people to learn gamemaker)

    data about earnings on youtube a rare, but (he has much less suscribers than spalding or heartbeast)
    you of course need to get enough popularity (just reaching 1M min of view time/month isn't that easy. For not that much of revenue of a few hundreds dollars (and if you then go to the time spent to create that content... earning/h of work are low if you don't reach the 250k-1M suscribers - I don't know where is the treshold, but it's certainly high and only a few stars reach it - note that when you reach it, you get other stream of revenue also via sponsors&co).

    But even so, it certainly influence the content creation toward a trend for popularity. Because that's what google rewards too.
     
    Niels and sitebender like this.
  12. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,393
    May be true for others (endlessly making platformer tutorials with no real value IMO).
    But I at least try to make valuable tutorials...

    Also my videos are short, but I still get good CPM...
     
  13. Alessio

    Alessio Member

    Joined:
    Jun 22, 2016
    Posts:
    109
    Well, i thought it was common knowledge that the code was mainly proposed by Spalding. But i may be incorrect, who knows. But that's not truly important. For your second point, i highly doubt they'd have any interest in that, otherwise they would have already done that without me asking them.

    And that's truly disarming. I didn't say this in my OP because i was writing a wall of texts as tall as this Crash level and had to cut down content, but only now i created an "experiment" project file where i can experiment and learn coding with GML (edit: even because i don't think "remaking Pong" would be satisfying or productive at all, i want to make platformers, for God's sake). I started with collisions (because they're one of the most important things in games involving walls and movements) and i've definitely steered away from Spalding's approach. It's true that not many people do this and would rather take the easy path, but that shouldn't be a reason these tutorial creator keep doing unhelpful generic content. As you said:
    That's definitely the approach i and, i think, many popular 16bit games have took (Mario, Metroid, Sonic, Megaman, ecc...), to not mention the fact they implemented slopes in a mathematical way instead of using a bitmask-like slope with precise collisions (which is also not fine when you want to see smooth sub-pixel movements if you use pixel-art). To solve the multiple walls problem, one shall loop through all detected walls/tiles through the collision code and check which one is the closest. But there is also this interesting piece of documentation that is dedicated to platformers, but i think it can be used for top-down games as well.

    Why not discuss about it, though? Anyway, when i'll get better at GML, that's probably what i will do. But i'd be as bold as brass if i started writing tutorials without proper experience. Sadly, this is what a lot of youtubers copying Spalding's code and pasting it on their own tutorials do. And that's what i basically called out here.

    May be true for Spalding... but i don't know if it's the same for Hearbeast: he even charged money for stuff you easily find in other tutorials (containing Spalding's code) or even for stuff that is written better in the manual.
     
    Last edited: Apr 18, 2018
    RichHopefulComposer likes this.
  14. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,450
    There are also people that put marketplace assets for sale that are exactly the same (in end product) as what can be found in the tutorials.
    I guess they are hoping to earn a bit of cash from the people that don't bother to look around or use Google.
     
    sitebender likes this.
  15. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,393
    heck, I have found my tutorials uploaded as assets line of code by line of code, even the comments!
     
    sitebender and rIKmAN like this.
  16. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,556
    I'm actually using this approach myself (and might have picked it up through osmosis of browsing the GMC), it's not like it's an infinite source of ancient evil or anything like that. Tools are just tools, and if they get the job done, who cares if they're inefficient? I like my objects representing the things they represent, that speeds up level creation and problem solving :p
    (A huge chunk of the time I spend at work is browsing StackOverflow, for that matter, so it's not like only GM noobs benefit from copypasting known solutions)

    The main problem with this approach is that GM's default behavior with objects is a bit unreliable (you can't control the order step events are executed, for instance) so it easily leads to problems if you want moving platforms. Also, slopes require more complicated collision masks. It's a quick and dirty approach that solves simple problems, and it might be a crutch that traps people, but of course nobody highlights that (most people that do tutorials probably are just looking for quick and dirty solutions, anyway). As Nassim Nicholas Taleb puts it in The Black Swan, any counterexamples are buried in the graveyard of silent evidence.
     
    Bentley likes this.
  17. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,450
    Some people have no shame, and other people are too lazy / ignorant to bother to research or learn how to do things themselves - perfect storm.

    PS. I hope you reported them.
     
    Guest User and sitebender like this.
  18. Yal

    Yal GMC Memer GMC Elder

    Joined:
    Jun 20, 2016
    Posts:
    3,556
    Considering they copied the comments, I'd say there's evidence suggesting they weren't lazy; it should be safe to assume they have no shame. Giving people the benefit of the doubt is normally a pretty nice course of action, but you should be careful not to let people abuse your niceness either...
     
  19. rIKmAN

    rIKmAN Member

    Joined:
    Sep 6, 2016
    Posts:
    4,450
    CTRL+A and CTRL+C is a lot quicker/easier than hand editing code to remove comments, so I would say it suggests laziness on the part of the code thief too (and no shame or stupidity!).
    However that comment was aimed at the people purchasing the assets - they just want to pay a few dollars and have it work than bother to try and understand or learn for themselves.

    Of course I'm talking about assets that are pretty simple and covered in tutorials rather than assets that are actually worth the money.
     
  20. Alessio

    Alessio Member

    Joined:
    Jun 22, 2016
    Posts:
    109
    This is not strictly related to this topic, but i would like to comment on this: for too long i did that because that's the kind of education i have got, but some people aren't worth good manners because they're actual scumbags, and as i wrote in my last status: Life advice from me: if you start feeling like you can't trust someone anymore, you're probably right.
     
    Yal likes this.
  21. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,393
    back to the original question though.
    Its actually a very sound piece of code, so no, I'm not mad that its everywhere.
    It's the collision code I used most of the time (well, not quite, I have a few modifications), with no issues.

    Sure its not perfect, but games never are. Making it sub-pixel accurate is probably not worth the extra code and cpu time.
    If you have a sub-pixel accurate solution that is also efficient and easy to use, please share!
     
  22. zendraw

    zendraw Member

    Joined:
    Jun 20, 2016
    Posts:
    1,362
    i dont think its spawn`s code, i think ive actually seen it in the manual but not sure.
     
  23. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,393
    Its defenetly not his code. That code has been around for ages!


    Actually, if you are interested in another technique, you may be interested in this:
    Its also a very widely used piece of code, that works in a very similar way internally.
    (I did not come up with this technique, it has been floating around the GMC for as long as i have been here).
     
    sitebender likes this.
  24. Alessio

    Alessio Member

    Joined:
    Jun 22, 2016
    Posts:
    109
    Well, i know this from someone called Zach Bell, who made a game called Ink. But that alone won't allow correct sub-pixel movements, so one will have to add this too.

    The problem is that this will only support pixel-perfect movement, and if you want to have smooth movements as it happens in Shovel Knight (which is basically a "HD" pixel game), with that code it's not possible. It's also a hassle, to be honest. For my game, i would resize the application surface anyway or something similar instead (i'm still uninformed on the topic because i didn't get to that point in the manual yet), in this way i wouldn't need to tweak any movement code at all, but correct me if i'm wrong.

    As for me, i made something like this:
    Code:
    inside_wall = collision_rectangle(bbox_left, bbox_top, bbox_right, bbox_bottom, obj_square, false, false)
    player_bbox_right = sprite_get_bbox_right(spr_blue_rectangle) - sprite_get_xoffset(spr_blue_rectangle)
    if h_speed > 0 then right_detector = h_speed + sign(h_speed) else right_detector = 0
    colliding_right = collision_rectangle(bbox_right, bbox_top, bbox_right+right_detector, bbox_bottom, obj_square, false, false)
    
    if not inside_wall
        {
        if colliding_right
            {
            x = (colliding_right.bbox_left-1) - player_bbox_right
            h_speed = 0
            }
        }
    Keep in mind this is just a snippet of a longer code that takes into account every side of the moving object, corners too. This also solves problems with very fast movements as the bounding box is stretched and not shifted towards the speed's direction, but what the code doesn't do is checking which wall instance the moving object will snap to if the collision detection range detects more than one instance towards the direction it is going (as it is now, it will snap to the first created instance). Also keep in mind i was just fiddling with code in order to learn something, so it's not i have done a master job, even if the system seems to work well. Also, i forgot semicolons, lol!

    I haven't got anything else so far.
     
  25. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,393
    Yeah that's essentially the same as the bit of code you posted in your original post.
    Oh yeah, that's the kind of code I demonstrate in this tutorial (with tiles):

    and that i use in this live stream (for object - object and object - tiles collisions):
     
  26. ShaunJS

    ShaunJS Just Another Dev GMC Elder

    Joined:
    Apr 12, 2016
    Posts:
    148
    Haha.

    It is a shame that the method has an issue with non-integer movement but this is easily solved. Also pretty much anything using place_meeting has this problem! I think the only way to not run into this problem would be to snap back out of the object post collision rather than to pre-emptively detect the collision. (I could be wrong! If you know a similarly helpful and far more elegant snippet let me know!)

    As has been pointed out you can solve the issue simply by not moving at non-integers, preferably by using something that dumps out your fractions every frame while still applying them to movement in the long run.

    Code:
    hsp_final += hsp_fraction;
    vsp_final += vsp_fraction;
    hsp_fraction = frac(hsp_final);
    vsp_fraction = frac(vsp_final);
    hsp_final -= hsp_fraction;
    vsp_final -= vsp_fraction;
    
    This makes the system pixel perfect and also will solve lots of other strange sub-pixel positioning issues you might run into. Does add a few caveats to moving around though (remembering to empty vsp_fraction when jumping for example). Credit to (I believe?) YellowAfterLife for this technique.

    I use this all of the time and I haven't run into any issues with it "not looking smooth" with HD graphics. But to be fair the sub pixel inaccuracies wouldn't even necessarily be an issue if I was using HD artwork.

    And of course, no I didn't "invent" the original collision code. I certainly didn't invent the concept of "using loops in a collision check" which is just a super basic concept. I started making videos because there really weren't many out there at the time. I actually learned this stuff myself from an ancient article by SoulRed12: https://soulred12.wordpress.com/2008/05/29/soulhow-platform-game/
     
    Last edited: Apr 20, 2018
    Wraithious, Niels, Bentley and 2 others like this.
  27. kupo15

    kupo15 Member

    Joined:
    Jun 20, 2016
    Posts:
    769
    I have a question about this which I saw you use in your tile collision vid. You said don't use Round, looking for trouble...why is that? What trouble? If I'm going to be moving from xpos 10 to 15.89, do I really want to floor my speed and only move 5 pixels and add that 0.89 to the next step when I'm much closer to 6? Wouldn't it be better to move 6 then subtract 0.11 next frame? I assume this pixel difference doesn't make that much of a difference with an HD size game compared to a lower res but for the sake of correctness I wanted to ask.

    Was this warning only for tile collision and not object collision?;

    Would it be better to store your true x coor in another variable as a reference that holds your fractional position so that you can round your actual x position appropriately without losing your fraction position? The difference here is that you would be rounding appropriately and moving say 6 then 5 instead of always moving 5 then 6
     
    Last edited: Apr 20, 2018
  28. hogwater

    hogwater Member

    Joined:
    Jun 22, 2017
    Posts:
    635
    NEW LORE
     
    Bentley, Guest User and cidwel like this.
  29. Rob

    Rob Member

    Joined:
    Jul 12, 2016
    Posts:
    680
    Is it because it doesn't round to the nearest number but nearest even number? If the value is 2.5 it gets rounded down to 2, and if it's 3.5 it gets rounded up to 4. I don't know about the negative effects this would have on a game though.
     
  30. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,393
    The problem here is that you will end up with a very jittery system.

    By using an accumulator like shaun suggests you have a much steadier speed, it will only ever vary by one unit.
    If you do use the add or subtract system you describe, then the speed may vary by 2 units.
     
  31. Bentley

    Bentley Member

    Joined:
    Jun 18, 2017
    Posts:
    789
    lol

    ----------------------------------------------------------------------------------------

    I'm not saying anyone here has said anything mean about you ShaunJS , but I've seen it elsewhere.

    Anyway, my point is that I think when people say something rude, it comes from a place of jealousy. No one's forcing you to watch his tutorials.
     
    Last edited: Apr 30, 2018
  32. cidwel

    cidwel Member

    Joined:
    Jun 30, 2016
    Posts:
    122
    I like to season my salads with some spalding collision checking.
    I only add some spalding collision checking into my coffee every morning


    booo .. Pretty simple for me. It's a very simple code that just works pretty well for what it is. It is perfect for novices. If you need a deeper and accurate collision checking then probably you are not a novice and do not need a tutorial to solve collision issues
     
    Guest User likes this.
  33. sylvain_l

    sylvain_l Member

    Joined:
    Sep 18, 2016
    Posts:
    705
    2? how is that possible? all he does is move the thresold from 0 with only positive reminder to 0.5 with a reminder in [-0.5, 0,5]
     
  34. GMWolf

    GMWolf aka fel666

    Joined:
    Jun 21, 2016
    Posts:
    3,393
    say you are at 0.99 fractional and 0.2 fractional speed per frame.
    So you are now over the fraction, so you choose to add it to the position.
    not you are at 0.1 fractional. you add o.2 to get to 0.3;
    you are now under 0.5 so you choose to subtract from position.

    In one frame, you carried +1 over, in the next frame, you carried -1 over. that's a difference of 2.
     
  35. gmx0

    gmx0 Member

    Joined:
    Jul 10, 2016
    Posts:
    58
    Pretty sure I've seen that code long before Spaulding was a thing, and I've been using GM since 2004.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice