当前位置: 首页 - Programming
显示模式: 普通 | 列表

游戏中的基础碰撞检测算法

在游戏中,经常需要进行碰撞检测的实现,例如判断前面是否有障碍以及判断子弹是否击中飞机,都是检测两个物体是否发生碰撞,然后根据检测的结果做出不同的处理。

进行碰撞检测的物体可能有些的形状和复杂,这些需要进行组合碰撞检测,就是将复杂的物体处理成一个一个的基本形状的组合,然后分别进行不同的检测。

下面简单介绍一下两种最基本的形状进行碰撞的时候进行的处理。

1、矩形和矩形进行碰撞

一般规则的物体碰撞都可以处理成矩形碰撞,实现的原理就是检测两个矩形是否重叠。我们假设矩形1的参数是:左上角的坐标是(x1,y1),宽度是w1,高度是h1;矩形2的参数是:左上角的坐标是(x2,y2),宽度是w2,高度是h2。

在检测时,数学上可以处理成比较中心点的坐标在x和y方向上的距离和宽度的关系。即两个矩形中心点在x方向的距离的绝对值小于等于矩形宽度和的二分之一,同时y方向的距离的绝对值小于等于矩形高度和的二分之一。下面是数学表达式:

x方向:| (x1 + w1 / 2) – (x2 + w2/2) | < |(w1 + w2) / 2|

y方向:| (y1 + h1 / 2) – (y2 + h2/2) | < |(h1 + h2) / 2|

在程序中,只需要将上面的条件转换成代码就可以实现了。


阅读全文...

标签: 游戏  碰撞  算法 

分类: Programming | Related  引用: 0  评论: 0  点击: 72

ETS named_table

new(Name, Options) -> tid() | atom()

  • Name = atom()
  • Options = [Option]
  •  Option = Type | Access | named_table | {keypos,Pos} | {heir,pid(),HeirData} | {heir,none} | {write_concurrency,bool()}
  •   Type = set | ordered_set | bag | duplicate_bag
  •   Access = public | protected | private
  •   Pos = int()
  •   HeirData = term()
Creates a new table and returns a table identifier which can be used in subsequent operations. The table identifier can be sent to other processes so that a table can be shared between different processes within a node.
The parameter Options is a list of atoms which specifies table type, access rights, key position and if the table is named or not. If one or more options are left out, the default values are used. This means that not specifying any options ([]) is the same as specifying [set,protected,{keypos,1},{heir,none},{write_concurrency,false}].


阅读全文...

标签: ETS  named_table 

分类: Programming | Erlang  引用: 0  评论: 0  点击: 8

xmerl_scan


xmerl_scan
模块 MODULE
xmerl_scan
 
模块摘要 MODULE SUMMARY
This module is the interface to the XML parser, it handles XML 1.0.
 
描述 DESCRIPTION
This module is the interface to the XML parser, it handles XML 1.0. The XML parser is activated through xmerl_scan:string/[1,2] or xmerl_scan:file/[1,2]. It returns records of the type defined in xmerl.hrl. 参考:tutorial on customization functions.


