Actionscript 3 这是一个类似的游戏;蠕虫”;。物理学。as3
请帮我写一个弹丸离地反弹的函数。如何找到碰撞的角度,反射角应该是多少,或者至少了解碰撞点Actionscript 3 这是一个类似的游戏;蠕虫”;。物理学。as3,actionscript-3,Actionscript 3,请帮我写一个弹丸离地反弹的函数。如何找到碰撞的角度,反射角应该是多少,或者至少了解碰撞点 package { import flash.display.Sprite; import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.BlendMode; import flash.geom.Point; import flash.geom.Matri
package {
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BlendMode;
import flash.geom.Point;
import flash.geom.Matrix;
import flash.events.Event;
import flash.events.MouseEvent;
public class worms extends Sprite {
public var terrain_bmpd=new BitmapData(550,200,true,0xFF00FF00);//This is the Bitmap of the terrain
public var terrain_bmp=new Bitmap(terrain_bmpd);//and this the BitmapData
public var character=new Sprite();//The character will work as a worm
public var hole=new Sprite();//That's the hole we need
public var hole_matrix:Matrix;//The hole_matrix is used to set the position of the hole
public var left_foot:Point;
public var right_foot:Point;//These are the feet of the character. We will use it to check collisions
public function worms() {
draw_objects();//This function draws the character, the terrain and the hole.
stage.addEventListener(Event.ENTER_FRAME,fall);
}
public function fall(e:Event) {
/*This function will move down the character if there isn't a collision
between the terrain and the "feet" of the character*/
for (var i:int=0; i<10; i++) {//We want to check every pixel if there's acollision, so we won't move the character 10 pixels all at once
left_foot=new Point(character.x-5,character.y+10);
right_foot=new Point(character.x+5,character.y+10);
if (!(terrain_bmpd.hitTest(new
Point(terrain_bmp.x,terrain_bmp.y),0x01,left_foot))&&!(terrain_bmpd.hitTest(new Point(terrain_bmp.x,terrain_bmp.y),0x01,right_foot))) {
character.y++;//If there aren't any collisions, make the character fall one pixel
}
}
}
public function draw_objects() {
terrain_bmp.y=200;//The terrain shouldn't be at the top of the stage!
stage.addChild(terrain_bmp);//We can make the terrain visible
character.graphics.beginFill(0x0000FF);//Let's draw the character. It will be a blue rectangle.
character.graphics.drawRect(-5,-10,10,20);
character.x=250;
stage.addChild(character);
hole.graphics.beginFill(0x000000);//Now we draw the hole. It doesn't matter the colour.
hole.graphics.drawCircle(0,0,30);
}
}
}
包{
导入flash.display.Sprite;
导入flash.display.Bitmap;
导入flash.display.BitmapData;
导入flash.display.BlendMode;
导入flash.geom.Point;
导入flash.geom.Matrix;
导入flash.events.Event;
导入flash.events.MouseEvent;
公共类蠕虫扩展了Sprite{
public var terrain_bmpd=new BitmapData(550200,true,0xFF00FF00);//这是地形的位图
public var terrain_bmp=新位图(terrain_bmpd);//这是位图数据
public var character=new Sprite();//该字符将用作蠕虫
public var hole=new Sprite();//这就是我们需要的洞
public var hole_矩阵:matrix;//hole_矩阵用于设置孔的位置
左脚:点;
public var right_foot:Point;//这些是角色的脚。我们将使用它来检查碰撞
公共职能(){
draw_objects();//此函数用于绘制角色、地形和洞。
stage.addEventListener(事件。进入框架,下降);
}
公共功能下降(e:事件){
/*如果没有碰撞,此函数将向下移动角色
在地形和角色的“脚”之间*/
对于(var i:int=0;i平坦、水平地面:
//Check next movementstep
if((vel_y + pos_y) > terrainBottom) {
vel_y = -vel_y*DampingFactor;
}
对于一般情况,您需要使用三角函数将vel_x,vel_y转换为上图所示的情况。与前面的答案一样,使用vel_x和vel_y以向量形式表示角色的运动。使用这些值在每次迭代时增加x和y坐标。在您的示例中,您使用vel_x=0,vel_y=1,因为每次将y坐标增加1
如果曲面与水平面成逆时针方向测量的角度为a,则曲面反弹(重新影响)后的x水平和y水平将为
x_-vel.cos2A+y_-vel.sin2A和x_-vel.sin2A-y_-vel.cos2A。要了解其推导过程,请参见
这是一种完全弹性碰撞(碰撞时不损失速度)。这有几个部分,你似乎真的不知道从哪里开始
像其他人提到的那样,你的物体(投射物、角色等)的运动应该用a表示,即x和y速度。(a点
可以表示向量。)
接下来,要检测基于像素的地形上的碰撞角度,您可以在中执行一系列点(像素)命中测试。这将为您提供一组命中点,您可以将其定义为从对象中心开始的向量
这里有一个函数,我曾经这样做:
/**
* Test for a hit against a shape based on a point and radius,
* returns the angle of the hit or NaN if no hit.
* Like hitTestPoint, xPos and yPos must be in stage coordinates.
*/
function hitTestAngle(shape:Sprite, xPos:Number, yPos:Number, radius:Number, samples:uint = 90):Number {
const PI2:Number = Math.PI * 2, SAMPLE:Number = 1 / samples;
var dx:Number, dy:Number, a:Number, tx:Number = 0, ty:Number = 0, hits:int = 0;
var i:int = samples;
while(i--){
a = PI2 * (i * SAMPLE);
dx = radius * Math.cos(a);
dy = radius * Math.sin(a);
if(shape.hitTestPoint(xPos + dx, yPos + dy, true)){
hits++;
tx += dx;
ty += dy;
}
}
if(!hits)
return NaN;
return Math.atan2(ty, tx) * (180 / Math.PI);
}
这使用了hitTestPoint()
,但由于地形是BitmapData
,因此可以使用getPixel32()
和
您还可以使用,它在引擎盖下使用了类似的方法,具有一系列额外的功能
获得碰撞角度后,可以将定义为要创建的碰撞曲面
这应该让你开始了。请描述你当前看到的错误或不希望看到的行为。你发布的代码似乎根本没有任何投射物。如果((vel_y+pos_y)>terrainBottom)它是什么?弹丸在y方向的速度,弹丸的y位置和地面的y坐标。如何计算角度A?如何计算接触点的坐标?如何计算接触点的坐标?谢谢!工作。超级。