This site requires JavaScript, please enable it in your browser!
Greenfoot back
darkmist255
darkmist255 wrote ...

2011/12/7

A simple "wall"

1
2
darkmist255 darkmist255

2011/12/7

#
Now, I know that there are some really good 2d rigid body physics scenarios out there, but that's not what I'm trying to achieve really (though I do hope that I will eventually be able to understand them :D). I just am trying to make it so that my actor "Character" cannot pass through the actor "PlainRock". I have already got the collision detection working with getOneIntersectingObject, but am wondering how I can prevent the character from moving through. I am going to try coding something along the lines of "if the player's right is to the right of the rock's left, cancel the player's move by moving the player left." But that seems like it might not behave smoothly, or will just be very tedious to code, is there a more efficient way to do this? If not, do you think this will work no matter where the rock is?
Builderboy2005 Builderboy2005

2011/12/8

#
My favorite way of doing collision physics is this: First move the character in the X direction and check for collisions, if there are collisions, move the player backwards until there are no more collisions. Second, so the same with the Y axis, move the character in the Y direction and check for collisions, if there are any, move the player backwards until there are no more collisions. This ensures your player will never end up inside of other objects, and the separation of Axes makes for smooth movement when you are standing on platforms and the like.
darkmist255 darkmist255

2011/12/8

#
so, should I use a for statement for this (since I've never used a for statement, might be interesting) or should I use a loop, or just a regular method?
Builderboy2005 Builderboy2005

2011/12/8

#
This would probably need several loops in order to accomplish, and probably not a For() loop. For loops are in general only used if you want to loop for a specific and exact number of times, and in our case, it is possible there is no looping at all. If the player doesn't collide with an object, we will not need to move him backwards and no looping would be needed.
darkmist255 darkmist255

2011/12/8

#
So how do you think this would work:
        while((character.getX() + 25) >= (getX() - 25) && (character.getX() + 25) <= getX())
        {
            character.setLocation((getX() - 1), getY());
        }
then just repeat that for right, up, down? ps: the sprite is 50x50, thus (getX() + 25) is the right edge.
darkmist255 darkmist255

2011/12/8

#
Also, since the speed will always be stats.moveSpeed, I could instead make it character.setLocation((getX() - stats.moveSpeed), getY()); and at that point I might not even need a loop.
Builderboy2005 Builderboy2005

2011/12/8

#
I would do something more like:
While (getOneIntersectingActor(blah.class)!=null){
    setLocation(getX()-1,getY());
}
That way it is independent of what type of objects or images you are using.
darkmist255 darkmist255

2011/12/8

#
My only concern with that is that it is always moving to the left. What if the actor intersects from the right? Or the top/bottom?
Builderboy2005 Builderboy2005

2011/12/8

#
Right, that loop is only executed in the case that the actor is moving to the right. A modified loop might look something like this:
While (getOneIntersectingActor(blah.class)!=null){  
    if(velocityX > 0){
        setLocation(getX()-1,getY());  
    }else{
        setLocation(getX()+1,getY());
    }
} 
And then include an identical loop for the Y direction
darkmist255 darkmist255

2011/12/9

#
I will try my hand at it :D!
darkmist255 darkmist255

2011/12/9

#
Do you think I should include this code in the character class or in the rock class, or is there really no difference?
Builderboy2005 Builderboy2005

2011/12/9

#
You include it in any object that moves. If you don't include it in a moving object, that object will no have any collision detection.
darkmist255 darkmist255

2011/12/9

#
If I make a method in the class groundMover (which the player and all moving objects are in) called checkCollisions(), I can use getX() and getY() within that method and it will get the X or Y of the actor calling it, correct?
Builderboy2005 Builderboy2005

2011/12/9

#
If the Actors like the player and such extend the class GroundMover, the method CheckCollisions() can use the methods getX() and getY() just like normal. Remember, the classes that extend GroundMover inherit the methods GroundMover contains, so it's almost exactly the same as simply having checkCollisions() in your player class :) In short; Yes! :D
darkmist255 darkmist255

2011/12/9

#
Yeah, extend was the word I was looking for, thanks :D! I'm gonna code that now!
There are more replies on the next page.
1
2