Jump to content


Photo

Character Teleports When Colliding With a Wall


  • Please log in to reply
7 replies to this topic

#1 Cloud Architect

Cloud Architect

    GMC Member

  • New Member
  • 462 posts

Posted 11 June 2011 - 08:23 PM

Sup guys :)

I'm working on a game at the moment, but I've come across something that's really stumped me and doesn't seem to be anywhere on here. :/

It's a platform game.

My problem is that I created a collision script which detects the side of a block you hit and changes hspeed and vspeed properly depending on where you hit it, but a really strange error occurs when you fall from the top inbetween two blocks, the player literally teleports about 200 pixels away from where he was to the right or left, I don't even use the X or Y variable to move the player around so seriously whaaattttt :(!

I've provided a link for download so you can get in there and see what's wrong with it, because I'm lost, even in debug mode looking at the variables for the player hasn't shown anything significant.

It requires that you have GM8, I use pro so I advise you have it too. If you don't have GM8 then I've put the scripts in the zip file with it :), there are 4 of them. I've done this because I can't pinpoint the problem anywhere, otherwise I would have posted the specific part of the code :/

Thanks in advance for your help!

Download Here

EDIT: To recreate the error jump onto one of the single blocks and move to the left and just allow yourself to fall until you land. You'll land between two blocks, not quite on one or the other. The player will then randomly spawn somewhere else.

Posted Image

Edited by Cloud Architect, 11 June 2011 - 09:29 PM.

  • 0

#2 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 17013 posts
  • Version:GM:Studio

Posted 11 June 2011 - 08:54 PM

Okay this error is cause by two things... the gravity, and the way you handle the initial collision. As you hit down sometimes gravity pulls you just slightly into the block, then the collision detection detects that you have a hit on the left which then kicks the move_contact_solid() code into operation. So to avoid this change your script to this...
var dd;
//Get the center point for you and the colliding object
//and then the direction to move out of collision...
dd=point_direction(other.x+16, other.y+16,x+16,y+16);
while(place_meeting(x,y,other))
{
x+=lengthdir_x(1,dd);
y+=lengthdir_y(1,dd);
}

//Check for above and below collisions first!

//Collision from below
if bbox_top > other.bbox_bottom //if hitting block from below
{
	room_caption = "below";
	other.solid = true; //set block to solid <<<<<<<<<<< Why? They are already solid!
	move_contact_solid(up, jump_speed);
	vspeed = 0;
	exit; //Important! This prevents further collision codes from running...
} 
else
//Collision from above
if bbox_bottom < other.bbox_top //if hitting block from below
{
	room_caption = "above";
	other.solid = true; //set block to solid
	move_contact_solid(down, jump_speed);
	vspeed = 0;
	gravity=0;
	can_jump = true; //reset can_jump if landing from above.
	exit;
}

//Left collision
if bbox_left > other.bbox_right //if hitting from the left side of the player
{
	room_caption = "left";
	other.solid = true; //set block to solid
	move_contact_solid(left, max_speed_left);
	hspeed = 0;
	exit;
} 
else
//Right Collision
if bbox_right < other.bbox_left //if hitting from the right side of the player
{
	room_caption = "right";
	other.solid = true; //set block to solid
	move_contact_solid(right, max_speed_right);
	hspeed = 0;
	exit;
}

I haven't tested it more than a few seconds, but it seems to be a good solution!
  • 0

#3 Cloud Architect

Cloud Architect

    GMC Member

  • New Member
  • 462 posts

Posted 11 June 2011 - 09:16 PM

Thanks for the speedy reply man!

Well that seems to have solved one glitch, but not the glitch I meant. I'll post an image of how to recreate it :)

EDIT: Moved image to OP.

EDIT EDIT: OH WAIT. Just saw another bit of code. Put it in and now it works. Ah cheers man, gonna mess with it for a sec. Just trying to understand how this works :)

Edited by Cloud Architect, 11 June 2011 - 09:26 PM.

  • 0

#4 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 17013 posts
  • Version:GM:Studio

Posted 11 June 2011 - 09:23 PM

I´ve been playing around with the code and finally found something that works for me 100%

Spoiler


I have changed no other code in the game, so if it dosn't work I have no idea why not!
  • 0

#5 Cloud Architect

Cloud Architect

    GMC Member

  • New Member
  • 462 posts

Posted 11 June 2011 - 09:29 PM

I´ve been playing around with the code and finally found something that works for me 100%

Spoiler


I have changed no other code in the game, so if it dosn't work I have no idea why not!


Nah man it works! Thanks a lot! Sorry about that I should have copied and pasted it and read it first. Cheers for your help man, if you could do me one more favour can you exaplain how the code you added to the end of this works? It's definitely a little bit above my current ability x
  • 0

#6 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 17013 posts
  • Version:GM:Studio

Posted 11 June 2011 - 09:32 PM

It takes the direction between the center of the colliding object and the centr of the player, and then moves the player along that direction until it is no-longer colliding with the object. It's a really handy little code and I use it all the time! Anyway, I'm glad I could help you!
  • 0

#7 Cloud Architect

Cloud Architect

    GMC Member

  • New Member
  • 462 posts

Posted 11 June 2011 - 09:34 PM

It takes the direction between the center of the colliding object and the centr of the player, and then moves the player along that direction until it is no-longer colliding with the object. It's a really handy little code and I use it all the time! Anyway, I'm glad I could help you!


Ah nice man, cheers dude, I was expecting to be waiting a lot longer haha x
  • 0

#8 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 17013 posts
  • Version:GM:Studio

Posted 11 June 2011 - 09:35 PM

No problems mate... Happy to have helped!
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users