// Returns the fraction of the segment that was visible func (l *Los) TestSeg(seg linear.Seg2) float64 { seg.P = seg.P.Sub(l.in.Pos) seg.Q = seg.Q.Sub(l.in.Pos) wrap := len(l.in.Buffer.ZBuffer) a1 := math.Atan2(seg.P.Y, seg.P.X) a2 := math.Atan2(seg.Q.Y, seg.Q.X) if a1 > a2 { a1, a2 = a2, a1 seg.P, seg.Q = seg.Q, seg.P } if a2-a1 > math.Pi { a1, a2 = a2, a1 seg.P, seg.Q = seg.Q, seg.P } start := int(((a1 / (2 * math.Pi)) + 0.5) * float64(len(l.in.Buffer.ZBuffer))) end := int(((a2 / (2 * math.Pi)) + 0.5) * float64(len(l.in.Buffer.ZBuffer))) count := 0.0 visible := 0.0 for i := start % wrap; i != end%wrap; i = (i + 1) % wrap { dist2 := float32(rays[i].Isect(seg).Mag2()) if dist2 < l.in.Buffer.ZBuffer[i] { visible += 1.0 } count += 1.0 } return visible / count }
func fireDoLine(c *cmwc.Cmwc, pos linear.Vec2, angle, stored float64, speed int, level *game.Level) fireExplosion { rng := rand.New(c) ray := (linear.Vec2{1, 0}) // ray.Scale(math.Abs(rng.NormFloat64()/10) + 50) scale := (stored/5 + 50) * (1 + rng.Float64()*(0.2+stored/2000)) ray = ray.Rotate(angle).Rotate(rng.NormFloat64() * (0.2 + stored/7500)).Scale(scale) seg := linear.Seg2{pos, pos.Add(ray)} base.DoOrdered(level.Room.Walls, func(a, b string) bool { return a < b }, func(_ string, poly linear.Poly) { for i := range poly { if seg.DoesIsect(poly.Seg(i)) { isect := seg.Isect(poly.Seg(i)) seg.Q = isect } } }) p1 := rng.Intn(speed) p2 := rng.Intn(speed) p3 := rng.Intn(speed) return fireExplosion{ Pos: seg.Q, Radius: rng.Float64()*40 + 30, Timer: 0, Start: 1*speed + p1, Peak: 4*speed + p1 + p2, End: 5*speed + p1 + p2 + p3, } }
func (l *Los) DrawSeg(seg linear.Seg2, source string) { seg.P = seg.P.Sub(l.in.Pos) seg.Q = seg.Q.Sub(l.in.Pos) wrap := len(l.in.Buffer.ZBuffer) a1 := math.Atan2(seg.P.Y, seg.P.X) a2 := math.Atan2(seg.Q.Y, seg.Q.X) if a1 > a2 { a1, a2 = a2, a1 seg.P, seg.Q = seg.Q, seg.P } if a2-a1 > math.Pi { a1, a2 = a2, a1 seg.P, seg.Q = seg.Q, seg.P } start := int(((a1 / (2 * math.Pi)) + 0.5) * float64(len(l.in.Buffer.ZBuffer))) end := int(((a2 / (2 * math.Pi)) + 0.5) * float64(len(l.in.Buffer.ZBuffer))) for i := start % wrap; i != end%wrap; i = (i + 1) % wrap { dist2 := float32(rays[i].Isect(seg).Mag2()) // dist = rays[i].Isect(seg).Mag2() if dist2 < l.in.Buffer.ZBuffer[i] { l.in.Buffer.ZBuffer[i] = dist2 l.in.Buffer.SBuffer[i] = source } } }
func distFromPointToSeg(p linear.Vec2, s linear.Seg2) float64 { s.P = s.P.Sub(p) s.Q = s.Q.Sub(p) cross := s.Ray().Cross() crossSeg := linear.Seg2{Q: cross} if crossSeg.Left(s.P) != crossSeg.Left(s.Q) { return s.DistFromOrigin() } da := s.P.Mag() db := s.Q.Mag() if da < db { return da } return db }
func (b *BaseEnt) Think(g *Game) { // This will clear out old conditions b.StatsInst.Think() var dead []int // Calling DoOrdered is too slow, so we just sort the Gids ourselves and go // through them in order. pids := make([]int, len(b.Processes))[0:0] for pid := range b.Processes { pids = append(pids, pid) } sort.Ints(pids) for _, pid := range pids { proc := b.Processes[pid] proc.Think(g) if proc.Dead() { dead = append(dead, pid) } else { b.StatsInst.ApplyCondition(proc) } } // Removed dead processes from the ent for _, id := range dead { delete(b.Processes, id) } if b.Delta.Speed < -1.0 { b.Delta.Speed = -1.0 } if b.Delta.Speed > 1.0 { b.Delta.Speed = 1.0 } // TODO: Speed is a complete misnomer now - fix it! force := b.Delta.Speed * (linear.Vec2{1, 0}).Rotate(b.Target.Angle).Dot((linear.Vec2{1, 0}).Rotate(b.Angle_)) b.ApplyForce((linear.Vec2{1, 0}).Rotate(b.Angle_).Scale(force * b.Stats().MaxAcc())) mangle := math.Atan2(b.Velocity.Y, b.Velocity.X) friction := g.Friction b.Velocity = b.Velocity.Scale( math.Pow(friction, 1+3*math.Abs(math.Sin(b.Angle_-mangle)))) if b.Velocity.Mag2() < 0.01 { b.Velocity = linear.Vec2{0, 0} } else { size := b.Stats().Size() sizeSq := size * size // We pretend that the player is started from a little behind wherever they // actually are. This makes it a lot easier to get collisions to make sense // from frame to frame. epsilon := b.Velocity.Norm().Scale(size / 2) move := linear.Seg2{b.Position.Sub(epsilon), b.Position.Add(b.Velocity)} prev := b.Position walls := g.local.temp.WallCache.GetWalls(int(b.Position.X), int(b.Position.Y)) for _, wall := range walls { // Don't bother with back-facing segments if wall.Right(b.Position) { continue } // Check against the segment itself if wall.Ray().Cross().Dot(move.Ray()) <= 0 { shiftNorm := wall.Ray().Cross().Norm() shift := shiftNorm.Scale(size) col := linear.Seg2{shift.Add(wall.P), shift.Add(wall.Q)} if move.DoesIsect(col) { cross := col.Ray().Cross() fix := linear.Seg2{move.Q, cross.Add(move.Q)} isect := fix.Isect(col) move.Q = isect } } } for _, wall := range walls { // Check against the leading vertex { v := wall.P originMove := linear.Seg2{move.P.Sub(v), move.Q.Sub(v)} originPerp := linear.Seg2{linear.Vec2{}, move.Ray().Cross()} dist := originMove.DistFromOrigin() if originPerp.DoesIsect(originMove) && dist < size { // Stop passthrough isect := originMove.Isect(originPerp).Add(v) diff := math.Sqrt(sizeSq - dist*dist) finalLength := isect.Sub(move.P).Mag() - diff move.Q = move.Ray().Norm().Scale(finalLength).Add(move.P) } else if v.Sub(move.Q).Mag2() < sizeSq { move.Q = move.Q.Sub(v).Norm().Scale(size).Add(v) } } } b.Position = move.Q b.Velocity = b.Position.Sub(prev) } if math.Abs(b.Angle_+b.Target.Angle-math.Pi) < 0.01 { b.Angle_ += 0.1 } else { frac := 0.80 curDir := (linear.Vec2{1, 0}).Rotate(b.Angle_).Scale(frac) targetDir := (linear.Vec2{1, 0}).Rotate(b.Target.Angle).Scale(1.0 - frac) newDir := curDir.Add(targetDir) if newDir.Mag() > 0.01 { b.Angle_ = newDir.Angle() } } }