数据类型 DATA TYPES
global_state()
The global state of the scanner, represented by the #xmerl_scanner{} record.
option_list()
Options allow to customize the behaviour of the scanner. 参考:tutorial on customization functions.
Possible options are:
{acc_fun, Fun}
Call back function to accumulate contents of entity.
{continuation_fun, Fun} | {continuation_fun, Fun, ContinuationState}
Call back function to decide what to do if the scanner runs into EOF before the document is complete.
{event_fun, Fun} | {event_fun, Fun, EventState}
Call back function to handle scanner events.
{fetch_fun, Fun} | {fetch_fun, Fun, FetchState}
Call back function to fetch an external resource.
{hook_fun, Fun} | {hook_fun, Fun, HookState}
Call back function to process the document entities once identified.
{close_fun, Fun}
Called when document has been completely parsed.
{rules, ReadFun, WriteFun, RulesState} | {rules, Rules}
Handles storing of scanner information when parsing.
{user_state, UserState}
Global state variable accessible from all customization functions
{fetch_path, PathList}
PathList is a list of directories to search when fetching files. If the file in question is not in the fetch_path, the URI will be used as a file name.
{space, Flag}
'preserve' (default) to preserve spaces, 'normalize' to accumulate consecutive whitespace and replace it with one space.
{line, Line}
To specify starting line for scanning in document which contains fragments of XML.
{namespace_conformant, Flag}
Controls whether to behave as a namespace conformant XML parser, 'false' (default) to not otherwise 'true'.
{validation, Flag}
Controls whether to process as a validating XML parser: 'off' (default) no validation, or validation 'dtd' by DTD or 'schema' by XML Schema. 'false' and 'true' options are obsolete (i.e. they may be removed in a future release), if used 'false' equals 'off' and 'true' equals 'dtd'.
{schemaLocation, [{Namespace,Link}|...]}
Tells explicitly which XML Schema documents to use to validate the XML document. Used together with the {validation,schema} option.
{quiet, Flag}
Set to 'true' if xmerl should behave quietly and not output any information to standard output (default 'false').
{doctype_DTD, DTD}
Allows to specify DTD name when it isn't available in the XML document. This option has effect only together with {validation,'dtd' option.
{xmlbase, Dir}
XML Base directory. If using string/1 default is current directory. If using file/1 default is directory of given file.
{encoding, Enc}
Set default character set used (default UTF-8). This character set is used only if not explicitly given by the XML declaration.


阅读全文...

标签: xmerl_scan 

分类: Programming | Erlang  引用: 0  评论: 0  点击: 13

Erlang OTP 之 Application

application

Generic OTP application functions

In OTP, application denotes a component implementing some specific functionality, that can be started and stopped as a unit, and which can be re-used in other systems as well. This module interfaces the application controller, a process started at every Erlang runtime system, and contains functions for controlling applications (for example starting and stopping applications), and functions to access information about applications (for example configuration parameters).
An application is defined by an application specification. The specification is normally located in an application resource file called Application.app, where Application is the name of the application. Refer to app(4) for more information about the application specification.
This module can also be viewed as a behaviour for an application implemented according to the OTP design principles as a supervision tree. The definition of how to start and stop the tree should be located in an application callback module exporting a pre-defined set of functions.
Refer to OTP Design Principles for more information about applications and behaviours.

Functions


get_all_env() -> Env

get_all_env(Application) -> Env

  • Application = atom()
  • Env = [{Par,Val}]
  •  Par = atom()
  •  Val = term()


阅读全文...

标签: OTP  Application 

分类: Programming | Erlang  引用: 0  评论: 0  点击: 46

Erlang OTP 自定义behaviour

为什么要使用behaviour,如果您对erlang有所了解的话,就明白其中的好处。
可以做到代码通用,可以减少错误,可以使用很多成熟的久经考验的模式,可以减轻无谓的重复劳动等等。。
有些时候,你可能需要定义自己的behaviour,这可不仅仅是OTP的权力。
自己定义behaviour非常简单,仅仅需要几步。
下面是一个例子:


Erlang 代码
  1. -module(my_behaviour).  
  2.   
  3. -export([behaviour_info/1]).  
  4. behaviour_info(callbacks) ->  
  5.     [{init,1},  
  6.      {handle, 2}];  
  7. behaviour_info(_Other) ->  
  8.     undefined.  
  9.   
  10. -export([start/1, stop/0]).  
  11. start(Mod) ->  
  12.     State = Mod:init(0),  
  13.     {ok, State2} = Mod:handle(add,  State),  
  14.     io:format("state :~p~n", [State2]).  
  15. stop() ->  
  16.     stop.  


阅读全文...

标签: OTP  自定义  behaviour 

分类: Programming | Erlang  引用: 0  评论: 0  点击: 37

Erlang四大behaviour之三-gen_event

1. 事件处理规则

