Install Steam
login
|
language
简体中文 (Simplified Chinese)
繁體中文 (Traditional Chinese)
日本語 (Japanese)
한국어 (Korean)
ไทย (Thai)
Български (Bulgarian)
Čeština (Czech)
Dansk (Danish)
Deutsch (German)
Español - España (Spanish - Spain)
Español - Latinoamérica (Spanish - Latin America)
Ελληνικά (Greek)
Français (French)
Italiano (Italian)
Bahasa Indonesia (Indonesian)
Magyar (Hungarian)
Nederlands (Dutch)
Norsk (Norwegian)
Polski (Polish)
Português (Portuguese - Portugal)
Português - Brasil (Portuguese - Brazil)
Română (Romanian)
Русский (Russian)
Suomi (Finnish)
Svenska (Swedish)
Türkçe (Turkish)
Tiếng Việt (Vietnamese)
Українська (Ukrainian)
Report a translation problem
There has to be someone out there who has made a worm, snake, or chain of some kind.
Anyone able to see where my problem lies?
at the head step event, is it intended for wbody.direction to equal the head direction not the last segment direction?
does the snake start with a head only, or a head and a single body segment ?
Good eye on the direction. I hadn't noticed that. It should be being set to the direction of the last segment. I'll give that a shot here then.
The snake starts with a head only. Everytime you collide with a food object it increases the heads "segments_to_add" variable by 1 which is then checked and handled in the step event of the head posted above.
edit:
obj_player_head: create
segment_list = ds_list_create();
ds_list_add(segment_list, id);
segments_to_add = 0;
angle_clamp = 180;
the rest is a copy paste of your code
I tried what you posted with changing the angle_clamp to 180 instead of 90 in the head create event.
That did allow the body segments to trail behind it in a way that seemed right.
However, there is still wonky stuff going on with it.
The reason they look like they are trailing properly with 180 is because aren't being locked to only varying by 90 degrees of the heads direction. They are still being placed at the front of the snake, but with 180 as the clamp they can drag behind in a more visually appealing way.
The image_angles of the body segments are also still wrong. Despite them being tied to the direction of the body itself, which should be facing within a clamped arc of the segment ahead of them, they are still facing backwards.
For reference, I based my code on this tutorial - Dividable Segmented Worm Tutorial
In that tutorial the worm is entirely computer controlled. It chooses a random destination, lunges towards it, and then chooses a new destination.
The worms sprites is a rectangle with a red line to show facing and they were made with the sprite facing upwards in the sprite editor so all the code relating to angles has to alter the direction by 90 since Gamemaker assumes sprites are facing right in the editor for angle 0.
The worm segments are added immediately upon creation of the worm. The head makes the first segment and they get set as parent and child. Each segment after that does the same where its creating segment is its parent and the segment it creates is its child.
I made the tutorial program and it works exactly as shown in the video. No problems at all.
I wanted to expand upon the idea and fix some things that bothered me so I uesd that tutorials code as a basis to make my own. The differences between mine and the one in the video is...
* In mine the players controls the head. I doubt this is related to my issues at all.
* The head creates all of the segments and segments are added over time. The segments aren't using the one before it as a parent directly but are stored on a ds_list where they pull the information from the segment before it. I did it this way so the head would know all the segments at that below to it to later allow me to expand by doing more interesting collisions and things. Its possible that this is related to my issues, but the segments seem to be getting created and associated with the segment ahead of them fairly well.
* I made my sprites facing to the right in the editor. I figure it would be more proper and would help with me not needing to account for altering the direction by 90 to compensate. I kind of suspect that this might be a large part of my issue. Even though I, to my knowledge at least, changed the code to compensate for the difference in facing.
spr_food - this sprites appearance and size is almost entirely irrelevant. It exists solely to collide with so anything you use for it should be fine. So long as its at least 16 x 16 in size it should work out just fine.
spr_player_head - 32 x 16 in size. It looks and was made exactly like in the video linked in previous posting aside from being made to face to the right in the sprite editor instead of being 16 x 32 in size and facing up like in the tutorial video.
spr_player_body - exact copy of spr_player_head aside from coloring. I have the head colored blue with a red facing line and the body colored white with a red facing line.
The origin point for the food is centered.
The origin point for the head is X = 0 Y = 8
The origin point for the body is X = 32 Y = 8
I've tried using using varying combinations of X = 0 or X = 32 for the head and body. It produces different looking results, but my overall problem is still the same where the body segments are added onto the front of the snake instead of the back.
obj_controller
Simply used to generate a piece of food and to allow me to exit the game with ESC key. The follow code is just copied out of the "show information" button on the object window.
obj_food
This object consists solely of a collision check with obj_player_head and sets it up to add another segment.
obj_player_head
This is the head of the snake / worm and is able to be driven around by the player. Going off the screen wraps to the opposite side. Collecting food makes you longer. The draw even just adds some text for debugging that shows the number of segments waiting to be added and the id of the last instance stored in the ds_list of body worm segments.
obj_player_body
Nothing fancy to say about this. It only has a create and step event. Create sets up some variables to a default setting just to ensure they exist before trying to use or change them. Step event is just supposed to make it follow the segment before it.
I've only got one room and its quite simple.
rm_player_snake - I don't see a way to copy / paste the room information, but its pretty basic.
850 x 480 sized room. 60 room speed. No background, just filled with the color white. The only two instances are 1 obj_controller and 1 obj_player_head. The controller comes first in instance order.
That is everything to create the project I'm trying to get working currently. It wouldn't take more than a couple minutes for anyone to copy and paste everything to re-create this and experiment with it themselves. Seeing the problem in action and being able to directly mess with it is probably the only real way to solve it I'd guess. Unless something is blatantly wrong with my math / angle formulas.
I wanted to rule out my ds_list stuff as being the issue so I just took the AI controlled head and added in movement controls to it and was able to duplicate the issue I'm seeing in my experimental player controlled project.
The culprit is how its handling direction and image_angle. I have no idea how to fix it but here is what I found.
In the obj_head create event it has this...
In the step event it has this controlling the heads movement...
Also in the step event is this code to control the direction and facing...
If I replace the movement code with my movement controls code and replace the line "dir = point_direction(target_x,target_y,x,y);" with "dir = direction" the end result is almost the same as what I'm seeing in my messed up player controlled worm.
The worm moves the same as in my project where the head pushes the first body segment and drags the rest instead of correctly dragging them all behind it. The difference is that the facing of them is correct this time.
From what I'm guessing at this point, the AI controlled worm that looks right is actually travelling in the opposite direction of what the direction variable holds. Which would make sense as to why my worms using the direction variable seem to be moving and facing the wrong way.
So I think I see whats going on at this point... but I'll be damned if I know what to do to make it work as desired.
EDIT - UPDATE - Tested my theory that the AI version is travelling in the opposite direction and it seems correct. By replacing the line "dir = point_direction(target_x,target_y,x,y);" with "dir = direction + 180" the worm "appears" correct.
It looks like its traveling in the right direction, but that is only because its traveling in reverse with the facing of the sprite set backwards.
Knowing whats going on is helpful, but I still don't know how to solve my issue. I'm not sure how to go about having the body segments be made to properly follow behind my head object.
nothing change drasticly, it just now your snake head is moving backward and the segment is following correctly.
The problem? Here's the thing:
Your snake is moving toward the direction it's pointing, and the body is moving away the direction it's pointing. The head is moving foward and the body is moving backward, and the segmented is programed to angled toward the direction 0.
i'll explain more of it with picture if you're blur with my half ass explanation.
Changing the speeds to travel backwards is a workaround to get the appropriate looking results.
Though that would cause issues when using the direction for things like firing off a projectile or whatever as I would always need to compensate by 180 to the direction since the worm is actually travelling backwards.
Any idea on how I'd do it so that the pieces are working as intended? So that the head would travel in the proper direction. The body segments would follow the segment ahead of them with their direction before being turned clamped within a 90 degree tolerance of the segment ahead of them.
from what i can see, if you wanna shoot a bullet you can have a bullet shooting variable for that, something like:
it's a workaround for your current problem for now.
Ideally, I'd prefer to avoid having my code necessitate the use of workarounds.
I appreciate the assistance on this. Its been troubling me for several days now. It doesn't seem like it should be that hard, but anything with math, such as angles and all that, doesn't process so well in my head so I end up with a lot of problems.
I've also never done anything like this before. The video I used as a tutorial wasn't really much of a tutorial. They didn't really explain anything about what their lines of code were doing or the reasons behind why they handled things the way they did.
After making the project they showed in the video and messing around with it a bunch I decided to adapt it to my own variant where the player would control the head, but it resulted in all this.
I did my best to pick apart all of their lines of code relating to direction and movement. I looked up everything in the manual to try to ensure I was getting how it was working properly. I've used point_direction and lengthdir myself in my own projects so I figured it wouldn't be that difficult to change over, but I was proven wrong.
I'm currently picking all those lines of code apart once again and trying to mess them to see if I can track down whats going now that understand that the AI version is travelling in the opposite direction all the time.
I set the origin for both the head and body sprites to be X = 0 and Y = 8.
Then I changed these lines of code in the bodies step event...
So basically just swapping the order of the x/y coordinates listed in the point_direction line and changed it from adding to subtracting lengthdir.
I'll need to go back over those lines math-wise to try to understand exactly what was going on there since I now know that those were responsible for my ass-backwards worm issue.
The only thing I'm noticing now that its traveling in the correct direction is that there is a bit of distance between the head and the first body segment. The body segments follow each other very closely as if connected, but the first body segment has a bit of a gap between it and the head when the worm is in motion.
Its really minor issue that I might be able to fix by altering the origin points a bit to give more overlap. Its not a critical problem by any means though I am curious why there is the gap when the movement code is the same for all body segments.
Thanks again for the assistance. When I code myself into a corner its always hard to dig my way back out without having someone bounce ideas off of and either point out a flaw in my code or at least make me look at things from a different perspective.
If you try that out and happen to see whats causing the head / body gap thats occuring I'd love to know.
Also the gap is caused by the code execution order, so by changing the Step Event to End Step will make sure the position change of the head object will execute first.
Also:
https://dl.dropboxusercontent.com/u/41066092/GIF.gif
For me if I did only one or the other the result would be that my body segments would rapidly alternate between sticking off the left or right side at a 90 degree angle to the head. They were swapping so quickly that they appeared to be in both places at once and transparent.
Since I noticed that either change on its own produced that same result, curiousity begged that I try changing them both which turned out to solve my direction issue.
I still need to go back and do "the maths" to see how exactly those two lines change direction of travel.
As for the gap... thanks a ton for that information. I've never messed with the Start / End Step Event types before. I knew that they would change the order that step events would be executed, but I've never found a use for them myself and everything I've ever read has never really pointed out a use for them.
I would have never figured out to use that to prevent the gap on my own. Execution order would not have crossed my mind. If anything I'd have likely assumed it was related to the speed or acceleration rates of the body segments and tried to mess with them to match speeds with the head. Likely this would have been a disaster.
I'll have to try changing the Step Event to an End Step when I get a chance.
In regards to your image, thats how my brain feels when I try to process math. It was at once horrifying and hypnotizing. I'd say it almost qualifies as art.
I'm under the assumption that is your worm with debugging text showing things like the x/y coordinates and I assume direction, image_angle, etc?
Thanks again for all the assistance by the way. You've been a great help.