查看: 2579|回复: 12

[已经过期] 一个悲伤的问题QWQ,关于地图上抛物线箭支的一系列问题

[复制链接]
梦石
0
星屑
1821
在线时间
2528 小时
注册时间
2010-10-12
回帖
1336

开拓者

发表于 2015-2-10 11:16:50 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
  1. #==============================================================================
  2. # ■ EFS_Arrow
  3. #------------------------------------------------------------------------------
  4. #  定义弓箭失的类
  5. #==============================================================================
  6. =begin
  7.   *   8   *
  8.    *     *
  9.     *   *
  10. 4   *#*    6
  11.     *   *
  12.    *     *
  13.   *   2   *
  14. =end
  15. class EFS_Arrow < Sprite
  16.   attr_accessor:hit
  17.   #--------------------------------------------------------------------------
  18.   # ● 初始化数据
  19.   #--------------------------------------------------------------------------
  20.   def initialize(viewport)
  21.     super(viewport)
  22.     @hit = false
  23.     @direction = 2
  24.     @wait_count = 0
  25.     @angle = 0
  26.     @velocity = 15
  27.     @hight = 50
  28.     @flat_flying = false
  29.   end
  30.   #--------------------------------------------------------------------------
  31.   # ● 发射箭失
  32.   #--------------------------------------------------------------------------
  33.   def display(event,target_event,name,d_name)
  34.     #赋值
  35.     @final_x = target_event.x*32- 34
  36.     @final_y = target_event.y*32- 68
  37.     @event=event
  38.     @target_e = target_event
  39.     @d_name = d_name
  40.     @direction = event.direction
  41.     return if self.bitmap!=nil
  42.     #创建新的位图
  43.     self.bitmap=RPG::Cache.character(name,0)
  44.     #设置角度
  45.     case @direction
  46.     when 2
  47.       base_angle = 180
  48.     when 4
  49.       base_angle = 90
  50.     when 6
  51.       base_angle = 270
  52.     when 8
  53.       base_angle = 0
  54.     end
  55.     #起始位置
  56.     self.ox = self.bitmap.width / 2
  57.     self.oy = self.bitmap.height / 2
  58.     self.x = event.screen_x
  59.     self.y = event.screen_y-32
  60.     self.z = 11
  61.     x1=self.x
  62.     y1=self.y
  63.     #目标坐标
  64.     x3=target_event.screen_x
  65.     y3=target_event.screen_y-32
  66.     #调整
  67.     adjust_with_distance
  68.     #最高点
  69.     x2=(x1+x3)/2
  70.     y2=(y1+y3)/2-@hight
  71.       case @direction
  72.        ####################################
  73.       when 2  #↓
  74.         x2 = self.x
  75.         y2 = self.y
  76.         y1= y3
  77.         if event.x<target_event.x
  78.           x1= x3-2*(x2-x3).abs
  79.         elsif  event.x>target_event.x
  80.           x1= x3+2*(x2-x3).abs
  81.         else  
  82.           #待修改
  83.           shoot_flat(event.screen_x,target_event.screen_x,event.screen_y,target_event.screen_y)
  84.           return
  85.         end  
  86.           return if @flat_flying
  87.          
  88.     #计算abc值
  89.     @a,@b,@c = HzhjMath.parabola(x1, y1, x2, y2, x3, y3)
  90.     @x  = x2
  91.     #速度、Δ角度计算
  92.     v = @velocity
  93.     @angle = Math.atan2(y2,x2)*180/Math::PI
  94.     @angle_max = -@angle
  95.     t = (x3-x1)/v
  96.     @delta_angle = @angle/t
  97.         if event.x<target_event.x
  98.           self.angle = base_angle+@angle
  99.         elsif  event.x>target_event.x
  100.           self.angle = base_angle-@angle
  101.         else  
  102.           #待修改
  103.           shoot_flat(event.screen_x,target_event.screen_x,event.screen_y,target_event.screen_y)
  104.           return
  105.         end   
  106.     #目标点备份
  107.     @to_x=x3
  108.     @to_y=y3
  109.     ####################################
  110.       when 4 #←
  111.     #计算abc值
  112.     @a,@b,@c = HzhjMath.parabola(x1, y1, x2, y2, x3, y3)
  113.     @x  = x1
  114.     #速度、Δ角度计算
  115.     v = @velocity
  116.     @angle = Math.atan2(y2,x2)*180/Math::PI
  117.     @angle_max = -@angle
  118.     t = (x3-x1)/v
  119.     @delta_angle = 2*@angle/t
  120.        self.angle = base_angle-@angle
  121.            #目标点备份
  122.     @to_x=x3
  123.     @to_y=y3
  124.     ####################################
  125.       when 6  #→
  126.     #计算abc值
  127.     @a,@b,@c = HzhjMath.parabola(x1, y1, x2, y2, x3, y3)
  128.     @x  = x1
  129.     #速度、Δ角度计算
  130.     v = @velocity
  131.     @angle = Math.atan2(y2,x2)*180/Math::PI
  132.     @angle_max = -@angle
  133.     t = (x3-x1)/v
  134.     @delta_angle = 2*@angle/t
  135.         self.angle = base_angle+@angle
  136.             #目标点备份
  137.     @to_x=x3
  138.     @to_y=y3
  139.     ####################################        
  140.       when 8   #↑
  141.         x1=self.x
  142.         y1=self.y
  143.         x2 = target_event.screen_x
  144.         y2 = target_event.screen_y-32
  145.         y3 = y1
  146.         if event.x<target_event.x
  147.           x3= x3+2*(x2-x1).abs
  148.         elsif  event.x>target_event.x
  149.           x3= x3-2*(x2-x1).abs
  150.         else  
  151.           #待修改
  152.           shoot_flat(event.screen_x,target_event.screen_x,event.screen_y,target_event.screen_y)
  153.         end  
  154.           return if @flat_flying
  155.          
  156.     #计算abc值
  157.     @a,@b,@c = HzhjMath.parabola(x1, y1, x2, y2, x3, y3)
  158.     @x  = x1
  159.     #速度、Δ角度计算
  160.     v = @velocity
  161.     @angle = Math.atan2(y1,x1)*180/Math::PI
  162.     @angle_max = -@angle
  163.     t = (x3-x1)/v
  164.     @delta_angle = @angle/t
  165.         if event.x<target_event.x
  166.           self.angle = base_angle-@angle
  167.         elsif  event.x>target_event.x
  168.           self.angle = base_angle+@angle
  169.         else  
  170.           #待修改
  171. shoot_flat(event.screen_x,target_event.screen_x,event.screen_y,target_event.screen_y)
  172.           return
  173.         end   
  174.     #目标点备份
  175.     @to_x=x2
  176.     @to_y=y2
  177.       end
  178. ####################################  
  179.    
  180.    
  181.    
  182.    
  183.    
  184.    
  185.   end  

  186.   def shoot_flat(x1,x3,y1,y3)
  187.     case @direction
  188.     when 2
  189.       self.angle = 180
  190.     when 4
  191.       self.angle = 90
  192.     when 6
  193.       self.angle = 270
  194.     when 8
  195.       self.angle = 0
  196.     end
  197.     #目标点备份
  198.     @to_x=x3
  199.     @to_y=y3
  200.     @x = x1
  201.     @y = y1
  202.     @flat_flying = true
  203.     return
  204.   end
  205.   
  206.   
  207.   def test
  208.       loop do
  209.       update
  210.       Graphics.update
  211.       break if droped?
  212.       end
  213.   end
  214.   #--------------------------------------------------------------------------
  215.   # ● 根据距离自动调整速度、高度
  216.   #--------------------------------------------------------------------------
  217.   def adjust_with_distance
  218.      d = ((@event.x - @target_e.x).abs + (@event.y - @target_e.y).abs).round
  219.      @height = d*32/1.5
  220.      @velocity = d*1.5
  221.   end  
  222.   
  223.   #--------------------------------------------------------------------------
  224.   # ● 刷新画面
  225.   #--------------------------------------------------------------------------
  226.   def update
  227.     super
  228.     if arrived? and not droped?
  229.       drop
  230.     else  
  231.       if droped?
  232.       self.ox = $game_map.display_x / 4
  233.       self.oy = $game_map.display_y / 4
  234.       self.x = @final_x
  235.       self.y = @final_y
  236.       remove
  237.     else
  238.       
  239.       
  240.       self.x=@x
  241.       if @flat_flying
  242.         self.y = @y
  243.       else
  244.         self.y = HzhjMath.parabola_y(@x,@a,@b,@c)
  245.         self.angle -= @delta_angle
  246.       end
  247.       case @direction
  248.       when 2
  249.         if @flat_flying
  250.          @y +=  @velocity
  251.         else
  252.         @x =  @event.x < @target_e.x ? @x+@velocity/2 : @x-@velocity/2
  253.         end  
  254.       when 4
  255.         @x-=@velocity
  256.       when 6
  257.         @x+=@velocity
  258.       when 8
  259.         if @flat_flying
  260.          @y -=  @velocity
  261.         else
  262.         @x =  @event.x < @target_e.x ? @x+@velocity/2 : @x-@velocity/2
  263.         end  

  264.       end
  265.       
  266.       
  267.       end
  268.     end
  269.    
  270.    
  271.   end
  272.   
  273.   #--------------------------------------------------------------------------
  274.   # ● 到达目的地没
  275.   #--------------------------------------------------------------------------
  276.   def arrived?
  277.     case @direction
  278.     when 2
  279.       return self.y>=@to_y
  280.     when 4
  281.       return self.x<=@to_x
  282.     when 6
  283.       return self.x>=@to_x
  284.     when 8
  285.       return self.y<=@to_y
  286.     end
  287.    
  288.    
  289.   end  
  290.   
  291.   #--------------------------------------------------------------------------
  292.   # ● 落地
  293.   #--------------------------------------------------------------------------  
  294.   def drop
  295.     self.angle = 0
  296.     self.bitmap = RPG::Cache.character(@d_name,0)
  297.     self.src_rect.set(rand(4)*100,(@direction/2-1)*100,100,100)
  298.     self.x =@target_e.x*32 - 34
  299.     self.y =@target_e.y*32 - 68
  300.     self.ox = $game_map.display_x / 4
  301.     self.oy = $game_map.display_y / 4
  302.     self.z = 11
  303.     @wait_count = 450
  304.   end  
  305.   #--------------------------------------------------------------------------
  306.   # ● 落地?
  307.   #--------------------------------------------------------------------------  
  308.   def droped?
  309.      return self.bitmap == RPG::Cache.character(@d_name,0)
  310.   end
  311.   #--------------------------------------------------------------------------
  312.   # ● 消失
  313.   #--------------------------------------------------------------------------     
  314.   def remove
  315.     if @wait_count == 0
  316.       self.bitmap = nil
  317.     else
  318.       @wait_count -= 1
  319.     end
  320.   end  
  321.   def delete
  322.     self.bitmap = nil
  323.   end
  324. end
