Partage
  • Partager sur Facebook
  • Partager sur Twitter

transfert de vecteur (greenfoot)

17 janvier 2022 à 23:06:58

Bonjour, bonsoir,

Je suis en train de créer un jeu en java sur greenfoot et je rencontre un petit problème vers l'étape finale de mon projet. 

Je vous explique. J'ai une voiture (dirigée par un vecteur) et j'ai envie que quand cette voiture touche la balle, la voiture transfère son vecteur dans la balle pour qu'elle prenne la même direction que la voiture. Pour le moment, quand ma voiture touche la balle, elle avance qu'à droite, peu importe d'où on la touche

voici le code de la voiture:

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class car11 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class car11 extends SmoothMover
{
    /**
     * Act - do whatever the car11 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public car11()
    {
        addForce(new Vector(3, 0));
    }
    public void act()
    {
        move();
        checkKeys();

    }

    private void checkKeys() 
    {
        ignite(Greenfoot.isKeyDown("up"));
        
        
        if (Greenfoot.isKeyDown("left")) 
        {
            setRotation(getRotation() - 5);
        }
        if (Greenfoot.isKeyDown("right")) 
        {
            setRotation(getRotation() + 5);
        }
         

    }
        private void ignite(boolean boosterOn) 
    {
        if (boosterOn) 
        {
            addForce (new Vector(getRotation(), 0.06));
        }
        else
        {
            accelerate (0.9);  // acceleration with factor < 1 is actually slowing down.
        }
    }
}



voici le code de la balle:

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ball1 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ball1 extends SmoothMover
{

    
 
    /**
     * Act - do whatever the Ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Ball1()
    {
        addForce(new Vector(3, 0));
    }
    public void act() 
    {
        bounceOffEdge();
        move();
        movement();
        but();
    }
    public void movement()
    {
        if (getOneIntersectingObject(car11.class) != null) 
        {
            addForce (new Vector(getRotation(), 0.06));
        }
                else
        {
            accelerate (0.99);  // acceleration with factor < 1 is actually slowing down.
        }
    }
    private void bounceOffEdge()
    {
        if (getX() == 0 || getX() == getWorld().getWidth()-1) {
            setLocation((double)getX(), (double)getY());
            getMovement().revertHorizontal();
            accelerate(0.9);
        }
        else if (getY() == 0 || getY() == getWorld().getHeight()-1) {
            setLocation((double)getX(), (double)getY());
            getMovement().revertVertical();
            accelerate(0.9);
        }
    }
    public void but()
{
    if (getOneIntersectingObject(goal1.class) != null)
    {
        getWorld().removeObject(this);
    }
}
    }

et enfin mon smoothMover qui contient la fonction move:

import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A variation of an actor that maintains precise location (using doubles for the co-ordinates
 * instead of ints). It also maintains a current movement in form of a movement vector.
 * 
 * This is a variation of the SmoothMover class presented ealier in the book (version 2.0).
 * This version implements wrap-around movement: when the actor moves out of the world at one
 * side, it enters it again at the opposite edge.
 * 
 * @author Poul Henriksen
 * @author Michael Kolling
 * 
 * @version 2.1
 */
public abstract class SmoothMover extends Actor
{
    private Vector movement;
    private double exactX;
    private double exactY;
    
    public SmoothMover()
    {
        this(new Vector());
    }
    
    /**
     * Create new thing initialised with given speed.
     */
    public SmoothMover(Vector movement)
    {
        this.movement = movement;
    }
    
    /**
     * Move in the current movement direction. Wrap around to the opposite edge of the
     * screen if moving out of the world.
     */
    public void move() 
    {
        exactX = exactX + movement.getX();
        exactY = exactY + movement.getY();
        if(exactX >= getWorld().getWidth()) {
            exactX = 0;
        }
        if(exactX < 0) {
            exactX = getWorld().getWidth() - 1;
        }
        if(exactY >= getWorld().getHeight()) {
            exactY = 0;
        }
        if(exactY < 0) {
            exactY = getWorld().getHeight() - 1;
        }
        super.setLocation((int) exactX, (int) exactY);
 
    }
    
    /**
     * Set the location from exact coordinates.
     */
    public void setLocation(double x, double y) 
    {
        exactX = x;
        exactY = y;
        super.setLocation((int) x, (int) y);
    }
    
    /**
     * Set the location from int coordinates.
     */
    public void setLocation(int x, int y) 
    {
        exactX = x;
        exactY = y;
        super.setLocation(x, y);
    }

    /**
     * Return the exact x-coordinate (as a double).
     */
    public double getExactX() 
    {
        return exactX;
    }

    /**
     * Return the exact y-coordinate (as a double).
     */
    public double getExactY() 
    {
        return exactY;
    }

    /**
     * Increase the speed with the given vector.
     */
    public void addForce(Vector force) 
    {
        movement.add(force);
    }
    
    /**
     * Accelerate the speed of this mover by the given factor. (Factors < 1 will
     * decelerate.)
     */
    public void accelerate(double factor)
    {
        movement.scale(factor);
        if (movement.getLength() < 0.15) {
            movement.setNeutral();
        }
    }
    
    /**
     * Return the speed of this actor.
     */
    public double getSpeed()
    {
        return movement.getLength();
    }
    
    /**
     * Stop movement of this actor.
     */
    public void stop()
    {
        movement.setNeutral();
    }
    
    /**
     * Return the current speed.
     */
    public Vector getMovement() 
    {
        return movement;
    }
}

Merci d'avance de vos réponses.

Cordialement,
JAMP


-
Edité par justaminecraftplayer 17 janvier 2022 à 23:08:06

  • Partager sur Facebook
  • Partager sur Twitter
20 janvier 2022 à 7:38:14

A ton avis, dans ton new Vector(3 , 0) 3 et 0 ca représente quoi?
  • Partager sur Facebook
  • Partager sur Twitter