想做一个小游戏就是中间有一个小人,四角会朝他发射子弹。但不知道子弹怎么能朝小人方向?public void add(int count,float x, float y) //x y为小人坐标
{
add(count, Left_Top,x,y);
add(count, Left_Bottom,x,y);
add(count, Right_Top, x,y);
add(count, Right_Bottom, x,y);
}
public void add(int count,int dir,float sx, float sy)
{
//Random random = new Random();
for (int i = 0; i < count; i++)
{
int x = 0,y = 0; //子弹坐标
double v_x = 0; //子弹速度
double v_y = 0;
double t;
switch (dir) //判断从哪个角发出
{
case Left_Top:
x = 0; y = 0;
t = sx / Math.sqrt(sx * sx + sy * sy);
v_x = 3 * t;
v_y = -3 * Math.sqrt(1 * t * t);
break;
case Left_Bottom:
x = 0; y = 480;
sy = 480 - sy;
t = sx / Math.sqrt(sx * sx + sy * sy);
v_x = 3 * t;
v_y = 3 * Math.sqrt(1 * t * t);
break;
case Right_Top:
x = 320; y = 0;
sx = 320 - sx;
t = sx / Math.sqrt(sx * sx + sy * sy);
v_x = -3 * t;
v_y = -3 * Math.sqrt(1 * t * t);
break;
case Right_Bottom:
x = 320; y = 480;
sy = 480 - sy;
sx = 320 - sx;
t = sx / Math.sqrt(sx * sx + sy * sy);
v_x = -3 * t;
v_y = 3 * Math.sqrt(1 * t * t);
break;
default:
break;
}
Particle particle = new Particle(v_y, v_x, x, y);
particleSet.add(particle);
}
}
这样写貌似不对跪求正解,谢谢
{
add(count, Left_Top,x,y);
add(count, Left_Bottom,x,y);
add(count, Right_Top, x,y);
add(count, Right_Bottom, x,y);
}
public void add(int count,int dir,float sx, float sy)
{
//Random random = new Random();
for (int i = 0; i < count; i++)
{
int x = 0,y = 0; //子弹坐标
double v_x = 0; //子弹速度
double v_y = 0;
double t;
switch (dir) //判断从哪个角发出
{
case Left_Top:
x = 0; y = 0;
t = sx / Math.sqrt(sx * sx + sy * sy);
v_x = 3 * t;
v_y = -3 * Math.sqrt(1 * t * t);
break;
case Left_Bottom:
x = 0; y = 480;
sy = 480 - sy;
t = sx / Math.sqrt(sx * sx + sy * sy);
v_x = 3 * t;
v_y = 3 * Math.sqrt(1 * t * t);
break;
case Right_Top:
x = 320; y = 0;
sx = 320 - sx;
t = sx / Math.sqrt(sx * sx + sy * sy);
v_x = -3 * t;
v_y = -3 * Math.sqrt(1 * t * t);
break;
case Right_Bottom:
x = 320; y = 480;
sy = 480 - sy;
sx = 320 - sx;
t = sx / Math.sqrt(sx * sx + sy * sy);
v_x = -3 * t;
v_y = 3 * Math.sqrt(1 * t * t);
break;
default:
break;
}
Particle particle = new Particle(v_y, v_x, x, y);
particleSet.add(particle);
}
}
这样写貌似不对跪求正解,谢谢
解决方案 »
- Java 中怎么实现一次编译,到处运行?
- 一个java编程
- 奇怪的问题,谁能解释一下。。。。
- 一个神奇的现象,我左边JTree,右边JTable,都加了JScrollPane,用JSplitPane分左右,JTable读数据库,但是必须先移动一下JTree和JTable分解的那条东西
- 请问,eclipse启动时显示的那个有其logo的面板是如何实现的阿?谢谢
- I/O这里学得吐血,有什么好的学习方法啊
- 请大家介绍一下ArrayList的用法。
- 跪求 Java Karnagh map (K-map) 源代码
- 100分周末大放送——Java面试一般会出什么样的题目,请高人指点!
- APPLET画图问题(有送分之嫌)
- 求教一个类的设计问题
- 工作一年多的困惑,请大家解答
|
|y
V以上为坐标系定义.
长方形坐标 "左上角(x0,y0)","右上角(x1,y1)","左下角(x2,y2)","右下角(x3,y3)" 宽w = x1-x0,高h=y2-y0;
人坐标x,y.
子弹速度v,横向分解速度vx,纵向分解速度vy.
子弹需要t时间可以击中人.则vt * vt = w*w + h*h. 求出t.从左上角一发字段直线飞行人,求任意时刻tx子弹位置cx,xy.
则cx = x0 + t*vx;
cy = y0+ t*vy.
把人所在点投影到y轴上得到点p,坐上角p0,人p和p组成三角形长宽比和速度三角是相似的.就可以求出得到vx和vy了.