如何使精灵旋转到中心(在Java中,但如果你知道一个公式,它也会有帮助)
我正在爪哇做一个游戏,敌人的精灵盘旋到中心,破坏主塔。我唯一有问题的是使雪碧螺旋的公式。我在互联网上找到的所有信息如下: 这就是我想做的,但我想让它们从JFrame外部螺旋到一个点,而不是从JFrame内部的点如何使精灵旋转到中心(在Java中,但如果你知道一个公式,它也会有帮助),java,jframe,sprite,spiral,Java,Jframe,Sprite,Spiral,我正在爪哇做一个游戏,敌人的精灵盘旋到中心,破坏主塔。我唯一有问题的是使雪碧螺旋的公式。我在互联网上找到的所有信息如下: 这就是我想做的,但我想让它们从JFrame外部螺旋到一个点,而不是从JFrame内部的点 我在中四,我还没有太多关于这些公式的知识,如果我理解这些公式有问题,我很抱歉。提前谢谢 让精灵看起来像螺旋的一种简单方法是,假装它附在一个手臂上,就像时钟的指针一样,绕着螺旋的中心旋转。当手臂旋转时,慢慢地将精灵沿着手臂向中心移动。你最终得到的是一部经典之作 我可以为您模拟一些代码,但这
我在中四,我还没有太多关于这些公式的知识,如果我理解这些公式有问题,我很抱歉。提前谢谢 让精灵看起来像螺旋的一种简单方法是,假装它附在一个手臂上,就像时钟的指针一样,绕着螺旋的中心旋转。当手臂旋转时,慢慢地将精灵沿着手臂向中心移动。你最终得到的是一部经典之作 我可以为您模拟一些代码,但这需要几分钟的时间
好的,这是密码
public static double getArmX(double length, double angle) {
return Math.cos(angle) * length;
}
public static double getArmY(double length, double angle) {
return Math.sin(angle) * length;
}
这些是数学的核心。它们返回距中心指定距离(长度)和距中心角度(角度)的图元的x和y值
现在,我不知道您的代码是如何设置的,但是让我们假设我们有一个名为spiralProgress
的double
,它表示您的实体的螺旋有多远。在spiralProgress==0
处,实体刚刚开始,在spiralProgress==1
处,实体处于中心
下面是获取实体的x和y的代码:
double startingRadius = 64;
double rotations = 10;
double x = getArmX(startingRadius * (1-t), t * rotations * Math.PI * 2);
double y = getArmY(startingRadius * (1-t), t * rotations * Math.PI * 2);
在该代码段中,startingRadius
是指实体从中心开始的单位数(像素,如果程序中x和y的意思是这样的话),而rotations
是指实体在到达中心之前围绕中心循环的次数
它返回的坐标是围绕{0,0}的螺旋线,因此如果你想围绕其他点旋转,比如说{screenWidth/2,screenHeight/2}
,你需要将screenWidth/2
添加到x
中,将screenHeight/2
添加到y
这里有一个完整的Java程序来演示这个数学。在窗口中的任意位置单击鼠标以重置缓和曲线
package net.eonz.stackoverflow.spiral;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable, MouseListener {
private static final long serialVersionUID = 1L;
public static final String NAME = "untitled";
public static final int HEIGHT = 600;
public static final int WIDTH = 600;
public static final int SCALE = 1;
private boolean running = false;
public void start() {
running = true;
new Thread(this).start();
this.addMouseListener(this);
}
public void stop() {
running = false;
}
public void run() {
long last = System.currentTimeMillis();
while (running) {
long now = System.currentTimeMillis();
double dt = (now - last) / 1000.0;
last = now;
update(dt);
render();
}
}
double t = 0;
public void update(double dt) {
t += dt / 16;
if (t > 1)
t = 1;
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, this.getWidth(), this.getHeight());
/* SPIRAL MATH IS HERE */
double startingRadius = this.getHeight() * 0.40;
double rotations = 10;
double x = getArmX(startingRadius * (1 - t), t * rotations * Math.PI
* 2);
double y = getArmY(startingRadius * (1 - t), t * rotations * Math.PI
* 2);
g.setColor(Color.black);
g.fillRect((int) (x - 8) + this.getWidth() / 2,
(int) (y - 8) + this.getHeight() / 2, 16, 16);
/* END SPIRAL MATH */
g.dispose();
bs.show();
}
public static double getArmX(double length, double angle) {
return Math.cos(angle) * length;
}
public static double getArmY(double length, double angle) {
return Math.sin(angle) * length;
}
@Override
public void mouseClicked(MouseEvent e) {
this.t = 0;
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
public static void main(String[] args) {
Game game = new Game();
game.setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
game.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
JFrame frame = new JFrame(Game.NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(game, BorderLayout.CENTER);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
game.start();
}
}
我也是java编程新手,作为我课程的一部分,我最近不得不编写一个螺旋 这是我课程中的解决方案文件。 这将绘制一个简单的螺旋 我希望这对你有帮助。它还可以帮助您理解正在发生的事情 享受
谢谢这将帮助我开始,我会试着自己解决,但如果我放弃,我知道向谁寻求帮助。再次感谢@user2736516我不是说我不打算写代码,因为这会花费太长时间,我是说我会写代码并更新帖子。
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class DrawSpiral2 extends JPanel
{
// draws a square shape that continually spirals outward
public void paintComponent( Graphics g )
{
super.paintComponent( g );
g.setColor( Color.GREEN );
// draw a green spiral
int x = getWidth() / 2;
// x coordinate of upperleft corner
int y = getHeight() / 2;
// y coordinate of upperleft corner
int radiusStep = 20;
// distance the radius changes
int diameter = 0; // diameter of the arc
int arc = 180; // amount and direction of arc to sweep
// draws individual lines in to form a spiral
for ( int i = 0; i < 20; i++ )
{
if ( i % 2 == 1 ) // move the x position every other repetition
x -= 2 * radiusStep;
y -= radiusStep; // move the y position
diameter += 2 * radiusStep; // increase the diameter
g.drawArc( x, y, diameter, diameter, 0, arc );
// draw the arc
arc = -arc; // reverse the direction of the arc
} // end for
} // end method paintComponent
} // end class DrawSpiral2
public class DrawSpiralTest2
{
public static void main( String args[] )
{
DrawSpiral2 panel = new DrawSpiral2();
JFrame application = new JFrame();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
application.add( panel );
application.setSize( 300, 300 );
application.setVisible( true );
} // end main
} // end class DrawSpiralTest2