Snake clone - help!
by DiegoMedina · in Technical Issues · 01/12/2012 (4:38 pm) · 8 replies
Hello everyone,
I'm new to Torque 2D, and as my first project I'm trying to create a snake clone. Currently the problem I have is that as the head moves the movement of the body pieces is too elastic and rope like, unlike the movement from the original snake game. After I create the head I add body pieces by using the following function, which mounts a new body piece to the last one.
I'm new to Torque 2D, and as my first project I'm trying to create a snake clone. Currently the problem I have is that as the head moves the movement of the body pieces is too elastic and rope like, unlike the movement from the original snake game. After I create the head I add body pieces by using the following function, which mounts a new body piece to the last one.
/**
* Handles increasing the size of the serpent
*/
function playerSizeUp()
{
// Increase the size of the serpent
$tailSize += 1;
// Create body part
$body[$tailSize] = new T2dStaticSprite() {
scenegraph = scenewindow2d.getscenegraph();
UseMountForce = true;
MountForce = 0.0;
};
$body[$tailSize].setImageMap(snakeHeadImageMap);
$body[$tailSize].setSize("0.800 0.800");
$body[$tailSize].setPosition($body[$tailSize-1].getPosition());
//Mount body part to the tail
$body[$tailSize].mount($body[$tailSize-1], "0.0 0.0", 16, false, false, false, false);
}
#2
As far as I can tell (though judging by you comment regarding the mount position I'm probably making a mistake) I am mounting the new body piece at the tail end.
I was able to make a video of my snake moving around. As you can see the movement is very elastic, and just like you mentioned it grows only as it moves.
01/13/2012 (2:38 pm)
Hello and thanks for the help!As far as I can tell (though judging by you comment regarding the mount position I'm probably making a mistake) I am mounting the new body piece at the tail end.
//Mount body part to the tail $body[$tailSize].mount($body[$tailSize-1], "0.0 0.0", 16, false, false, false, false);
I was able to make a video of my snake moving around. As you can see the movement is very elastic, and just like you mentioned it grows only as it moves.
#3
$body[$tailSize].mount($body[$tailSize-1], "0.0 0.0", 16, false, false, false, false);
That 2nd false in the function probably needs to be set to true since you'll probably want the object to go directly to the mount point when it gets mounted. Force is set to 16 but I'd imagine you would want it at 0. You want your mount to remain in a specific place at all times. This is probably causing the elastic effect. When you set it to 0 your snake only shows one piece right? Well that's because you haven't adjusted your link points on your body pieces. You see, all of your body pieces are automatically mounting to the center of each other, so basically you have a bunch of body pieces on top of each other all moving in unison. you need to change the link point of the body pieces. The best way would be to do it in the body piece's datablock with the LinkPoints = "" variable...but if you wanted to do it another way, I guess you could use the addLinkPoint(%offsetX, %offsetY) function to each piece right before you mount it to your snake. Hope this works.
01/13/2012 (9:39 pm)
Couple of things here. $body[$tailSize].mount($body[$tailSize-1], "0.0 0.0", 16, false, false, false, false);
That 2nd false in the function probably needs to be set to true since you'll probably want the object to go directly to the mount point when it gets mounted. Force is set to 16 but I'd imagine you would want it at 0. You want your mount to remain in a specific place at all times. This is probably causing the elastic effect. When you set it to 0 your snake only shows one piece right? Well that's because you haven't adjusted your link points on your body pieces. You see, all of your body pieces are automatically mounting to the center of each other, so basically you have a bunch of body pieces on top of each other all moving in unison. you need to change the link point of the body pieces. The best way would be to do it in the body piece's datablock with the LinkPoints = "" variable...but if you wanted to do it another way, I guess you could use the addLinkPoint(%offsetX, %offsetY) function to each piece right before you mount it to your snake. Hope this works.
#4
Well after changing the mount force to 0 I see "two blocks": the head, and one piece for the body. I'm sure the other pieces of the body are mounted on top of each other like you suggested. I've been playing with the LinkPoints variable but I obviously don't know what I'm doing since all the blocks seem to be mounted on top of each other.
This is the code I have to create the head and mount the first body piece and increase the body size:
This is the code I have to increase the size of the body (I changed mountforce to 0 and added the linkPoints variable to the data block):
01/16/2012 (11:08 am)
Hi there!Well after changing the mount force to 0 I see "two blocks": the head, and one piece for the body. I'm sure the other pieces of the body are mounted on top of each other like you suggested. I've been playing with the LinkPoints variable but I obviously don't know what I'm doing since all the blocks seem to be mounted on top of each other.
This is the code I have to create the head and mount the first body piece and increase the body size:
function playerSnake::onLevelLoaded(%this, %scenegraph)
{
//set the player's name to the instance
$head = %this;
$head.MountForce = 0;
$body[$tailSize] = %this.getPosition(); //Initialize body
/**
* Create first body part and attach it to the head
*/
$body[$tailSize] = new T2dStaticSprite() {
scenegraph = scenewindow2d.getscenegraph();
LinkPoints = "1 1";
MountForce = 0.0;
};
$body[$tailSize].setImageMap(snakeHeadImageMap);
$body[$tailSize].setSize("0.800 0.800");
$body[$tailSize].setPosition($head.getPosition());
$body[$tailSize].mount($head, "0.0 0.0", 16, false, false, false, false);
/**
* Increase body size
*/
for(%count = 0; %count < 100; %count++)
{
playerSizeUp();
}
.
.
.This is the code I have to increase the size of the body (I changed mountforce to 0 and added the linkPoints variable to the data block):
/**
* Handles increasing the size of the serpent
*/
function playerSizeUp()
{
// Increase the size of the serpent
$tailSize += 1;
// Create body part
$body[$tailSize] = new T2dStaticSprite() {
scenegraph = scenewindow2d.getscenegraph();
UseMountForce = true;
LinkPoints = "-2 0";
MountForce = 0.0;
};
$body[$tailSize].setImageMap(snakeHeadImageMap);
$body[$tailSize].setLayer( 2 );
$body[$tailSize].setSize("0.800 0.800");
// Set up collision model for tail part
$body[$tailSize].setCollisionActive( false, true );
$body[$tailSize].setPosition($body[$tailSize-1].getPosition());
//Mount body part to the tail
$body[$tailSize].mount($body[$tailSize-1], "0.0 0.0", 0, false, false, false, false);
}
#5
Snake Example
01/16/2012 (1:32 pm)
Hey Diego. I'm thinking more and more that mounting is not the way to go on this one. Even if the link points were in the right place, when the snake changed direction, all of the body parts would change direction with the snake instantly which is not what you want. Particles is definitely the way to go and I actually did a little test to see if it works. Everything seems to go really well except there are a few gaps in the body pieces every now and then. This may be because I'm using the mouse but adjusting the speed of the snake may also help to keep that from happening. Here's a video of what I did that should get you going in the right direction.Snake Example
#6
It looks like you made the video private can you change it and maybe post a code snippet of how you achieved the snake using particles?
01/16/2012 (4:32 pm)
Hey Andrew,It looks like you made the video private can you change it and maybe post a code snippet of how you achieved the snake using particles?
#8
01/18/2012 (11:04 am)
Thank you Andrew. Let me play with it. I'll post a video later.
Torque Owner Andrew Son
Honestly, I'm not sure if mounting is the way to go for this. You may want to check out trying to use particles believe it or not. You could simply mount a particle effect to your snake and have the snake object emit a "body" that will erase on it's own over a period of time. To simulate growth, just adjust/increase the time before the particle can disappear, and just make sure the particle effect has collision active. I don't know if this would work, it's just an idea. Let me know if you try it if it works or not. I'm kind of curious.