在OTP中,事件管理器是一个事件可以发送到的命名对象,一个事件可以是一个错误、一个警告、或者一些要写入日志的信息
在事件管理器中,有0个、一个或者多个事件处理器被安装,当事件管理器被一个事件通知时,这个事件将被安装在事件管理器中的事件处理器处理,
事件管理器用一个进程实现,事件处理器用回调模块实现。事件管理器本质上维护一个{Module, State}列表,每一个Module为一个事件处理器,而State为事件处理器的内部状态。

 

2. 例子

事件处理器的回调模块把错误信息写入终端
 
-module(terminal_logger).
-behaviour(gen_event).
-export([init/1, handle_event/2, terminate/2]).
init(_Args) ->
    {ok, []}.
handle_event(ErrorMsg, State) ->
    io:format("***Error*** ~p~n", [ErrorMsg]),
    {ok, State}.
terminate(_Args, _State) ->
    ok.


阅读全文...

标签: behaviour  gen_event 

分类: Programming | Erlang  引用: 0  评论: 0  点击: 17

Erlang四大behaviour之四 - Supervisor

1. 监督规则

一个监督者负责启动、停止、监控他的子进程。监督者的一个基本概念就是当必要的时候重启子进程保证它们的存活
哪个子进程要重启和被监控是由一个子规程列表决定的,子进程按照列表中指定的顺序启动,并按相反的顺序终止

 

2. 实例

监督者的回调模块
 
-module(ch_sup).
-behaviour(supervisor).
-export([start_link/0]).
-export([init/1]).
start_link() ->
    supervisor:start_link(ch_sup, []).
init(_Args) ->
    {ok, {{one_for_one, 1, 60},
          [{ch3, {ch3, start_link, []},
            permanent, brutal_kill, worker, [ch3]}]}}.


阅读全文...

标签: behaviour  Supervisor 

分类: Programming | Erlang  引用: 0  评论: 0  点击: 31

erl -s make all -s c q


erl -s make all -s c q
的解释


make
A Make Utility for Erlang
The module make provides a set of functions similar to the UNIX type Make functions.
Functions

all() -> up_to_date | error
all(Options) -> up_to_date | error
  • Options = [Option]
  •  Option = noexec | load | netload | <compiler option>
This function first looks in the current working directory for a file named Emakefile (see below) specifying the set of modules to compile and the compile options to use. If no such file is found, the set of modules to compile defaults to all modules in the current working directory.
Traversing the set of modules, it then recompiles every module for which at least one of the following conditions apply:
  • there is no object file, or
  • the source file has been modified since it was last compiled, or,
  • an include file has been modified since the source file was last compiled.
As a side effect, the function prints the name of each module it tries to compile. If compilation fails for a module, the make procedure stops and error is returned.


阅读全文...

标签: 编译 

分类: Programming | Erlang  引用: 1  评论: 0  点击: 13

erlang四大behaviour之二 - gen_fsm

今天介绍erlang的一个非常重要的behaviour,就是gen_fsm-有限状态机,有限状态机的作用非常之多,比如文本解析,模式匹配、 游戏逻辑等等方面的处理都是它的强项,所以这个behaviour非常之重要

1. 有限状态机

有限状态机可以用下面这个公式来表达
 