复制代码





鄙人瞎j8乱写的一个从地图上某事件能发射一只抛物线移动的弓箭到目标事件的一个测试的玩意儿。。

于是问题来了, 游戏中,如果人物移动,画面跟着移动的话,弹道就偏了。
我不能使用
    self.ox = $game_map.display_x / 4
    self.oy = $game_map.display_y / 4
这种方法,因为我要把弓箭给旋转一定角度

所以问题来了=A=

这个应该怎么解决









梦石
0
星屑
34511
在线时间
5096 小时
注册时间
2012-11-19
回帖
4715

开拓者

发表于 2015-2-10 17:16:20 | 显示全部楼层
ox,oy 原点一般不变化的话,赋值一次就行了啊。
画面坐标的话,参照 Game_Character 的 screen_x 和 screen_y
在刷新里面你可以写:
箭矢的画面坐标: self.x = (箭矢实际X坐标 * 128 - $game_map.display_x + 3) / 4 + 箭矢位图的宽 / 2
                            self.y = (箭矢实际Y坐标 * 128 - $game_map.display_x + 3) / 4 + 箭矢位图的高

点评

一样有用,和图片旋转没关系。就算左右翻转都一样。  发表于 2015-2-11 08:53
但是我把箭矢旋转了一定的角度就不管用了啊= =  发表于 2015-2-11 03:51

