Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411599 Posts in 69387 Topics- by 58445 Members - Latest Member: gravitygat

May 08, 2024, 10:18:01 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Problem with collision box when using image_xscale = -1
Pages: [1]
Print
Author Topic: Problem with collision box when using image_xscale = -1  (Read 3593 times)
Jaun
Level 0
*



View Profile
« on: October 07, 2015, 12:13:29 PM »

I create a simple platformer project following a tutorial on Youtube. Everything is working fine, except for one little problem that is driving me nuts. When the sprite of my character collide with a oWall object (the one I use as ground and walls), the right one is fine, but the left one is off by one pixel. A gif may exemplify what I'm trying to say better:



Here is all the code of the game (<100 lines of code):

Create Event of oHero:
Code:
myGravity= 1;
mySpeed= 4;
speedJump= 12;
speedH = 0;
speedV= 0;

Step Event of oHero (PHYSICS):
Code:
KEY_LEFT = keyboard_check(vk_left) or keyboard_check(ord('A'));
KEY_RIGHT = keyboard_check(vk_right) or keyboard_check(ord('D'));
KEY_DOWN = keyboard_check(vk_down) or keyboard_check(ord('S'));
KEY_JUMP = keyboard_check(vk_up) or keyboard_check(ord('W'));

if (place_meeting(x, y + 1, oWall))
{
    speedV = 0;
    
    if (KEY_JUMP)   speedV = -speedJump;
}

else
{
    if (speedV < 10)   speedV += myGravity;
}

if (KEY_RIGHT)  speedH = mySpeed;
if (KEY_LEFT)   speedH = -mySpeed;

if ((!KEY_LEFT and !KEY_RIGHT) or (KEY_LEFT and KEY_RIGHT))     speedH = 0;

// HORIZONTAL
if (place_meeting(x + speedH , y, oWall))
{
    while(!place_meeting(x + sign(speedH), y, oWall))
    {
        x += sign(speedH);
    }
    speedH = 0;
}
x += speedH;

// VERTICAL
if (place_meeting(x, y + speedV, oWall))
{
    while(!place_meeting(x, y + sign(speedV), oWall))
    {
        y += sign(speedV);
    }
    speedV = 0;
}
y += speedV;

Step Event of oHero (SPRITES):
Code:
if (yprevious != y)
{
    sprite_index = sHeroJumping;
    mask_index = sHeroJumpingMask;
    
    image_speed = 0;
    image_index = (y > yprevious);
}

else
{
    if (xprevious != x)
    {
        sprite_index = sHeroWalking;
        mask_index = sHeroWalkingMask;
        
        image_speed = 0.3;
    }
    
    else
    {
        if (KEY_DOWN)
        {
            sprite_index = sHeroDown;
            mask_index = sHeroDownMask;
        }
        
        else
        {
            sprite_index = sHeroIdle;
            mask_index = sHeroIdleMask;
        }
    }
}

if (speedH != 0)   image_xscale = sign(speedH);

The oHero sprite mask and all the animations mask are centered and have the same width. I'm not using anything solid neither the build-in gravity stuff of Game Maker. Every sprite have Precise Collision Checking on. I know it have something to do with image_xscale because everything works fine if I don't flip the sprite. I'm using Game Maker Studio (v1.4).

So... is image_xscale messing with the collision box? anything wrong in the code? any workaround for this problem?
« Last Edit: October 07, 2015, 12:19:04 PM by Jaun » Logged

English is not my first language, so let me know if I made any grammatical crime.
zleub
Level 0
**


View Profile WWW
« Reply #1 on: October 07, 2015, 12:50:58 PM »

if place_metting() is your collision function, i think in your call to (place_meeting(x, y + 1, oWall)), the sign of the + 1 should depend on your scale ?
Logged
Jaun
Level 0
*



View Profile
« Reply #2 on: October 07, 2015, 01:37:01 PM »

if place_metting() is your collision function, i think in your call to (place_meeting(x, y + 1, oWall)), the sign of the + 1 should depend on your scale ?

Thanks for your answer! That bit of code is for checking if there is ground just under the character, so I don't really think it has something to do with the problem.

The actual collision code is below, under the "HORIZONTAL" (and "VERTICAL") comments.
Logged

English is not my first language, so let me know if I made any grammatical crime.
oahda
Level 10
*****



View Profile
« Reply #3 on: October 08, 2015, 12:27:50 AM »

I don't really know GM any more, but could it be that the mask isn't perfecrly centered and then it doesn't flip when the image does?
Logged

Juskelis
Level 1
*



View Profile
« Reply #4 on: October 08, 2015, 10:52:57 AM »

Yeah, I think it has something to do with the collision mask not working correctly. If I remember correctly, you can draw a rect over the mask by using the "bbox_left, bbox_right, etc" variables for debugging it.
Logged

oahda
Level 10
*****



View Profile
« Reply #5 on: October 08, 2015, 11:22:37 AM »

Isn't there a checkbox for making the mask visible? That's what I can remember, at least, but I'm not sure.
Logged

Juskelis
Level 1
*



View Profile
« Reply #6 on: October 08, 2015, 11:54:55 AM »

There's a way to see the mask in the image editor, but there's no prebuilt option for seeing it in game
Logged

Conker
Guest
« Reply #7 on: October 08, 2015, 01:27:34 PM »

Its your mask, make sure its centered correctly
Logged
ProgramGamer
Administrator
Level 10
******


aka Mireille


View Profile
« Reply #8 on: October 08, 2015, 01:50:35 PM »

Also as a last resort, try to flip your sprite independently from your mask by drawing manually. It worked for me once.
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic