douyi6960 2015-03-13 18:03
浏览 193

Golang接口强制转换为嵌入式结构

I want to implement a collision library using the interface Collidable

type Collidable interface{
    BoundingBox() (float64,float64,float64,float64)
    FastCollisionCheck(c2 Collidable) bool
    DoesCollide(c2 Collidable) bool
    Collide(c2 Collidable)
}

It has predefined shapes like.

type Circle struct{
X,Y,Radius float64
}

The idea is that I can do

type Rock struct{
    collision.Circle
    ....
}

which then implements the interface Collidable, so I can pass it to a Spatial Hash Map (that expects a collidable). Only thing needed to do would be to override the Collide() function to my needs.

However the functions in type circle can not handle the type rock, even tough it has a circle embedded.

func (c1 *Circle) DoesCollide(i Collidable) bool{
    switch c2 := value.(type) {
    case Circle:
    //doesn't fire, as it is of type Rock (unknown in this package)
    //Needed is something like
    //if i_embeds_Circle then c2 := i_to_Circle 
    }
}

Is this possible? Is there a better way?

  • 写回答

4条回答 默认 最新

  • dqsxsmi3704 2015-03-13 19:58
    关注

    Not sure how off the mark this is, but posting it in case it helps you in any way.

    http://play.golang.org/p/JYuIRqwHCm

    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序