评分

参与人数 1星屑 +100 收起 理由
hys111111 + 100 认可答案

查看全部评分

xp vx va mv  va mz 各类型脚本/插件定制
回复

使用道具 举报

…あたしは天

梦石
0
星屑
2311
在线时间
4033 小时
注册时间
2010-10-4
回帖
10548

开拓者贵宾

发表于 2015-2-10 18:55:03 | 显示全部楼层
本帖最后由 taroxd 于 2015-2-11 10:22 编辑

题外话:抛物线为何不用参数方程?坐标容易算得多

VA 中“跳跃”的参数方程大致是这样的:
  1. x = x0 + (t / ymax / 2) * (x1 - x0)
  2. y = y0 + (t / ymax / 2) * (y1 - y0) + (ymax**2 - (t - ymax)** 2) / 2

  3. 其中

  4. x0, y0 为起始点坐标(地图坐标)
  5. x1, y1 为目标点坐标(地图坐标)
  6. ymax = 10 + sqrt((x1 - x0)**2 + (y1 - y0)**2) - @move_speed, @move_speed 为角色的移动速度
  7. t 为参数(时间,单位为帧),范围为 0..ymax*2
复制代码

点评

请问你这个方程式怎么得出来的。(t / ymax / 2) 这个是啥意思?最后一句ymax=10+**** 10是什么作用?想用你的这个方程,但是不了解。希望详细解答下!  发表于 2019-6-16 19:59
不会  发表于 2015-2-11 09:25

评分

参与人数 1星屑 +100 收起 理由
hys111111 + 100 认可答案

查看全部评分

回复

使用道具 举报

梦石
0
星屑
1821
在线时间
2528 小时
注册时间
2010-10-12
回帖
1336

开拓者

 楼主| 发表于 2015-2-11 12:20:12 | 显示全部楼层
taroxd 发表于 2015-2-10 05:55
题外话:抛物线为何不用参数方程?坐标容易算得多

VA 中“跳跃”的参数方程大致是这样的: ...

所噶,这个容易好多(o゚ω゚o)
这么说斜率怎么算了又
(o゚ω゚o)

点评

当然 dy/dx 也可以用 dy/dt ÷ dx/dt 来算,不过没什么必要就是了  发表于 2015-2-11 12:31
另外,里面所有的除法都是指实数的除法,不是 Ruby 整数的地板除  发表于 2015-2-11 12:29
斜率 (y(t + Δt) - y(t)) / (x(t + Δt) - x(t)) 其中 Δt 取任意一个比较小的数字  发表于 2015-2-11 12:27
这个计算方式算出的是地图坐标。把地图坐标转化为画面坐标自然就涉及到地图滚动了  发表于 2015-2-11 12:25
等等,如果这样算的话,箭会跟着地图一起滚动么?  发表于 2015-2-11 12:21

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部