javascript游戏中的碰撞检测? [英] collision detection in javascript game?

查看:82
本文介绍了javascript游戏中的碰撞检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的地图数组;;


map[0] = [0,0,0,0,0,0]
map[1] = [0,1,0,1,0,1]
map[2] = [0,0,0,0,0,0]
map[3] = [1,0,1,0,1,0]
map[4] = [0,0,0,0,0,0]
map[5] = [0,1,0,1,0,1]

1 =障碍
0 =无:

1= Hurdle 0 = Nothing

我将在地图中检测黑/白玩家和障碍物的碰撞检测。

i've to detect collision detection b/w player and hurdles in map.

player.x& player.y是左上角的参考。
每个障碍的宽度和长度都是40px。

player.x & player.y is the reference from left top . Each hurdle is 40px of width and length .

我需要一个概念而不是代码

i need a roughly concept not codes

推荐答案

你必须将玩家位置标准化为碰撞图单位,因为玩家位置在 px 单位且你的碰撞图位于数组索引单位,然后测试玩家试图输入的字段是否不是障碍。

You have to normalize player position to the collision map unit as player position is in px units and your collision map is in array index units and then test if the field the player tries to enter is not a hurdle.

function normalizePosition(entity){
    return { 
        x: Math.ceil(entity.pos.x / TILE_WIDTH), 
        y: Math.ceil(entity.pos.y / TILE_HEIGHT) 
    }
}

标准化位置将在碰撞图上显示实体坐标然后你必须测试实际的碰撞,看看进入的实体是什么类型的瓷砖

Normalize position will give you entity coordinates on the collision map and next you have to test the actual collision to see what kind of tile is the entity entering

function mapCollision(entity, map){
    var mapCoords = normalizePosition(entity),
        tileType = map[mapCoords.y][mapCoords.x];
    return tileType;
}

它会在地图上返回玩家规范化位置的磁贴类型代码如果你想拥有不同的东西,那么就像某种减速陷阱或任何其他熊坑一样。然后你可以处理不同的情况,其中零将默认接受玩家进入牌。

It will return the code of tile type on the map for player normalized position in case you wanted to have there different things then just blocks like some kind of slowing down traps or whatever other bear pits. You could then handle different cases where zero would default to accepts player entering the tile.

希望我没有告诉太多:)

Hope I didn't tell to much :)

祝你好运,如果你还记得,如果你在完成后与我分享这些效果,我会很高兴:)

Good luck with your game and if you'll remember I'd be happy if you'd share the effects with me when its done :)

汤姆

更新

我做了一个简单的例子,以便更好地解释目的:
http://fiddle.jshell.net/qFCyn/1/

I made a simple example for better interpretation purpose: http://fiddle.jshell.net/qFCyn/1/

这篇关于javascript游戏中的碰撞检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