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

2017/10/6

help wall collision

theCode theCode

2017/10/6

#
i have a wall with a code that just declare if actor is to your right move him rightand so on for the 4 directions but if you keep turnin the actor it kind f glitches through and sorry for the english don't kill me grammar police
theCode theCode

2017/10/6

#
this is the wall's code
public class Wall extends Actor
{
    Actor enemy1;
    Actor soldierleft;
    Actor soldierright;
    Actor soldierup;
    Actor soldierdown;
    public void act() 
    {
     pushsoldier();
    }
    public void pushsoldier()
    {
     soldierright = getOneObjectAtOffset(+2,0,Soldier.class);
     if(soldierright != null)soldierright.setLocation(soldierright.getX()+5,soldierright.getY());
     soldierleft = getOneObjectAtOffset(-2,0,Soldier.class);
     if(soldierleft != null)soldierleft.setLocation(soldierleft.getX()-5,soldierleft.getY());
     soldierup = getOneObjectAtOffset(0,-2,Soldier.class);
     if(soldierup != null)soldierup.setLocation(soldierup.getX(),soldierup.getY()-5);
     soldierdown = getOneObjectAtOffset(0,+2,Soldier.class);
     if(soldierdown != null)soldierdown.setLocation(soldierdown.getX(),soldierdown.getY()+5);
     soldierright = getOneObjectAtOffset(+5,0,Soldier.class);
     if(soldierright != null)soldierright.setLocation(soldierright.getX()+5,soldierright.getY());
     soldierleft = getOneObjectAtOffset(-5,0,Soldier.class);
     if(soldierleft != null)soldierleft.setLocation(soldierleft.getX()-5,soldierleft.getY());
     soldierup = getOneObjectAtOffset(0,-5,Soldier.class);
     if(soldierup != null)soldierup.setLocation(soldierup.getX(),soldierup.getY()-5);
     soldierdown = getOneObjectAtOffset(0,+5,Soldier.class);
     if(soldierdown != null)soldierdown.setLocation(soldierdown.getX(),soldierdown.getY()+5);
    }
}
EduandErnst EduandErnst

2017/10/7

#
From my understanding you're trying to restrict the movement of your Actor by a wall/obstacle. It would be helpful if you could clarify your direction issue... but for now I'll provide you an example code fragment that would prevent your actor from walking through "Obstacle.class"
Actor obstacle = getOneIntersectingObject(Obstacle.class);
if (obstacle != null){
      setLocation(getX() -(dx*xSpeed), getY());
}
danpost danpost

2017/10/8

#
By using the wall to "push" the soldier off, you are doing many more checks than required for collisions -- that is, you need to determine where the soldier is with respect to the wall. If collision checking was done in the soldier class at the time a collision occurs, then the locational relationship between the actors would already be known by the directional vector of the moving actor. I would not have the wall check anything (no act method there) and after any moving or turning of the mobile actor, check for collisions immediately and back off at that time.
theCode theCode

2017/10/12

#
EduandErnst wrote...
From my understanding you're trying to restrict the movement of your Actor by a wall/obstacle. It would be helpful if you could clarify your direction issue... but for now I'll provide you an example code fragment that would prevent your actor from walking through "Obstacle.class"
Actor obstacle = getOneIntersectingObject(Obstacle.class);
if (obstacle != null){
      setLocation(getX() -(dx*xSpeed), getY());
}
how would i manage all the other directions? it only works for one do i make it like i did it in the wall but in my actor code?
Super_Hippo Super_Hippo

2017/10/12

#
This code is only working if dx represents the movement in x-direction and the actor just moved dx*xSpeed (so -dx*xSpeed will place the actor where it was). Then you can do the same with the y-direction.
You need to login to post a reply.