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

2019/6/21

Shots should bounce of the wall correctly but idk how...

StalinRule34 StalinRule34

2019/6/21

#
public class Shot extends Actor
{
    private int health = 100;  //private int variable für den Lebenswert (100)
    private Car myCar;  // private Variable für den code unter diesem welche die

    public Shot (Car myCar)
    {
        this.myCar = myCar;  
        setRotation(myCar.getRotation());  //rotation des schusses mit der richtung des Autos verbindet 
    }

    public void act()
    {
        double entfernung = Math.sqrt(Math.pow(getX()-640, 2)+Math.pow(getY()- 402.5 ,2));
        if(entfernung >= 364 || isTouching(block.class)){   // Wenn man vom Mittelpunkt aus 364 Pixel weit entfernt ist oder größer wird ,dann
            turn(180);  //drehe dich um 180 grad
            loseHealth(50);  //verliere 50 leben   || verliert leben und verschwindet durch untere Codes aus der Welt ,weil er nur 1 Mal Apprallen soll.
        }
        move(9);  //bewege dich mit 9 als Geschwindigkeitswert voraus
    }

    public void loseHealth(int amount)  // Nutzt int variable im folgenden coding...
    {
        health -= amount;
        if (health < 1)      //wenn health kleiner als 1 ist ( also 0 ) ,dann...
        {
            getWorld().removeObject (this);   // entfernt die Welt dieses Auto
        }
    } 

} 

The parts after // are for my group members and i need help at coding my shot.class correctly: Right now it can bounce off once off my Wall or a block and if it hits the enemy it lets him lose Health and dissapears but my "Wall" is a circle ( its round ) so I dont know how to make it bounce off at a oppisite angle (idk if its named like that english is my 3d language sry btw)
danpost danpost

2019/6/21

#
Turn around, then take the difference between the angles of the new direction of travel and the radial line through your location (line to the center of the circle), double it and add it to your rotation. That should set you properly on your new course.
StalinRule34 StalinRule34

2019/6/23

#
Thanks dude you´re insane
danpost danpost

2019/6/23

#
StalinRule34 wrote...
Thanks dude you´re insane
Did you get it working?
You need to login to post a reply.