State(S) x Event(E) -> Actions(A), State(S')

表示的就是在S状态时如果有事件E发生,那么执行动作A后把状态调整到S’。
对于一个用gen_fsm行为实现的状态机来说,状态转变规则被写为符合如下规定的一系列Erlang函数:
StateName( Event, StateData ) ->
.. code for actions here …
{ next_state, StateName’, StateData’ }



2. 一个例子

erlang手册中用这个例子来解释的:开锁问题,有一个密码锁的门,它就可以看作一个状态机,初始状态门是锁着的,任何时候有人按一个密码键就会 产生一个事件,这个键值和前面的按键组合后与密码相比较,看是否正确,如果输入的密码顺序是对的,那么将门打开30秒,如果输入密码不完全,则等待下次按 钮按下,如果输入密码顺序是错的,则重新开始等待按键按下。

 
-module(code_lock).
-behaviour(gen_fsm).
-export([start_link/1]).
-export([button/1]).
-export([init/1, locked/2, open/2]).
start_link(Code) ->
    gen_fsm:start_link({local, code_lock}, code_lock, Code, []).
button(Digit) ->
    gen_fsm:send_event(code_lock, {button, Digit}).
init(Code) ->
    {ok, locked, {[], Code}}.
locked({button, Digit}, {SoFar, Code}) ->
    case [Digit|SoFar] of
        Code ->
            do_unlock(),
            {next_state, open, {[], Code}, 3000};
        Incomplete when length(Incomplete)<length(Code) ->
            {next_state, locked, {Incomplete, Code}};
        _Wrong ->
            {next_state, locked, {[], Code}}
    end.
open(timeout, State) ->
    do_lock(),
    {next_state, locked, State}.


阅读全文...

标签: gen_fsm  behaviour 

分类: Programming | Erlang  引用: 0  评论: 0  点击: 30

erl启动参数说明

最近被一个问题困扰,先把错误提示发出来:
kernel-poll not supported; "K" parameter ignored
{error_logger,{{2010,3,27},{9,23,16}},"Protocol: ~p: register error: ~p~n",["inet_tcp",{{badmatch,{error,duplicate_name}},[{inet_tcp_dist,listen,1},{net_kernel,start_protos,4},{net_kernel,start_protos,3},{net_kernel,init_node,2},{net_kernel,init,1},{gen_server,init_it,6},{proc_lib,init_p_do_apply,3}]}]}
{error_logger,{{2010,3,27},{9,23,16}},crash_report,[[{initial_call,{net_kernel,init,['Argument__1']}},{pid,<0.20.0>},{registered_name,[]},{error_info,{exit,{error,badarg},[{gen_server,init_it,6},{proc_lib,init_p_do_apply,3}]}},{ancestors,[net_sup,kernel_sup,<0.9.0>]},{messages,[]},{links,[#Port<0.65>,<0.17.0>]},{dictionary,[{longnames,true}]},{trap_exit,true},{status,running},{heap_size,377},{stack_size,24},{reductions,454}],[]]}
{error_logger,{{2010,3,27},{9,23,16}},supervisor_report,[{supervisor,{local,net_sup}},{errorContext,start_error},{reason,{'EXIT',nodistribution}},{offender,[{pid,undefined},{name,net_kernel},{mfa,{net_kernel,start_link,[['xge@127.0.0.1',longnames]]}},{restart_type,permanent},{shutdown,2000},{child_type,worker}]}]}
{error_logger,{{2010,3,27},{9,23,16}},supervisor_report,[{supervisor,{local,kernel_sup}},{errorContext,start_error},{reason,shutdown},{offender,[{pid,undefined},{name,net_sup},{mfa,{erl_distribution,start_link,[]}},{restart_type,permanent},{shutdown,infinity},{child_type,supervisor}]}]}
{error_logger,{{2010,3,27},{9,23,16}},std_info,[{application,kernel},{exited,{shutdown,{kernel,start,[normal,[]]}}},{type,permanent}]}
{"Kernel pid terminated",application_controller,"{application_start_failure,kernel,{shutdown,{kernel,start,[normal,[]]}}}"}

Crash dump was written to: erl_crash.dump
Kernel pid terminated (application_controller) ({application_start_failure,kernel,{shutdown,{kernel,start,[normal,[]]}}})

Crash dump was written to: erl_crash.dump
Kernel pid terminated (application_controller) ({application_start_failure,kernel,{shutdown,{kernel,start,[normal,[]]}}})


Abnormal termination




百思不得其解,接着在运行程序的bat文件上下功夫,该BAT文件的内容是:
cd ebin
start werl +P 102400 +K true +S 2 -smp -name xge@127.0.0.1 -setcookie xge -config elog -s xge start


我想启动SHELL的命令行参数有很大关系,所以参看了下DOC文档,如下,使用到的参数高亮一下。



(最后找到问题的原因在于数据库无法连接,因为创建的用户无法对未授权的数据库进行操作。可是错误太怪了,这样的错误真的不好确定出错的原因!)









阅读全文...

标签: 参数  说明  erl 

分类: Programming | Erlang  引用: 0  评论: 0  点击: 92
< 1 2 3 4 5 6 7 >